Design Ticketmaster
hard 55 minSell numbered seats to far more people than there are seats.
Deep dives
Compare the options before you open them, then say which you would pick.
Deep dive 1
One seat, two buyers
Where does a hold live?
BreaksRead the status, then write it
- How
- Check in the application, then save.
- Limit
- Both readers see available, both write.
- Choose when
- Never.
BreaksOnly in Redis
- How
- Set a key if absent, with ten minutes of life.
- Limit
- Copies lag behind. A failover can forget a key, and two shoppers hold one seat.
- Choose when
- Where a double hold is harmless.
WorksRedis in front, the database behind
- How
- The key refuses most losers in a millisecond; the row decides.
- Limit
- Two states to keep equal, and a fallback path to test.
- Choose when
- Thousands of tries a second on the same seats.
StrongStatus and end time in the seat's row
- How
- Lock rows in order, check, update, count.
- Limit
- Each try costs a short transaction, a few milliseconds.
- Choose when
- Admission keeps it near 83 a second, as here.
What the tools really do
| Tool | Remember |
|---|---|
| Conditional update | Read committed is enough if you check how many rows changed |
| FOR UPDATE | Blocks other writers and lockers of the row, not plain readers |
| SKIP LOCKED | Good for "any free seat"; for one given seat an empty answer means busy, not sold |
| SERIALIZABLE | Correct too, but expect retries |
| Version number | Optimistic: the second writer sees a changed version and fails |
| SET with NX and EX | One atomic call; release only if the value is still yours |
| Redlock over several nodes | More moving parts, and the row must decide anyway |
if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1])endreturn 0Deep dive 2
The waiting room
14 million people arrive at ten. What do they meet?
BreaksMore servers
- How
- Scale the shop automatically.
- Limit
- It cannot grow 90 times in a minute, and 280 people still want each seat.
- Choose when
- Slow growth over hours.
WorksA place and an open connection for each
- How
- A sorted set holds places; servers push news every 30 seconds.
- Limit
- 140 connection servers, and mass reconnects if some die.
- Choose when
- Hundreds of thousands, or news must arrive in a second.
StrongSigned places and one public number
- How
- Shuffle the lobby once, sign each place, publish now serving at the CDN.
- Limit
- News is up to 30 seconds old; a place is not personal to look up.
- Choose when
- Millions, as here.
| Order | Good | Bad |
|---|---|---|
| By arrival | Feels natural | Rewards speed, distance and scripts |
| Shuffle the early ones, then arrival | Equal chance for everyone who was on time | One person with many accounts has more chances |
| Random for all, always | Simple | A latecomer may jump ahead of people who waited |
Running it
- 1
Admit by people inside
Next batch = 5,000 minus people inside minus passes not used yet. If checkout slows down, admission slows by itself.
- 2
Batches
At 1,667 a minute, a batch each 30 seconds is about 833. Smaller batches make smoother load.
- 3
Passes
Signed, 15 minutes, one account. Unused ones return to the count.
- 4
Say the truth
Place, about how long, and nearly gone when it is. Uncertainty makes people reload.
- 5
Prepare
Raise capacity and warm pages and maps an hour before; keep a switch that stops admission.
- 6
If Redis loses the list
Places are in the signed tokens and the number is in the database, so the line continues.
Deep dive 3
Money
In which order do you take money and sell the seat?
BreaksCharge inside the transaction
- How
- Lock rows, call the provider, commit.
- Limit
- Rows and connections wait for an outside network; a rollback cannot undo a charge.
- Choose when
- Never.
WorksCharge, sell, refund on failure
- How
- Each step has a step that undoes it.
- Limit
- Refunds take days to show, and fans see a charge without a seat.
- Choose when
- Payment methods that cannot reserve.
StrongAuthorise, sell, capture
- How
- Reserve money, commit the sale, take the money afterwards with retries.
- Limit
- Authorisations end after some days, so capture within minutes. A rare capture failure needs a rule: cancel or ask again.
- Choose when
- Cards, as here.
When the provider is silent
- 1
Do not guess
Keep the order pending and the seats paying.
- 2
Ask again with the same key
The answer is the first result. Or look up the payment.
- 3
Listen
The provider's webhook may arrive first. A state change that only goes forward makes order unimportant.
- 4
Give up safely
After some minutes cancel the authorisation, and only then free the seats.
- 5
Compare every day
Provider records against orders. Money without an order is returned; an order without money is looked at by a person.
Deep dive 4
Caches and the live map
5,000 shoppers look at a map. How do they see seats vanish?
BreaksQuery the seat rows on each view
- How
- Every shopper reads 50,000 rows.
- Limit
- Heavy reads on the database that decides holds.
- Choose when
- A few hundred seats.
WorksPush each change to everyone
- How
- A connection per shopper, one message per seat.
- Limit
- 83 changes a second to 5,000 people is 415,000 messages a second.
- Choose when
- Small halls.
StrongOne picture per section each second
- How
- Publish section pictures of about 500 seats; shoppers fetch the section they look at from the edge.
- Limit
- About a second old, so some clicks lose.
- Choose when
- Large halls, as here.
What is cached
| Data | Where | How long | Cleared by |
|---|---|---|---|
| Code, styles | CDN | 1 year, new name on change | Nothing |
| Venue maps, artist pictures | CDN | 24 h | Purge |
| Event details | Redis and CDN | 1 h and 5 min | Event changed message |
| Price tiers | Redis | 5 min | Time |
| Search results | Redis | 60 s | Time |
| Section counts | Redis | 30 s | Time |
| Section picture during a sale | CDN | 1 s | Time |
| Sessions | Redis | 24 h | Sign-out |
| Holds and orders | Not cached |
Edge cases
What can go wrong
| Case | Answer |
|---|---|
| Double click | Same key, same order |
| Browser closed | The hold ends by itself |
| Sign-in ends during a long wait | The signed place survives; sign in again |
| Payment slow or bank prompt | Seats in paying; answer 202, result by message |
| Redis down | Places from tokens; holds never needed it; pages from the database behind a breaker |
| Database dies during checkout | A synchronous copy takes over; the client repeats with its key |
| Order write fails after authorising | Release the money, say so |
| Map says free, seat is gone | 409 and near pairs |
| Two tickets for one seat anyway | The unique rule refuses; refund, apology, alarm, find the bug |
| Scripts | Challenge, limits, risk score, review after the sale |
| Attack at sale time | Edge filters; the waiting page is static |
| Event cancelled | Mark, refund in batches, tell everyone through the stream |
| Barcode photographed | Codes that change in the app, and one scan only |