Ledgers and holds: never oversell
Record money as entries that add up, and reserve stock without overselling.
A ledger, not a balance field
Instead of overwriting a balance, record each movement as an entry: −500 from A, +500 to B. The balance is the sum. Entries are never edited, so the history always explains the number.
Holds that expire
When a buyer picks a seat or the last item, place a hold for a few minutes. Others can't take it. If the buyer pays, the hold becomes a sale; if not, it expires and the item is free again.
hold(seat, user):
// succeeds only if nobody holds it
ok = db.update("seats", { heldBy: user, until: now + 10min },
where: { id: seat, heldBy: null or until < now })
return ok
One writer per item
The conditional update above is atomic: two buyers racing for the same seat can't both win. Do this check in the database, not in application memory.
Key takeaways
- Record money as entries; the balance is their sum.
- Holds reserve an item for a short, fixed time.
- Use an atomic conditional update so only one buyer wins.
Try it in the simulator: Keep bookings through a failover
Put this concept into practice on Design Movie Booking System:
- Build the booking flow: API servers → PostgreSQL, with bookings written through Kafka and Workers. Run the rush.
- Add the fault "Database primary failure" with no replica, then with an asynchronous replica, then with synchronous replication.
What to watch: Without a replica, bookings fail during the outage. An asynchronous replica fails over but loses bookings the old primary had confirmed and serves stale seat states. Synchronous replication passes: the simulator can't model seat locks, but it does check that confirmed bookings survive.
Check your understanding
Answer these questions interactively to test what you learned and track progress on your skill map:
- Why prefer ledger entries over updating a balance field?
- Two buyers click the last seat at the same moment. What prevents both getting it?