System design lab · Episode 15

The last ticket: when two buyers win the same seat

Three tickets are left, and six people press “Buy” at almost the same moment. If the app reads the count, thinks, and then writes the new count, two people can both see “1 left” and both get the ticket. This is a race condition, and it hides until the day many people arrive together. Follow the lessons next to the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
1234561TICKETS LEFTv0
Ball: a read or an answer. Flat tile: a writeKiosks at the left, the database at the right

Controls

How a kiosk buys
Who comes
Time

Want the details?

What is a race condition?

A race condition happens when the result depends on the timing of two things running at the same time. Here, each buyer reads the count and later writes a new count. Between the read and the write, another buyer can read the same old count. Both decide “there is one left”, and both buy it. Testing with one user never shows the bug; it appears on the busy day.

How to read the bench
Part on the benchWhat it means in a real system
Kiosks 1 to 6Buyers, each through an app server.
Ball on a laneA read, or the database's answer. One trip takes 0.35 s.
Flat tileA write.
Big number on the databaseThe row: how many tickets are left.
PadlockThe row is locked by one buyer; parked balls are buyers waiting for it.
VersionA counter that goes up with every write.
Ticket on a kioskThat buyer got a ticket. Red: an oversold ticket.
The numbers
  • Each trip to or from the database takes 0.35 s; each buyer thinks 0.5 s between reading and writing; a failed version check waits 0.3 s before starting again.
  • Six buyers for three tickets, arriving 0.25 s apart: read-then-write sells all 6 and leaves the count at 1. The lock sells exactly 3, but the last buyer waits about 4.5 s. The version check sells exactly 3 after 7 retries, not in arrival order. One atomic step sells exactly 3, in order, everyone done in about 1.2 s.
In real systems
  • Transactions alone do not always save you. Most databases default to an isolation level called Read Committed. A transaction that reads, waits for the app, then writes can still lose an update at that level. Stricter levels (Repeatable Read, Serializable) make the database stop one of the two transactions with an error, and the app must try again.
  • Pessimistic locking: SELECT … FOR UPDATE locks the row until the transaction ends. Safe and simple, but never keep a lock while a person types or a slow service answers.
  • Optimistic locking: a version column, and UPDATE … WHERE id = ? AND version = ?. If no row was changed, someone was first; read again. Many frameworks do this for you (for example JPA's @Version). Good when conflicts are rare.
  • Atomic update: UPDATE tickets SET left = left - 1 WHERE id = ? AND left > 0, then check that one row changed. Best when the rule fits in one statement.
  • Seats with numbers: give each seat its own row and a unique constraint on (event, seat) in the bookings table. The database itself refuses the second booking. Combine it with an idempotency key so a double click does not book twice.
Words to know
Race Condition
A result that depends on the timing of things running at the same time.
Lost Update
Two writes based on the same old value; one silently replaces the other.
Transaction
A group of reads and writes that the database treats as one unit.
Isolation Level
How much one transaction can see of others running at the same time.
Pessimistic Lock
Lock first, so nobody else can change the row meanwhile.
Optimistic Lock
Do not lock; check at write time that nothing changed, and retry if it did.
Atomic
Done as one step that nothing can come between.