Race condition debugging lab. We had 100 consoles in stock. We sold 163, and every unit test passes. Fix it live, then prove it under load.
Practice debugging a race condition 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 12:00 flash sale put 100 limited-edition consoles on sale. In 4 seconds the reservations service confirmed 163 reservations. The warehouse is now calling customers to cancel. The code checks stock before every reservation, and every unit test passes.
2. Observed Symptoms & Metrics
- Affected Endpoints / Components:
POST /reservations on the reservations service, which runs as two replicas behind localhost:8080. Postgres stock and reservations tables.
- Behavior: One request at a time works: the 101st buyer gets
409 Sold out. With 200 buyers at once, far more than 100 succeed. Sometimes stock.quantity even ends above zero while reservations has more than 100 rows.
- Error Rate: HTTP looks clean. Every request returns 201 or 409. The problem only shows in the counts.
- System Metrics: Postgres is healthy. No lock waits, no errors.
3. Application Logs
2026/10/12 12:00:00 reservations service listening on :8080
# psql
SELECT quantity FROM stock WHERE sku = 'CONSOLE-LTD';
quantity
----------
14
SELECT count(*) FROM reservations WHERE sku = 'CONSOLE-LTD';
count
-------
163
4. Your Objective
Step into the live environment while synthetic traffic is running, find the root cause, and patch the code.
To clear this stack, your fix must pass these checks across 10 back-to-back flash sales of 100 units, each hit by 500 buyers at once:
- Error Rate: Fewer than 1% of requests fail with 5xx (
http_errors < 1%).
- Performance: P99 latency stays below 1 second, even with 500 buyers arriving in the same instant (
p99_latency_ms < 1000).
- Data Integrity: Each sale makes exactly 100 reservations, stock ends at exactly 0, and every rejected buyer gets
409 (data_integrity).
Environment note: an admin service on localhost:9400 runs the sales. curl -X POST localhost:9400/reset restocks every SKU and clears the reservations, and curl localhost:9400/sales/CONSOLE-LTD shows the stock left and the reservations made. The background traffic runs a 150-buyer sale every 20 seconds, and the grader restocks before each of its sales.
Toolbox: curl -X POST localhost:8080/reservations -H 'Content-Type: application/json' -d '{"sku": "CONSOLE-LTD", "customerId": 1}', psql "$DATABASE_URL", and 300 buyers at once: seq 300 | xargs -P 300 -I{} curl -s -o /dev/null -w '%{http_code}\n' -X POST localhost:8080/reservations -H 'Content-Type: application/json' -d '{"sku": "CONSOLE-LTD", "customerId": {}}' | sort | uniq -c. 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 service), 409 (HTTP "Conflict", used here for sold out), 5xx (any HTTP 500-range error), p99 (99% of requests finished faster than this), SKU (Stock Keeping Unit, the product code).
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.