Courses System design · Part 6 · Case study 8
Case Study: Booking Concert Tickets
A famous singer announces a concert: 20,000 seats, sales open Friday at 10:00. At 9:59, half a million people are waiting with their finger on the button. In ten minutes every seat is gone. The system has two jobs that pull in different directions: survive the rush, and never sell one seat twice.
1. Requirements
Functional:
- Show the seat map with free seats; the buyer picks seats.
- Hold the chosen seats for 10 minutes while the buyer pays.
- After payment, the seats are sold and the buyer gets tickets; if the time runs out, the seats are free again.
Non-functional:
- A seat is never sold twice. This matters more than speed.
- Survive a huge rush at the opening minute without falling over.
- Be fair: people who came first should not lose to people who refresh fastest.
2. Numbers
- 20,000 seats, 500,000 people in the first minute. Most of them will not get a seat; the system must still answer them.
- If all 500,000 open the seat map within 60 seconds: about 8,000 requests a second, for a page that changes every second.
- But only 20,000 seat holds can ever succeed. The writes are few; the danger is the crowd of reads and attempts.
3. Data model
seats(event_id, seat_id, status, held_by, hold_until, version)
status: free | held | sold
bookings(id, event_id, seat_id, user_id, payment_id) UNIQUE (event_id, seat_id)
One row per seat. The UNIQUE (event_id, seat_id) constraint on bookings is the last line of defence: whatever goes wrong above it, the database refuses a second booking for the same seat.
4. Deep dive: holding a seat without double booking
This is the last ticket problem with numbered seats. “Read the seat, see it is free, then write held” lets two buyers both win. Hold the seat in one atomic step instead:
UPDATE seats
SET status = 'held', held_by = ?, hold_until = now() + interval '10 minutes'
WHERE event_id = ? AND seat_id = ?
AND (status = 'free' OR (status = 'held' AND hold_until < now()))
If one row changed, the seat is yours. If none changed, someone else has it: show “this seat was just taken” and let the buyer pick another. Notice that an expired hold counts as free right in the condition, so no job has to release seats on time for the sale to stay correct. (A cleanup job can still tidy the rows every minute, for the seat map.)
Buying several seats together: hold them in one transaction, in a fixed order (by seat ID), so two buyers wanting overlapping seats cannot deadlock.
5. Deep dive: the rush
Letting 500,000 browsers hit the seat service at once only produces errors. Common tools:
- A waiting room. At 10:00 everyone enters a queue and gets a place number. The site lets people in at a steady rate, say 2,000 a minute, with a short-lived token. This is a rate limiter in front of the whole sale, and backpressure made visible: “you are number 41,302, about 20 minutes”. It is also fair: first come, first in.
- A cached seat map. The map can be a second or two old; it comes from a cache or a CDN. That is safe, because the hold itself is checked by the database. A stale map only means some people try a seat that was just taken.
- Retries with jitter. When the site says “try again”, the app waits a random, growing time, not “every second” (retries). Otherwise the retry wave knocks the service over again.
6. Deep dive: paying
- The buyer pays through the bank gateway while the seats are held (the payment flow case study). The payment carries an idempotency key, so a double click does not pay twice.
- When payment is confirmed, in one transaction: set the seats to
soldonly if they are still held by this buyer, and insert the bookings. Tickets and an email go out through a queue. - The hard case: the payment succeeds, but the hold expired a second earlier and someone else took the seat. The condition fails, so the seat is not sold twice; instead the payment is refunded automatically and the buyer is told. Make it rare with a hold long enough for slow banks, and by extending the hold while the buyer is on the bank page.
7. Trade-offs
- Waiting room or open doors: a queue is slower for everyone at first, but nothing falls over and it is fair.
- Long or short holds: long holds are kind to slow payers but lock seats away from others; abandoned holds make a sold-out event look full while seats are still free.
- Exact or fast seat map: a live map for every viewer is expensive; a slightly old map is cheap, and the database check keeps it safe.
Labs used
Last ticket · Rate limiter · Backpressure · Cache · Retries · Message queue
If a topic is missing or a lesson was unclear, write it in the comments on the course page.