Lesson 1 of 9 · 22m
The Concurrency Mental Model: Interleavings, Not Parallelism
Why race conditions are really about unexpected instruction interleavings, how single operations decompose into steps, and why low traffic is the best hiding place a race can have.
The first race condition I ever hunted had been in production for three years. It corrupted one order in roughly every two million. Nothing was wrong with the code if you read it line by line — every line was correct. The bug was the space between the lines: the moments where another request, thread, signal, or process could run. That is the mental model you need. Concurrency bugs are not about things happening simultaneously. They are about operations you believed were atomic actually being divisible.
Every high-level operation is many small steps
# looks like one operation
balance -= amount
# actually at least three observable steps
current = read_balance(account) # step 1
new_balance = current - amount # step 2
write_balance(account, new_balance) # step 3Two requests can both perform step 1 before either performs step 3. Both read 100, both subtract 80, both write 20. The customer just spent 160 from an account containing 100. This is the classic read-modify-write race, and it exists in web requests, background jobs, kernels, databases, and smart contracts.
Think in interleavings
Do not ask "can these two pieces of code run at the same time?" Ask "what happens if execution switches after this exact line?" Even on one CPU core, a runtime can switch threads between any two bytecode instructions. In web systems, the switch happens between SQL statements, cache writes, filesystem calls, and network round trips.
A useful exercise is to mark the fragile boundary:
request A: check stock(1 remaining)
request B: check stock(1 remaining)
request A: create order
request B: create order
request A: decrement stock -> 0
request B: decrement stock -> -1 # invariant brokenThe invariant was "stock is never negative." Nobody wrote code that intentionally set it negative. The invariant died because enforcement and mutation were separated by a scheduling gap.
Races are timing amplifiers
Low traffic hides races because the vulnerable window is milliseconds wide. Then you add a cache, move the database across a network, deploy a slower disk, or get a traffic spike — and the window opens. Attackers widen it deliberately with parallel requests, retries, packet timing, or by triggering slow paths such as image processing, antivirus scans, or external API calls.
Atomicity is a property of the whole operation
"Each statement is atomic" does not mean "the business operation is atomic." PostgreSQL can make each UPDATE atomic while your application-level transfer still consists of two separate updates. Redis can make INCR atomic while your check-then-act sequence is not. Atomicity only matters at the boundary of the invariant you care about.
Field notes
- If a comment says "this should never happen," assume it happens under concurrency.
- Logging often serializes execution just enough to make the bug vanish while you observe it.
- Retries turn rare races into common ones because clients repeat the exact operation that failed halfway.
- The most dangerous sentence in a design review is "that endpoint is only called once." Under timeouts, everything is called more than once.
Was this lesson helpful?