Module lessons (1/3)
TOCTOU Races & Atomic SQL UPDATE Guards
Reproduce async check-then-act double spends and eliminate race windows with single-statement PostgreSQL row-locked predicates.
What you'll learn in this lesson
- Reproduce a Time-of-Check to Time-of-Use (TOCTOU) race condition where concurrent async requests double-spend a wallet balance.
- Understand why separate
SELECT balanceandUPDATEqueries fail under PostgreSQLREAD COMMITTEDconcurrency. - Eliminate race windows permanently with atomic SQL
UPDATE ... WHERE balance_cents >= $amount RETURNINGguards and deadlock-safeLEAST/GREATESTrow locks.
Overview
If your backend checks if (wallet.balance >= amount) in memory (or runs SELECT balance), yields at an await or network round trip, and then subtracts the amount, two parallel HTTP requests arriving within the same millisecond both inspect the stale 100 balance, both pass the check, and both withdraw $70 ($140 drained from a $100 account).
Even inside SQL transactions at PostgreSQL's default READ COMMITTED isolation level, splitting SELECT and UPDATE without FOR UPDATE row locks invites double spending. Pushing precondition + mutation into one atomic UPDATE ... WHERE ... RETURNING statement closes the window completely.
Sections covered in this lesson
- 11. Concurrency Races vs Atomic Serialization Patterns
- 22. Interactive Explorer: Concurrency Race & Atomic Guard Simulator
- 33. Progressive Labs: Warm-Up → Attack → Defend → Harden
- 44. Checkpoints & Code Review
Unlock "TOCTOU Races & Atomic SQL UPDATE Guards" and the full Application & Web Security curriculum
Reproduce async check-then-act double spends and eliminate race windows with single-statement PostgreSQL row-locked predicates.
Unlock all 6 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Application & Web Security plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.