Systemsfor Aravinda
0 / 78 core
Week 11 · Case studies

Design ticket sales

1.5 hours · core path

Read first: Transactions and isolation, Caching, Load shedding

In one sitting

The steady catalog is a cached page. The system you are designing is the minute a popular show goes on sale: a waiting room that admits buyers at a rate the seat primary can commit, a hold that expires, and a conditional update so two fans cannot sell the same seat.

Most minutes of a ticket site are a brochure. A few thousand people look at shows that are not on sale yet, and a cache answers them. The minute that defines the design is the one where a single show opens and a few hundred thousand people arrive together. If you size the system for the quiet day, that minute takes the database down and then oversells whatever survived.

Requirements

The user is a fan trying to buy seats to one announced show. The venue needs the number of sold seats to match the number of chairs, including after crashes, retries, and abandoned checkouts.

Functional requirements:

  • Show the catalog and the event page.
  • At on-sale, put fans in a line and admit them onto a buy path at a controlled rate.
  • Hold specific seats, or a count of general-admission spots, for a short checkout.
  • Take payment and flip the hold to sold, once, or release it.

Non-goals:

  • A resale marketplace. Inventory here is the venue’s, once.
  • A pricing model that reprices every few seconds. Fix the price for the hold.
  • Recommendations, artist pages, and the email you send the next morning. Those can lag.
  • Assigned-seat beauty. The map can be plain. The row that says the seat is free has to be right at the moment of the hold.

Estimation

Separate the quiet day from the on-sale minute. Both sets of numbers are assumptions.

Quiet day:

  • 3,000,000 event-page views.
  • The pages are cacheable. The seat map of a show that is not on sale can be cached too.
3,000,000 / 86,400 ≈ 35 page views/s
shortcut, day ≈ 100,000 s:  3,000,000 / 1e5 = 30/s

A day is 86,400 seconds. Thirty-five reads a second is a CDN problem if it is a problem at all. Do not draw the on-sale design for this number.

On-sale minute, still assumptions:

  • One arena, 15,000 seats.
  • 600,000 fans try to buy.
  • Half of them arrive in the first 60 seconds: 300,000 / 60 = 5,000 arrivals a second at the door.
  • The seat primary can commit 400 conditional updates a second for this event. That is a planning assumption you would replace with a measurement. Admit about 300 a second so the primary has headroom.
  • A fan who gets past the door and then sits on a connection occupies it for 3 seconds if you have no line. That 3 seconds is an assumption about a tangled request, not a goal.
arrivals in minute 1          5,000 / s
commits the primary can take  400 / s
admit target                  300 / s

line drain, if all 600,000 stay
  600,000 / 300 = 2,000 s ≈ 33 min

no line, Little's law
  5,000 * 3 s = 15,000 in-flight connections

Fifteen thousand concurrent checkouts against a database that can finish a few hundred commits a second is how the front door falls over before a single seat is fairly sold. The waiting room exists to turn 5,000 arrivals a second into 300 admits a second.

A second pair of knobs, because admit rate and inventory are different limits. Assumption: a hold lasts 8 minutes, and one order may pin up to 4 seats. If you admitted 300 fans a second for the whole hold window, 300 × 480 = 144,000 people would be in checkout, and you only have 15,000 seats. Cap how many fans are inside checkout at about twice the seats still free (assumption: 2×, so 30,000 at the start). The line releases a new fan when someone pays, gives up, or expires.

High-level design

The edge serves the waiting-room page from the CDN and only lets inventory calls through with a token for this event. A per-event line hands out a position and admits a batch when the checkout cap and the commit budget both allow it. Admitted fans read a seat map that may be a second stale, then take a hold on the primary. Payment runs after the hold commits. A sweeper returns expired holds.

flowchart LR
  fan[Fan]
  edge[Edge waiting room]
  admit[Admitted buyers]
  hold[Hold path]
  seats[Seat primary]
  pay[Payment call]
  sweep[Hold expiry]
  fan --> edge
  edge --> admit
  admit --> hold
  hold --> seats
  hold --> pay
  sweep --> seats

All seats for one event live on one primary, partitioned by event id. A transaction that has to lock seats across two databases is a two-phase commit you do not want on the hottest key of the year. The line protects that primary. It does not replace it.

