Interview Study Guide

Guide / System design Most asked

Design: ticket booking (BookMyShow)

Seat selection with temporary holds, never double-booking, the hot-show contention problem, virtual waiting rooms, payment timeouts and releasing expired holds.

Created · Updated · 5 min read

The core problem: many users want the same few seats at the same instant. Correctness (no double-booking) comes first, then fairness and load.

Fundamentals (the design)#

1. Requirements#

Functional: browse movies/shows, view seat map, select seats, hold them temporarily, pay, receive tickets, cancel/refund. Non-functional: a seat is never sold twice; holds expire (typically 5–10 min); handle bursts when a blockbuster opens (100K users for 500 seats); browsing stays available even if booking is overloaded.

2. Estimates#

10M DAU browsing, 500K bookings/day (6/s average) but a hot release can generate 50K requests/s on one show. The average is trivial; the hot show is the problem. Seat map reads are cacheable; seat state writes are tiny but contended.

3. Data model#

shows(show_id, movie_id, venue_id, start_time), show_seats(show_id, seat_id, status, held_by, held_until, booking_id) with status AVAILABLE | HELD | BOOKED, primary key (show_id, seat_id); bookings(booking_id, user_id, show_id, seats[], status, amount, idempotency_key); payments. Shard by show_id so one show’s contention is isolated to one shard.

4. The hold-then-confirm flow#

  1. User selects seats → POST /shows/{id}/holds {seats: [A1, A2]}.
  2. Hold atomically in one statement per seat set:
UPDATE show_seats
SET status='HELD', held_by=:user, held_until=now()+interval '8 minutes'
WHERE show_id=:s AND seat_id = ANY(:seats)
  AND (status='AVAILABLE' OR (status='HELD' AND held_until < now()));
-- affected rows must equal the number of requested seats, else ROLLBACK → "seats no longer available"
  1. Return a hold_id and expiry. The UI shows a countdown.
  2. POST /bookings with hold_id and Idempotency-Key → create payment; on success: UPDATE ... SET status='BOOKED' WHERE held_by=:user AND held_until > now(). If the hold expired during payment, refund automatically or re-hold if still free.
  3. A background sweeper (or just lazy check on read as above) releases expired holds; emit events so the seat map refreshes.

Alternatives: Redis SET seat:{show}:{seat} user NX EX 480 for fast holds with a DB write on confirm (then DB unique constraint (show_id, seat_id) on booked_seats is the final guard). Redis is an accelerator; the database constraint is the source of truth (Idempotency).

5. Seat map and real-time updates#

Cache the static layout (CDN) and the dynamic availability bitmap per show in Redis (updated on holds/bookings). Push changes through WebSocket/SSE to viewers, or poll every few seconds (stale is acceptable because the hold step is authoritative). Show “selected by someone else” on conflict with a friendly retry.

6. Handling a flash crowd#

  • Virtual waiting room / queue: users enter a queue (Redis sorted set by arrival or a randomised lottery for fairness), admitted in batches sized to booking capacity; the booking service only sees admitted users. Issue signed admission tokens.
  • Rate limit per user/IP, CAPTCHA/bot protection (scalpers), limit seats per booking (e.g. 6).
  • Static content on CDN; seat map served from cache; booking path isolated on its own autoscaled service and DB shard so browsing never starves it.
  • Multi-seat holds are all-or-nothing, in a deterministic order (sort seat IDs) to avoid deadlocks.

7. Payment and failure cases#

Payment fails/times out → hold expires and releases; payment succeeds after hold expiry → refund or re-confirm if still free (state machine decides). Idempotency key prevents double booking on retries. Use the outbox to emit BookingConfirmed → ticket generation, notification, analytics. Gateway timeout → unknown state, reconcile (Payment system).

Most asked questions#

1. How do you prevent double booking? Atomic conditional update of seat status (or a unique constraint on booked seats) inside a transaction; never check-then-write in app code.

2. How do you implement seat holds? HELD status with held_until, released lazily or by a sweeper; Redis SET NX EX as a fast layer with DB as truth.

3. How do you handle a surge on a hot show? Waiting room, rate limiting, caching, isolate booking capacity, shard by show.

4. What if payment takes longer than the hold? Extend holds while payment is in flight (bounded), or refund/re-confirm per state machine.

5. How do you show real-time availability? Cached bitmap + push updates; eventual consistency is fine because holds are authoritative.

Senior-level questions#

1. “Two users select overlapping seat sets simultaneously and deadlock.” Acquire seat rows in sorted order, use a single set-based UPDATE, set lock timeouts, retry on deadlock.

2. “Redis holds say seat free but DB says booked.” Treat DB as truth; on confirm the unique constraint rejects; reconcile by rebuilding Redis from DB; use expirations to self-heal.

3. “How do you make the waiting room fair and bot-resistant?” Randomised admission within an arrival window, signed single-use tokens, per-account limits, device/behaviour checks, and verified accounts for high-demand events.

4. “Design cancellation and refunds with seat re-release.” Cancellation transitions BOOKED → AVAILABLE only after refund initiation succeeds and per policy windows; idempotent; emit events; handle partial group cancellation.

5. “The database for one show is a bottleneck.” Per-show sharding and in-memory seat state in a single-writer actor per show (queue serialises operations), with the DB persisting the result; scale by many shows, not by a single show’s throughput.

Quiz#

1. How can you detect that a multi-seat hold partly failed?

Compare the affected row count with the number of requested seats; if fewer, roll back the whole transaction.

2. Why shard by show_id?

All contention for a show is local to one shard, and seat updates for different shows scale independently.

3. What is the final guard against double-booking?

A database unique constraint or conditional update on (show_id, seat_id).

4. Purpose of a virtual waiting room?

Admit users at a controlled rate so the booking system is not overloaded and access is fair.

5. How are expired holds released?

Lazily (treat expired holds as available on the next update) and by a background sweeper that publishes release events.

Finished this page?Mark it done to track your progress across the guide.
Pritam Ghosh · System design & AI engineering study notes