Go + Redis + Postgres. Guarantees an operation executes exactly once under concurrent retries, within a single Redis primary's serialized execution window.
.
├── cmd/server/main.go # Smoke-test entry point
├── internal/
│ ├── lease/service.go # Core API: CheckAndHold, Settle, Cancel, GetStatus
│ └── store/
│ ├── redis.go # Lua script loading + Redis calls
│ └── postgres.go # Durable settled outcomes
├── scripts/
│ ├── check_and_hold.lua # Atomic hold acquisition
│ ├── cancel.lua # Holder-safe hold release
│ └── mark_settled.lua # Cache update after Postgres write
├── migrations/
│ └── 001_create_settled_outcomes.sql
├── test/
│ └── integration_test.go # The one invariant test
└── docker-compose.yml # Real Redis + Postgres
docker compose up -d
# Wait ~5 seconds for healthchecks to pass, then:
go test -v -race -count=1 ./test/| Decision | Where it's explained |
|---|---|
| Lua vs MULTI/EXEC | scripts/check_and_hold.lua header |
| Postgres-before-Redis write order | internal/store/postgres.go WriteSettledOutcome() comment |
| Why TTL and not a heartbeat | internal/lease/service.go Config struct comments |
| Cancel safety (holder ID check) | scripts/cancel.lua header |
| EVALSHA + NOSCRIPT recovery | internal/store/redis.go evalSHA() |
| ON CONFLICT DO NOTHING in Postgres | internal/store/postgres.go WriteSettledOutcome() |
| GetStatus read-repair path | internal/lease/service.go GetStatus() |
TestExactlyOnceUnderConcurrentRetries: 50 goroutines race for the same key.
- Exactly 1 acquires the hold (Redis Lua atomicity)
- Exactly 1 calls Settle (application-level exactly-once)
- Exactly 1 Postgres row exists (durability + ON CONFLICT protection)
- GetStatus returns StatusSettled with valid result (read path)
Say these unprompted in interviews:
-
Failover window: async Redis replication → holds written just before primary death may not exist on the promoted replica. Fix:
WAITor synchronous replication. Otherwise: document as "exactly once except during the failover window." -
TTL miscalibration: if
DefaultHoldTTL < p99_operation_latency, the lease expires mid-execution and a second caller can acquire. Calibrate from real measurements. -
Redis Cluster cross-slot: keys for the same logical resource must hash to the same slot. Fix: hash tags —
{resource_id}:holdand{resource_id}:settledshare{resource_id}as the hash tag.