Loading...

A 20-minute mock system design interview. You drive the pace. Jump to the board when you are ready; skip capacity math if you want. The interviewer grades your thinking, not a template match.
This question looks like CRUD and is not. The inventory is finite and unique: seat G14 for the 7 pm show exists exactly once. The signal concentrates in three places: how you guarantee a seat is never sold twice, how a 5-minute hold expires safely, and whether you see that a hot on-sale is an admission problem, not a database-throughput problem. "Wrap it in a transaction" is not a design.
Two shared surfaces (only one is on screen at a time; use the panel arrow to switch):
Build incrementally while you talk. Trace one booking end to end through your diagram, from seat map to confirmed ticket, including the user who loses the race. Argue the tradeoff, not "industry standard."
Read this before you go in. These are the decisions, and why they matter.
The naive flow is read seat status → check it is free → write HELD. Two users click seat 55 in the same millisecond: both read "free", both write, both get charged. The check and the write must be one atomic step on the seat rows.
Grokking's answer is a SERIALIZABLE transaction: select the requested Show_Seat rows that are free, confirm the count, update seats and booking, commit. It is correct, but a serializable read takes shared locks, so two concurrent bookers usually deadlock or abort and retry, exactly when traffic peaks. The sharper default is a conditional update: UPDATE Show_Seat SET status=HELD, hold_id, expires_at WHERE show_id=99 AND seat_id IN (54,55,56) AND (status=AVAILABLE OR expires_at < now()), then check the affected row count; fewer than requested → roll back, return 409. Row locks last microseconds and the default isolation level is enough. SELECT … FOR UPDATE with seat IDs sorted (to avoid deadlocks) is equally defensible. A unique constraint on (show_id, seat_id) in the tickets table is the last line of defence.
A Redis lock (SET seat:99:55 NX PX 300000) is fast and gives expiry for free, but it is a second source of truth. Use it as a filter in front of the database if you like; the database still enforces the commit.
A hold lasts 5 minutes. A database transaction must not. So the hold is state on the seat row (HELD, hold_id, expires_at), not an open transaction.
Grokking tracks live holds in memory in an ActiveReservationsService: per show, a linked hash map from booking ID to creation time, head = oldest, so expiry pops from the head and completion removes in O(1). The durable copy is the Booking row with Status = Reserved. It adds a 5-second server-side buffer so the client's countdown always ends first.
The stronger refinement is lazy expiry: put expires_at in the hold predicate so an expired hold is free the instant it expires, even if the sweeper is late or dead. The sweeper then only tidies up and wakes waiting users. Also know the late-payment case: payment succeeds 20 seconds after the hold expired. Confirm must be conditional too (seats become BOOKED only where still HELD by this hold_id); otherwise rebook if still free, or refund.
Grokking's averages: 3 B page views and 10 M tickets a month is about 4 tickets per second. The real problem is the on-sale: BookMyShow's Coldplay sale had about 13 M people queued for about 150 K tickets. Even selling out in 30 minutes is only ~83 tickets per second. The booking database is not the bottleneck; arrivals are.
So put a virtual waiting room in front of booking. The waiting page is static on a CDN. An admission controller lets users through at a rate the booking path can handle (keep a few thousand active shoppers per show) and hands each a signed admission token that the booking service verifies locally. "Autoscale everything" is the weak answer: you would be scaling to serve millions of seat-map refreshes for seats that do not exist.
Grokking serves waiting users strictly first come, first served: a per-show WaitingUsersService (linked hash map, head = longest waiting) notified over long polling when holds expire. That is right for "all seats held, some may free up."
For a pre-sale crowd, strict FIFO rewards whoever's packet lands first at 12:00:00, which means bots. Ticketmaster's queue gives everyone who arrives before the start a random position, then appends later arrivals in order. Speed buys nothing. Pair it with login, per-account limits enforced at hold time across all of that account's holds, and rate limits at the gateway. Either policy is defensible if you can say who it favours.
Payment is an external, slow, sometimes ambiguous call. Double-clicks and retries must not charge twice. Send an idempotency key (use hold_id) to the provider, treat the provider's webhook as the source of truth, and make the confirm write conditional on the hold. If the provider goes down mid-sale: stop admitting new shoppers, extend or pause existing holds, resume on recovery. Do not book seats you have not been paid for.
Grokking: partition by ShowID, not MovieID; MovieID piles every show of a blockbuster onto one server. Keep Show_Seat, Booking and the show's holds on the same shard so hold and confirm are single-shard transactions. The hottest show is still one partition, and that is fine: with admission control its write rate is tens per second. What needs protecting is its read path: cache the seat map for 1–2 seconds and push seat changes to viewers instead of letting thousands refresh.
One complete design you can study, then rebuild in your own words. Do not recite it. In the interview, draw your graph and defend the choices.
Show_Seat inventory, not bookings.Rough numbers are fine; the method is what gets graded. The win is using them: "commits are tiny, arrivals are huge, so I build admission control, not a bigger database."
How the graph works: browsing and booking are separate branches. Browsing (search, show lists, seat maps) is read-heavy, cached, and allowed to be a second stale. Booking goes through a waiting room, then a Booking Service that places holds with one atomic statement against Postgres, tracks expiry in Redis, and confirms after the payment provider says yes. Seat changes are pushed back to viewers.
Web Client, Mobile Client
→ CDN (static pages + the waiting-room page)
→ API Gateway (auth, Rate Limiter rules; mark distributed)
→ Service "Search" → Elasticsearch ← Postgres "Catalog" (CDC)
→ Service "Waiting Room" ↔ Redis "Queue" (per-show sorted set) → signed admission token
→ Service "Booking" (mark distributed)
↔ Postgres "Bookings" (Show_Seat, Booking; sharded by ShowID) [atomic hold, conditional confirm]
↔ Redis "Holds" (hold_id → seats, TTL 5 min + 5 s)
→ Third-party API "Payment provider" (idempotency key = hold_id; webhook back)
→ Pub/Sub Topic "seat changes" → WebSocket Server → clients
Worker "Hold sweeper" → Postgres + wakes Waiting Room when seats free
Components:
Show_Seat and Booking. Sharded by ShowID so a show's seats, holds and bookings live together. ACID is the reason it is relational.expires_at is.POST /holds {G14, G15, G16} with the token. The API Gateway checks the rate limit; the Booking Service checks the token and A's seat count for this show.hold_id and expires_at = now + 5 min. Three rows changed: success. The hold is recorded in Redis with a TTL.hold_id. The webhook reports success.A never pays: at expires_at the seats are free again for the next conditional update, whether or not the sweeper has run. The sweeper tidies the rows and tells the Waiting Room seats freed up.
Payment lands after expiry: the confirm update matches zero rows. Rebook in the same transaction if the seats are still free, otherwise refund automatically.
The Waiting Room node dies: the queue lives in replicated Redis and each user holds a signed queue ticket with their position, so a replacement re-admits them in order. Grokking's in-memory WaitingUsersService loses the queue here unless it has a secondary; say so.
Show(show_id, movie_id, cinema_hall_id, starts_at)
Show_Seat(show_id, seat_id, status [AVAILABLE|HELD|BOOKED], hold_id, expires_at, booking_id, price)
Booking(booking_id, user_id, show_id, num_seats, status [RESERVED|BOOKED|EXPIRED], created_at)
Payment(payment_id, booking_id, amount, provider_txn_id, idempotency_key, status)
Hold (Redis): hold:{hold_id} → {show_id, seat_ids, user_id} TTL 305 s
Queue (Redis): queue:{show_id} → sorted set of user_id scored by position
(show_id, seat_id) on Show_Seat; unique (show_id, seat_id) on issued ticketsShow_Seat, Booking and holds by show_id; replicate each shard primary–secondarySELECT … FOR UPDATE or optimistic versioning instead of the conditional update. Grokking's SERIALIZABLE transaction, if you name its abort cost. Grokking's in-memory ActiveReservationsService and WaitingUsersService with consistent hashing on ShowID, if you give them a recovery story. Redis locks with TTL in front of the database. Long polling or SSE instead of WebSockets. What matters is that you can argue the tradeoff.