Skip to main content
Module 10 · Lesson 1 of 334/42 in the course~22 min
Module lessons (1/3)

TOCTOU Races & Atomic SQL UPDATE Guards

Lesson preview6 hands-on exercises readyApplication & Web Security · Module 10 — Race Conditions, Atomic SQL & Rate Limiting

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 balance and UPDATE queries fail under PostgreSQL READ COMMITTED concurrency.
  • Eliminate race windows permanently with atomic SQL UPDATE ... WHERE balance_cents >= $amount RETURNING guards and deadlock-safe LEAST/GREATEST row 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

  1. 11. Concurrency Races vs Atomic Serialization Patterns
  2. 22. Interactive Explorer: Concurrency Race & Atomic Guard Simulator
  3. 33. Progressive Labs: Warm-Up → Attack → Defend → Harden
  4. 44. Checkpoints & Code Review
Pro Lesson · Module 10 — Race Conditions, Atomic SQL & Rate Limiting

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.