Skip to content
StackDecode
System Design
16. Performance EngineeringAnimated

Race Conditions

Results depend on timing.

The idea

Two operations touch shared data without coordination and the outcome varies.

Watch it work

Press play, pause, or step through. Some animations have settings you can change.

Txn 1DatabaseTxn 2UPDATE balance = 0 (uncommitted)
  • current message
  • earlier
  • failure or attack

Txn 1 changes a value but has not committed.

step:
1/5
from:
Txn 1
to:
Database
1/5

Time runs downward. Solid arrows are requests, dashed arrows are replies.

Step by step

  1. 1Two threads read the value
  2. 2Both modify it
  3. 3Both write back
  4. 4One update is lost

Where it is used

Concurrent code.

Watch out for

Use locks, atomics or message passing.

Related