The Real Fullstack

The Lost Update

Two customers paid in at the same second. Why does the balance only show one payment?

Explorer7 minTimeline simulatorCode challenge
javapythongocsharpjavascriptsql
Mission:Reproduce the anomaly by choosing how the two threads interleave, then pick an implementation that is correct for every possible interleaving — not only the one you are looking at.Context · Learning objectives ▾

Pocketly, a fictional digital wallet, credits incoming transfers in a worker pool. Support has received a few complaints: a transfer is confirmed, but the balance never increased. Logs show both deposits ran and no exception was thrown.

Mission: Reproduce the anomaly by choosing how the two threads interleave, then pick an implementation that is correct for every possible interleaving — not only the one you are looking at.

Learning objectives

  • Explain why read-modify-write on shared state is not atomic.
  • Distinguish visibility (volatile) from atomicity.
  • Compare locks, atomic operations and optimistic retries.
SimulationSimulated

Run the simulation to see output.

Run simulation
class Account {  private long balance = 100;  void deposit(long amount) {    long current = balance;          // READ    balance = current + amount;      // WRITE (another thread may have written in between)  }}

Simplification

Each thread performs exactly two steps (read, write). Real CPUs, JITs and databases add caching and reordering, which make races more likely, not less. Educational model — numbers illustrate behaviour, they are not benchmarks.

Concepts

Related challenges

References