Design Ticketmaster
hard 55 minSell numbered seats to far more people than there are seats.
See it run
Hold a seat, buy it, and wait in a line of fourteen million: the main flows, one hop at a time.
Operations
Hold two seats
One short transaction gives a shopper two seats for ten minutes, or nothing.
Step 1 of 8
Click on A1 and A2
seats
A1, A2
key
random
The client sends both seats and a random key. Why: a repeat of this request must return the same hold.
Step 1: Click on A1 and A2
def hold(event, seats, user, key, now): if key in holds: return holds[key] # a retry with db.transaction(): rows = db.lock(event, sorted(seats)) # same order for all if len(rows) < len(seats) or not all(free(r, now) for r in rows): return 409, taken(rows, now) for r in rows: r.status, r.hold_id = "held", new_id() r.held_until = now + 10 * MINUTE return 201, hold_id, held_until, pricesdef free(r, now): return r.status == "available" or ( r.status == "held" and r.held_until <= now)- Latency
- < 500 ms p99
- Load
- 83/s a sale
- Hold
- 10 minutes
- Seats
- all or none
Locking rows in one agreed order means two shoppers who want the same pair cannot block each other for ever.
In the real world
The same design ticketmaster doing real jobs. Pick one and step through it.
The card call times out. Was the money reserved or not?
Step 1 of 5
No answer
seats
paying
answer
none
The seats are paying. We do not know the result, so we must not guess.
Step 1: No answer
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Read, then write
criticalTwo shoppers both read available and both write sold.
Wrongif seat.status == "available": seat.status = "sold"; save(seat)RightUPDATE seats SET status = 'held' WHERE id = :s AND status = 'available' -- 1 row or 0A hold without an owner check
criticalThe first lock expired and belongs to someone else now. Deleting it frees their seat.
Wrongredis.delete("lock:A1")Right# in one script: delete only if the value is mineif get("lock:A1") == my_token: delete("lock:A1")A lock and its expiry in two calls
majorA crash between them leaves a lock for ever.
WrongSETNX lock:A1 meEXPIRE lock:A1 600RightSET lock:A1 me NX EX 600Calling the card provider inside a transaction
majorRows stay locked for seconds and every other shopper waits.
WrongBEGIN; lock seats; charge(); COMMITRightmove to paying; authorise; then a short transactionA current time in a partial index
minorPostgreSQL wants an index rule whose result never changes; the current time does.
WrongCREATE INDEX ... WHERE expires_at < now()RightCREATE INDEX ... ON seats (held_until) WHERE status = 'held'
Interview tips
Two problems, not one
- A crowd at the door.
- One seat, two buyers.
- Solve the door first and the seat gets easy.
Say the numbers
- 14M people, 50,000 seats: 280 to 1.
- 1,667 people a minute inside.
- 83 holds a second: one database.
Know the states
- available, held, paying, sold.
- Time frees held, never paying.
- Two rows expected, two rows changed.
Close well
- Fair: shuffle the lobby.
- Safe: authorise, sell, capture.
- Next: bots, resale, several regions.