Case study: Ticket booking (BookMyShow)

Topic 13 of 15 in Week 7: Classic case studies14 min readAdvancedFree course

Overview

Ticketing is a classic test of correctness under contention: many people want the same few seats at the same moment, and selling a seat twice is unacceptable. The design centres on seat holds with expiry, atomic state changes, and protecting the system during massive on-sale spikes.

Try the exercise first: 45 minutes, then compare.

1. Requirements

Functional

  • Browse cities, venues, movies and events, shows and seat maps.
  • Select specific seats, hold them for up to 10 minutes during checkout.
  • Pay and receive tickets; cancel according to policy.

Non-functional

  • Never double-book a seat. Correctness beats availability for the booking step.
  • Fast browsing; booking completes in seconds.
  • Handle extreme spikes (2 million users in 10 minutes for 50,000 seats) gracefully and fairly.

2. Estimates

Text
normal   : 1,000 bookings / min ≈ 17 / s; browsing ~100× more
spike    : 2M users in 10 min ≈ 3,300 arrivals / s, each refreshing seat maps
           but only 50,000 seats → at most a few thousand successful holds per minute
seat data: a venue with 50,000 seats × show → small; contention, not size, is the problem

3. API

Text
GET  /v1/shows/{id}/seats                      → seat map with status (cached, may be slightly stale)
POST /v1/holds  {"show_id", "seat_ids": [...]}  → 201 {hold_id, expires_at} | 409 seats taken
POST /v1/bookings {"hold_id", "payment_method", "idempotency_key"} → 201 booking | 402 payment failed
DELETE /v1/holds/{id}                           → release

4. Data model

Text
shows:    show_id | event_id | venue_id | starts_at
seats:    show_id | seat_id | status (available, held, booked) | hold_id | hold_expires_at | version
          PRIMARY KEY (show_id, seat_id)
holds:    hold_id | user_id | show_id | seat_ids | expires_at | status
bookings: booking_id | hold_id | user_id | show_id | seat_ids | payment_id | status
          UNIQUE (show_id, seat_id) via a booking_seats table

5. High-level design

Ticket booking design. Fans pass through a waiting room that admits a set number of users per second, then reach the booking API; the seat map is served from a CDN and cache. The booking API places seat holds with a 10-minute TTL (1), takes payment idempotently (2), and confirms the booking in the bookings database (3), which enforces unique seats. Seats move from available to held to booked, and a hold that expires returns the seat to available. A hold is a conditional write that succeeds only if the seat is available, and the unique constraint is the final guard against double-selling.

6. Deep dives

Holding seats

When a user selects seats, the booking service holds them atomically:

SQL
UPDATE seats
SET status = 'held', hold_id = :h, hold_expires_at = now() + interval '10 minutes'
WHERE show_id = :s AND seat_id IN (:seats)
  AND (status = 'available' OR (status = 'held' AND hold_expires_at < now()));
-- succeed only if the number of updated rows equals the number of seats requested

Run it in a transaction and roll back if not all seats could be held, so a group of seats is all-or-nothing. Alternatives: Redis SET seat:{show}:{seat} hold NX PX 600000 per seat for speed, backed by the database for the final booking. Either way, holds expire automatically, and a cleanup job releases expired holds for accurate seat maps.

Never double-book

Defence in depth:

  1. The conditional hold above prevents two users holding the same seat.
  2. Converting a hold into a booking checks that the hold is still valid and belongs to the user.
  3. A unique constraint on (show_id, seat_id) in the booking table makes a double booking physically impossible, even if a bug slips through.

Payment and confirmation

  • Payment uses an idempotency key (the hold ID) so retries don’t charge twice.
  • On success, in one transaction: mark seats booked, create the booking, write an outbox event (tickets, email, analytics).
  • On failure or abandonment, release the hold.
  • Late success (the payment succeeds just after the hold expired): if the seats are still free, book them; otherwise refund automatically and apologise. This case must be designed, not discovered.

Surviving the on-sale spike

  • Virtual waiting room: users join a queue before the sale opens and are admitted to the booking flow at a rate the system can handle, with honest position updates. This also improves fairness.
  • Cache seat maps aggressively and accept slight staleness; the hold step is the source of truth.
  • Partition by show: each show’s seats are independent, so contention is local; very hot shows can get dedicated capacity.
  • Rate limits and bot protection (CAPTCHA, purchase limits per account).
  • Pre-scale everything before the announced time.

Movies, events, venues and show times are read-mostly: serve from caches and CDNs, with a search index for discovery.

7. Trade-offs and bottlenecks

  • Hold duration: long holds frustrate other buyers when seats are scarce; short holds rush payers.
  • Strong consistency for seats limits availability under partitions: acceptable, since overselling is worse.
  • Hot shows concentrate writes on one partition: queueing and the waiting room smooth them.

8. Follow-up questions

  • General admission (no specific seats)? Use an inventory counter with atomic decrement instead of per-seat rows.
  • Seat map live updates? Push held/booked changes over WebSockets or poll every few seconds.
  • Resale and transfers? Ticket ownership changes are new events with the same uniqueness guarantees.

Recap

Ticket booking serves browsing from caches, but changes seat state only through atomic, conditional writes in a strongly consistent store. Seats are held with an expiry, confirmed after an idempotent payment, and protected by a unique constraint against double booking. Late payments are handled explicitly, and on-sale spikes are tamed with waiting rooms, rate limits and per-show partitioning.

Exercise

Design it yourself (45 minutes)

Set a 45-minute timer and design a ticket booking platform like BookMyShow: users browse movies and events, pick specific seats from a seat map, hold them while paying (up to 10 minutes), and receive tickets. Normally 1,000 bookings per minute, but a big concert sale brings 2 million users in the first 10 minutes for 50,000 seats. No seat may ever be sold twice. Use the framework, then compare.

Text
1. Clarifying questions:
2. Functional requirements:
3. Non-functional requirements:
4. Estimates:
5. API:
6. Data model:
7. High-level design:
8. Deep dives (seat holds, double booking, payments, spikes):
9. Trade-offs and bottlenecks:

Summary

  • Read-heavy browsing is served from caches and CDNs; seat state changes go to a strongly consistent store.
  • Hold seats with a conditional write and an expiry, so abandoned checkouts release seats automatically.
  • A unique constraint on (show, seat) is the final guarantee against double booking.
  • Confirm bookings only after payment succeeds, idempotently; handle late payments with automatic refunds.
  • Survive on-sale spikes with a virtual waiting room, rate limits and per-show partitioning.