DSA Tracker

Low-level design

Design a movie ticket booking system

Design the classes for booking seats for a movie show, where two people must never get the same seat.

Good first question for: SDE-1 · Also asked as: Design BookMyShow

Requirements to confirm first

  • Browse movies, theatres and shows in a city
  • See the seat map of a show with free, held and booked seats
  • Select seats, hold them for a few minutes, pay, and receive a booking
  • Release held seats when payment fails or the hold runs out
  • Ask: seat categories and prices? Cancellation? One screen per show?

Write your own class list on paper before you open the answer key. Ten minutes of your own design is the practice; reading the key is only the check.

Show the answer key (write yours first)

Classes and what each one owns

Movie
Title, duration, language
Theatre and Screen
A theatre has screens; a screen has a fixed seat layout
Show
One movie on one screen at one start time
ShowSeat
The status of one seat for one show: free, held or booked
Booking
A user, a show, the seats, the amount and a status
SeatLockService
Holds seats for a user with an expiry
PaymentGateway
Interface to whoever takes the money

How they connect

  • A Seat belongs to a Screen and never changes; a ShowSeat belongs to a Show and carries the status
  • A Booking refers to ShowSeats and becomes confirmed only after payment
  • BookingService depends on the SeatLockService and PaymentGateway interfaces

Design patterns that fit, and why

State
A booking moves through created, pending payment, confirmed, cancelled and expired, and only some moves are allowed.
Strategy
Price depends on seat category, day and time.
Observer
The ticket message goes out after confirmation, outside the booking transaction.

Code sketch in Java

The core types and one real method. It is a sketch to talk through, not a full program.

enum SeatStatus { FREE, HELD, BOOKED }

interface SeatLockService {
  // all or nothing: every seat is held for this user, or none is
  boolean hold(String showId, List<String> seatIds, String userId, Duration ttl);
  void release(String showId, List<String> seatIds, String userId);
}

class BookingService {
  private final SeatLockService locks;
  private final BookingRepository bookings;

  Booking start(User user, Show show, List<String> seatIds) {
    if (!locks.hold(show.id(), seatIds, user.id(), HOLD_TIME)) throw new SeatsTakenException();
    return bookings.createPending(user, show, seatIds);
  }
  void onPaymentResult(Booking booking, boolean paid) { /* confirm, or release the seats */ }
}

Edge cases to raise yourself

  • Two users click the same seat in the same millisecond
  • Payment succeeds after the hold ran out and someone else took the seat
  • A user holds seats and closes the app
  • Partial success: three of four seats could be held

Where answers usually go wrong

  • Checking that the seat is free and then marking it booked as two steps. Another request gets through between them. It must be one conditional update or a lock, and you should say which.
  • A status field on Seat. The same physical seat is free at 6 pm and booked at 9 pm; the status belongs to the seat for one show.
  • Holding seats forever. Every hold needs an expiry and a release path when payment fails.
  • Sending the confirmation message inside the booking transaction, so a slow message provider keeps seats locked.

Follow-up questions

  • Add cancellation with a refund rule
  • A big release opens and thousands of people open one show: what protects the seat map?
  • Add food orders to a booking