AlgoViz

Design Ticketmaster

hard 55 min

Sell 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

Our systemShopperGatewayInventorySeat database

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

ShopperCheckoutPayment serviceCard provideroutsideSeat databaseEvent streamTickets, e-mail

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

    critical

    Two shoppers both read available and both write sold.

    Wrong
    if seat.status == "available":    seat.status = "sold"; save(seat)
    Right
    UPDATE seats SET status = 'held' WHERE id = :s  AND status = 'available'   -- 1 row or 0
  • A hold without an owner check

    critical

    The first lock expired and belongs to someone else now. Deleting it frees their seat.

    Wrong
    redis.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

    major

    A crash between them leaves a lock for ever.

    Wrong
    SETNX lock:A1 meEXPIRE lock:A1 600
    Right
    SET lock:A1 me NX EX 600
  • Calling the card provider inside a transaction

    major

    Rows stay locked for seconds and every other shopper waits.

    Wrong
    BEGIN; lock seats; charge(); COMMIT
    Right
    move to paying; authorise; then a short transaction
  • A current time in a partial index

    minor

    PostgreSQL wants an index rule whose result never changes; the current time does.

    Wrong
    CREATE INDEX ... WHERE expires_at < now()
    Right
    CREATE 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.