Database deadlock: Money transfers fail at random under load
Database deadlock debugging lab. Transfers pass every test, but under real traffic a few percent fail with a 500 and the rest slow down.
Practice debugging a database deadlock in a live, broken production system in your browser, with 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
The payments service passed every test in staging and went live this morning. Under real settlement traffic, a few percent of transfers now fail with 500 transfer failed, and some of the ones that succeed take over a second. Support says the failures look random. Nobody can reproduce them with a single request.
2. Observed Symptoms & Metrics
- Affected Endpoints / Components:
POST /transfers on the payments service, Postgres accounts and transfers tables.
- Behavior: Single transfers by hand always succeed. At 40 req/s, failures start within seconds. Most traffic is settlement between the merchant account (1) and the payout account (2), in both directions.
- Error Rate: About 3 to 8% of transfers fail with HTTP 500.
- System Metrics: Postgres CPU is low, but transfers keep piling up in
pg_stat_activity with wait_event_type = Lock. P99 latency climbs past 1 second in bursts that line up with the errors.
3. Application Logs
2026/10/12 02:14:00 payments service listening on :8080
2026/10/12 02:14:02 transfer 2 -> 1 failed: ERROR: deadlock detected (SQLSTATE 40P01)
2026/10/12 02:14:03 transfer 1 -> 2 failed: ERROR: deadlock detected (SQLSTATE 40P01)
# postgres
2026-10-12 02:14:02.311 UTC [2187] ERROR: deadlock detected
2026-10-12 02:14:02.311 UTC [2187] DETAIL: Process 2187 waits for ShareLock on transaction 9921; blocked by process 2190.
Process 2190 waits for ShareLock on transaction 9919; blocked by process 2187.
2026-10-12 02:14:02.311 UTC [2187] CONTEXT: while locking tuple (0,2) in relation "accounts"
4. Your Objective
Step into the live environment while synthetic traffic is running, diagnose the root cause, and patch the codebase.
To clear this stack, your fix must satisfy these automated verification checks under a 60-second load test:
- Error Rate: Fewer than 1% of requests fail (
http_errors < 1%).
- Performance: P99 latency stays below 300 ms (
p99_latency_ms < 300).
- Data Integrity: Money is never created or lost: the 20 accounts always total exactly 20,000, no balance goes negative, and at least 3,000 transfers complete (
data_integrity).
Environment note: POST /internal/reset puts every account back to 1,000. The grader calls it before each verification, so leave it in place.
Toolbox: psql "$DATABASE_URL", curl -X POST localhost:8080/transfers -H 'Content-Type: application/json' -d '{"from": 1, "to": 2, "amount": 10}'. Save a file in the editor to rebuild and restart the app. Click Verify when you think it's fixed.
Your account and data
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.
Privacy Policy · Terms of Service · Browse stacks ·
hello@bustedstack.com
This site needs JavaScript to run labs.