The Lost Update
Two customers paid in at the same second. Why does the balance only show one payment?
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.
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
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) }}