Business Logic··6 min read
Finding TOCTOU race conditions in coupons, wallets, and state machines
Why unit tests pass sequentially while concurrent HTTP/2 bursts redeem single-use promo codes or overdraw balances in milliseconds.
Key takeaway
Replace read-then-write balance logic with atomic conditional UPDATE statements (WHERE balance >= amount) and test concurrency directly.
Check-then-act fails under parallel requests
Checking a coupon usage count in application code and incrementing it in a second query leaves a window where parallel requests read unused state simultaneously.