Designing Movie Ticket Booking
Difficulty: Intermediate Patterns: State, Strategy, Repository Asked at: BookMyShow, Amazon, Ticketmaster, PhonePe
Browse shows, open a seat map, tap two seats, pay, collect tickets. The scope fits on a napkin, which is exactly why interviewers reach for it: almost the entire problem is one field on one class, and most candidates get that field wrong in the first thirty seconds.
The field is isBooked. A boolean says a seat is either free or sold, and a seat in a real booking flow spends its most interesting minutes in neither of those. Someone has selected it and is on the payment page right now, card details half typed, and they will lose it in five minutes if they stall. Squeeze that into a boolean and you get to choose which bug you would like. Flip isBooked on seat-select, and an abandoned checkout – the user closed the tab – removes that seat from the show permanently, because nothing is ever coming back to unset it. Flip it on payment-success instead, and two users are both shown the seat as free, both pay, and one of them gets a refund and an apology, which is the precise outcome the system exists to prevent. The fix is to stop treating the middle as an edge case and give it a name: a seat is AVAILABLE, HELD or BOOKED, and a HELD seat carries who holds it and the instant that claim dies.
Naming the third state turns one hard problem into two real ones. The first is that exactly one of N concurrent requests for the same seat must win, and that is not something you can check and then do, because the answer to the check goes stale in the gap between reading and writing. The second is that a hold has to expire by itself, without the process keeping a timer object per in-flight checkout, and without a restart quietly handing out a seat somebody is still paying for. Everything else – pricing by seat class, the payment gateway, the seat map a client renders – is ordinary code hanging off those two decisions.
💡 Hold, also called a soft lock or a reservation = a short, expiring claim on something you have not paid for yet. The seat is yours for a few minutes and nobody else’s, and if you do nothing it quietly stops being yours.
💡 Compare-and-set, usually shortened to CAS = one indivisible operation that says “if this value is still exactly what I last read, replace it, otherwise tell me what it actually is now”. It is how you perform check-then-act without taking a lock. Learn more →
💡 Race condition = two threads interleaving so the result depends on which one got there first. Two bookers both reading a seat as AVAILABLE and both writing HELD is the race this design is built around. Learn more →
💡 Idempotent = safe to apply twice. A payment gateway that never hears back from you will retry its callback, so “confirm this booking” has to be an operation whose second application changes nothing. Learn more →
Functional Requirements
- Browse shows – list the shows for a movie, each with its screen, start time and seat layout.
- Seat map – return every seat with its current state, so a client can gray out what it cannot have.
- Seat hold – selecting seats takes a short, expiring claim on all of the requested seats or on none of them.
- Payment – charge through an injected gateway, then confirm the booking on success and release the seats on failure.
- Hold expiry – an abandoned checkout returns its seats automatically, with no human intervention and no timer per hold.
- Idempotent confirmation – a payment callback delivered twice confirms the booking once and books no extra seat.
- Pricing – the amount owed is derived from the seats’ classes on the server, never sent by the client.
Non-Functional Requirements
- Exactly one winner per seat – at most one live hold and at most one confirmed booking can exist for a seat. N concurrent requests for the same seat produce one success and N-1 clean
SeatUnavailableExceptionrejections, never a silent overwrite. - Contention is per seat, not per show – taking a seat is O(1) and touches a single map entry, so a sold-out blockbuster does not serialise behind one lock. Reading a seat map is O(S) in the seats of that one show.
- No lock is held across I/O – the payment call happens with nothing locked and no transaction open, because a gateway round trip is orders of magnitude longer than every in-process operation here combined.
- Expiry costs no thread per hold – reclaiming is one scheduled sweep plus lazy expiry on the read path, so ten thousand in-flight checkouts create ten thousand map entries and zero timers.
Discussion
Newest first