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 bench | What it means in a real system |
|---|---|
| Kiosks 1 to 6 | Buyers, each through an app server. |
| Ball on a lane | A read, or the database's answer. One trip takes 0.35 s. |
| Flat tile | A write. |
| Big number on the database | The row: how many tickets are left. |
| Padlock | The row is locked by one buyer; parked balls are buyers waiting for it. |
| Version | A counter that goes up with every write. |
| Ticket on a kiosk | That 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 UPDATElocks 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
versioncolumn, andUPDATE … 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.