Deep dive

The line is allowed to be approximate

At 5,000 arrivals a second, handing out a position is an increment. One row that does nothing but next_ticket = next_ticket + 1 can take that rate if the statement is tiny. If it cannot, the waiting-room owner for that event (a single process with a lease) hands out positions from memory and persists a checkpoint. A crash may skip a few positions. Fans see an estimate of “about 40,000 people ahead.” You are allowed to be off.

You are not allowed to be off in the seat table. Say that split explicitly. The line is a load shedding mechanism with a place to stand. Shedding without a line is a 503. Shedding with a line is the product. Both are valid. The line is the better product until the line itself is unbounded.

Bound it. Assumption: you keep at most the 600,000 who already arrived, and further arrivals get a cheap rejection at the edge, a static page, not a database query. A queue without a bound is a memory leak. Rate limiting per account happens at the edge too, so one bot farm does not occupy the first hundred thousand positions. The limit is on who may join the line. It is not the inventory check.

Polling the line has to be boring. A position read comes from the admitted-counter in cache, not from a count of seat rows. If every fan refreshes against the primary once a second, you have rebuilt the 5,000-per-second problem with extra steps. Assume a 10-second refresh. 600,000 / 10 = 60,000 cache reads a second, which a small cache tier can answer, and zero seat locks.

Inventory that cannot oversell

Reserved seats are one row per seat: free, held, or sold, plus a hold id and an expiry. The hold is one conditional update: set held where this seat is still free. Zero rows means someone else has it. The fan picks again or returns to the line. Do not retry the same seat in a loop.

General admission is one counter row for the event, decremented where remaining is at least the party size. That is the same statement as a hotel room count. Read the count in the application, then insert a ticket row, and transactions at snapshot isolation will not save you. Both fans can see “one left” in their snapshot and both insert, because the inserts are different rows. Write skew. The counter row, or the seat row, is the row the two updates have to conflict on.

The failure mode teams actually ship is a cached remainder. Assumption: 40 application servers each cache “100 seats left” for the last block, as a copied number rather than a partition of distinct seats. Each server sells from its copy. You can sell that last block dozens of times over. Caching the event poster is right. Caching the right to decrement is the oversell. The seat map may be a second out of date so the picture loads. The hold path ignores the cache and runs the conditional update on the primary.

Shard by event, not by seat, unless the seats in one shard are a real partition of the inventory with their own counters. A hash of seat id that spreads one general-admission counter’s logical pool across primaries forces you to add the pieces back together. That sum is the write-skew bug again.

The hold has to expire, including during payment

Assumption: 8 minutes to pay, one extension of 2 minutes once payment has started. Both numbers live on the row. The extension is itself a conditional update that matches the hold id and uses the database clock. If it matches zero rows, the hold is already gone and you do not charge.

Confirm matches the hold id, the seats, and an expiry still in the future. The sweeper matches the hold id and an expiry in the past. One of those statements wins. A confirm that names only the seat can sell a seat that was released and held by the next fan.

Payment sits outside the database transaction. Commit the hold, extend it, charge, then flip to sold. A charge that runs first will capture money for a seat the sweeper has already given to someone else.

The fan’s double click and the client’s timeout need an idempotency key on the order. Store the key with the outcome. A retry returns the same tickets and does not pin a second set of seats. If the process dies after the payment provider has accepted the charge and before the seats flip to sold, record the recovery point and finish the flip on the way back in. Do not charge again.

When a hold fails because the event is actually exhausted, stop. A client that immediately retries is a retry storm aimed at a row that will keep returning zero. Show “none left right now.” Expired holds return seats to the pool, and the line can admit someone to try again. The page should say that holds expire, or the line thinks you are lying.

Tradeoffs

Choice What you gain What you spend
Waiting room with a capped length The primary sees 300 commits/s, not 5,000 arrivals/s Fans wait, and a crash can fuzz their position
Reject at the edge once the line is full The line process stays up Some fans never see a queue position
One row per reserved seat Holds on different seats run in parallel The best seats still collide, and the map has more rows
One counter for general admission One invariant, easy to point at Every buyer of that event serializes on one row
Seat map cached for a second The picture loads under the rush A fan can tap a seat that was just held, then get zero rows
Hold of 8 minutes A human can enter a card Seats sit idle while the line watches
CDN for the brochure, primary for the hold Quiet traffic never touches inventory You must refuse to cache “seats left” as a writable value

