AlgoViz

Design Ticketmaster

hard 55 min

Sell 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

ToolRemember
Conditional updateRead committed is enough if you check how many rows changed
FOR UPDATEBlocks other writers and lockers of the row, not plain readers
SKIP LOCKEDGood for "any free seat"; for one given seat an empty answer means busy, not sold
SERIALIZABLECorrect too, but expect retries
Version numberOptimistic: the second writer sees a changed version and fails
SET with NX and EXOne atomic call; release only if the value is still yours
Redlock over several nodesMore moving parts, and the row must decide anyway
Lua
if redis.call('GET', KEYS[1]) == ARGV[1] then  return redis.call('DEL', KEYS[1])endreturn 0
If you do use a Redis lock: free it only when it is yours, in one step.

Deep 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.
OrderGoodBad
By arrivalFeels naturalRewards speed, distance and scripts
Shuffle the early ones, then arrivalEqual chance for everyone who was on timeOne person with many accounts has more chances
Random for all, alwaysSimpleA latecomer may jump ahead of people who waited

Running it

  1. 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. 2

    Batches

    At 1,667 a minute, a batch each 30 seconds is about 833. Smaller batches make smoother load.

  3. 3

    Passes

    Signed, 15 minutes, one account. Unused ones return to the count.

  4. 4

    Say the truth

    Place, about how long, and nearly gone when it is. Uncertainty makes people reload.

  5. 5

    Prepare

    Raise capacity and warm pages and maps an hour before; keep a switch that stops admission.

  6. 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. 1

    Do not guess

    Keep the order pending and the seats paying.

  2. 2

    Ask again with the same key

    The answer is the first result. Or look up the payment.

  3. 3

    Listen

    The provider's webhook may arrive first. A state change that only goes forward makes order unimportant.

  4. 4

    Give up safely

    After some minutes cancel the authorisation, and only then free the seats.

  5. 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

DataWhereHow longCleared by
Code, stylesCDN1 year, new name on changeNothing
Venue maps, artist picturesCDN24 hPurge
Event detailsRedis and CDN1 h and 5 minEvent changed message
Price tiersRedis5 minTime
Search resultsRedis60 sTime
Section countsRedis30 sTime
Section picture during a saleCDN1 sTime
SessionsRedis24 hSign-out
Holds and ordersNot cached
When a popular key ends, one request refreshes it, others get the old copy, and some keys are refreshed a little early.

Edge cases

What can go wrong

CaseAnswer
Double clickSame key, same order
Browser closedThe hold ends by itself
Sign-in ends during a long waitThe signed place survives; sign in again
Payment slow or bank promptSeats in paying; answer 202, result by message
Redis downPlaces from tokens; holds never needed it; pages from the database behind a breaker
Database dies during checkoutA synchronous copy takes over; the client repeats with its key
Order write fails after authorisingRelease the money, say so
Map says free, seat is gone409 and near pairs
Two tickets for one seat anywayThe unique rule refuses; refund, apology, alarm, find the bug
ScriptsChallenge, limits, risk score, review after the sale
Attack at sale timeEdge filters; the waiting page is static
Event cancelledMark, refund in batches, tell everyone through the stream
Barcode photographedCodes that change in the app, and one scan only