Lock expiry debugging lab. Three payout workers share a Redis lock so only one pays each seller. Sellers keep getting paid twice.
Practice debugging a lock expiry in a live, broken production system in your browser, with Redis and PostgreSQL. Find the root cause in the logs, terminal and code, fix it, and a load test checks that the fix holds under real traffic.
1. The Incident Context
Three copies of the payout worker each run a pass every few seconds. They share a Redis lock so only one of them settles each seller's batch. Last night 61 sellers were paid twice. The lock code looks correct and Redis never failed over. The duplicates cluster around moments when the bank API was slow.
2. Observed Symptoms & Metrics
- Affected Endpoints / Components: the
payouts worker (3 replicas: w1, w2, w3), Redis keys lock:payout:{sellerId}, the bank API at localhost:9600/transfers, Postgres ledger and payouts tables.
- Behavior: For each seller, a worker takes
SET lock:payout:{id} 1 NX PX 5000, reads the seller's unpaid ledger entries, calls the bank, writes the payout row, and then runs DEL on the lock. Most batches are paid exactly once. Duplicates only show up for batches where the bank call was slow.
- Error Rate: No errors are logged. The problem only shows in the bank's transfer log and the
payouts table.
- System Metrics: Bank API latency is 1 to 9 seconds, and about 1 call in 20 takes 15 seconds. New sales land in
ledger all the time. Redis and Postgres are healthy.
3. Application Logs
2026/10/12 00:00:00 payouts worker w1 started
2026/10/12 00:00:00 payouts worker w2 started
2026/10/12 00:00:10 w1 acquired lock:payout:118, 4 entries, total 312.40
2026/10/12 00:00:15 w2 acquired lock:payout:118, 4 entries, total 312.40
2026/10/12 00:00:17 w1 bank transfer ok seller=118 amount=312.40 ref=tx-088a1
2026/10/12 00:00:17 w1 released lock:payout:118
2026/10/12 00:00:19 w2 bank transfer ok seller=118 amount=312.40 ref=tx-088c4
# psql: SELECT seller_id, count(*) FROM payouts WHERE created_at > now() - interval '1 day' GROUP BY 1 HAVING count(*) > 1;
(61 rows)
4. Your Objective
Step into the live environment while the workers are paying sellers, find the root cause, and patch the code.
To clear this stack, your fix must pass these checks during a 150-second run with 3 worker replicas, 200 sellers, new ledger entries arriving all the time, the bank's slow responses, and one replica killed with SIGKILL in the middle of a transfer:
- Data Integrity: No ledger entry is paid more than once, according to the bank's own transfer log (
double_paid_entries == 0).
- Completeness: Every entry created more than 30 seconds before the end of the run is paid (
unpaid_eligible == 0).
- Accounting: The bank and the ledger agree: every entry the bank paid has its
payout_id set and the other way round, and every transfer's amount is the sum of its entries (ledger_mismatches == 0).
Environment note: the bank API accepts an optional Idempotency-Key header. A repeated key gets 409 along with the original transfer. The bank sends the money as soon as a request arrives, so because of the kill, some transfers will reach the bank with nothing written on our side.
Toolbox: redis-cli monitor, redis-cli pttl lock:payout:118, curl 'localhost:9600/transfers?seller=118', curl localhost:9600/stats, psql "$DATABASE_URL", and curl -X POST -H "X-Agent-Secret: $AGENT_SECRET" 'localhost:9000/app/kill?replica=2' to kill a worker yourself. Save a file in the editor to rebuild and restart the app. Click Verify when you think it's fixed.
Terms in this lab: replica (one running copy of the worker), ledger (the list of amounts owed to sellers), NX (set only if the key doesn't exist), PX 5000 (expire after 5000 ms), TTL (time to live), SIGKILL (kills a process at once, no cleanup runs), idempotency key (an ID that lets the receiver tell a retry from a new request), 409 (Conflict).
You can browse stacks without an account. To run labs you sign in with your email or Google account. BustedStack uses your email address to
sign you in, and your name and profile picture to show your account and pre-fill certificates. We never access your Gmail, contacts or Drive,
sell your data or run ad trackers.
This site needs JavaScript to run labs.