Failure modes

Dependency down. The payment provider times out. The hold stays a hold until it expires or the fan retries with the same key. You do not mark the seat sold. The seat primary is down. Stop admitting. A waiting room that keeps handing fans onto a dead hold path is a crowd at a locked door, and a cache that keeps selling is an oversell. The CDN can keep showing the brochure.

Retry storm. Lost responses, a mobile client with a short timeout, and a sold-out event. Idempotency keys collapse duplicate confirms. A hard stop when the conditional update matches zero rows collapses the rest. Retries need backoff. They also need a budget: another attempt that will not fit in the fan’s patience is just load.

Hot partition. The event is the hot key on purpose. All of its seats are on one primary so the invariant is local. The line is how that primary stays inside 400 commits a second. Adding databases and scattering seats without a per-shard inventory will not make the hot key milder. It will make the oversell harder to see. A single famous show is supposed to be hot. Protect it. Do not pretend to shard it away.

Consistency. Two fans, one seat, two sold rows: the conditional update was skipped or was applied to a cached copy. Write skew on general admission is the same bug in insert form. A confirm that ignores the hold id is the expiry race. Stale position in the line is a consistency choice you can make. Stale ownership of a seat is not.

Durability. A sold seat is a committed row on the primary, replicated so a failover does not resell it. A position in line can be lost. A hold can be lost. A captured payment that has no sold row yet has to be recoverable from the idempotency record, which means that record commits in the same database as the seats before you call the charge complete. The dual-write to a second system is how you lose the recovery point.

In the interview

Spend the first five minutes refusing the steady state. “Thirty-five page views a second, cache it. The design is 5,000 arrivals a second against a primary I am willing to give 400 commits a second, so there is a line.” Non-goals are resale and pricing. The bottleneck is the seat primary for one event. The tradeoff is a wait and an approximate position, in exchange for a hard inventory row.

Draw the edge, the line, the conditional hold, the expiry, and the payment outside the transaction. Skip the seat-map rendering and the recommendation boxes.

The question that usually follows is “how do you not oversell?” Walk the predicate and the cached-counter failure. The one after that is “what happens to a hold if they never pay, and what if they pay as it expires?” Expiry, the hold id, the extension, then the charge.

Check yourself

The quiet site does about 35 page views a second and the on-sale door sees 5,000 arrivals a second. Which number is the design for, and what did you assume?

The on-sale door. The 35 a second is 3 million daily page views divided by 86,400, and a cache can serve it. The 5,000 a second assumes half of 600,000 fans arrive in the first minute. The primary was assumed to commit about 400 inventory updates a second, so the line admits about 300.

Why can the waiting-room position be approximate when the seat row cannot?

A skipped position changes how long someone waits and can be repaired by admitting the next fan. A duplicated seat sells one chair twice, and the fan finds out at the venue. Positions can live in a counter or in one process’s memory. Seat ownership is a conditional update on the primary.

Forty app servers each cache a copied value of 100 seats left and sell from it. What is wrong, and where is the write?

Those caches are forty copies of the same remainder, not forty disjoint blocks of seats. Each server will sell seats the others are selling. The hold has to be a conditional update on one primary row per seat, or on one counter row for general admission. The cache may serve the poster and a slightly stale map. It does not get to decrement.

A hold lasts 8 minutes and you admit 300 fans a second. Why is a second cap still required?

300 times 480 seconds is 144,000 fans inside checkout, and the arena only has 15,000 seats. The admit rate protects the database. A cap near twice the remaining seats protects the pool from being pinned by people who have not paid, and it keeps the line moving when holds expire.

Payment succeeds in the same second the sweeper frees the hold. Which statement should have run first, and what key do you store?

An extension of the hold, matching the hold id, has to succeed before any charge. If it matches zero rows, you do not charge. Confirm and sweeper use disjoint predicates on the database clock so only one of them flips the row. The order stores an idempotency key and the recovery point, so a retry after a successful charge finishes the flip and does not charge again.

Based on: Alex Xu, ByteByteGo volumes; Google SRE book and SRE workbook; Designing Data-Intensive Applications, Kleppmann and Riccomini.