0
0
LldHow-ToIntermediate ยท 4 min read

How to Design Movie Ticket Booking System: Architecture & Flow

To design a movie ticket booking system, create components for user management, movie listings, seat selection, and payment processing. Use database transactions to handle seat availability and prevent double booking, and design the system to scale with load balancers and caching.
๐Ÿ“

Syntax

The main components of a movie ticket booking system include:

  • User Service: Handles user registration, login, and profile.
  • Movie Service: Manages movie details, showtimes, and theaters.
  • Booking Service: Allows seat selection and booking with concurrency control.
  • Payment Service: Processes payments securely.
  • Notification Service: Sends booking confirmations.

These services communicate via APIs and use a shared database with transactions to ensure seat availability is consistent.

java
class MovieTicketBookingSystem {
  UserService userService;
  MovieService movieService;
  BookingService bookingService;
  PaymentService paymentService;
  NotificationService notificationService;

  void bookTicket(User user, MovieShow show, List<Seat> seats) {
    if (bookingService.reserveSeats(show, seats)) {
      if (paymentService.processPayment(user, seats)) {
        bookingService.confirmBooking(user, show, seats);
        notificationService.sendConfirmation(user);
      } else {
        bookingService.releaseSeats(show, seats);
      }
    } else {
      throw new Error("Seats not available");
    }
  }
}
๐Ÿ’ป

Example

This example shows a simplified booking flow where seat availability is checked and reserved atomically to avoid double booking.

java
class BookingService {
  private Set<String> reservedSeats = new HashSet<>();

  synchronized boolean reserveSeats(List<String> seats) {
    for (String seat : seats) {
      if (reservedSeats.contains(seat)) {
        return false; // seat already reserved
      }
    }
    reservedSeats.addAll(seats);
    return true;
  }

  void releaseSeats(List<String> seats) {
    reservedSeats.removeAll(seats);
  }
}

public class Main {
  public static void main(String[] args) {
    BookingService bookingService = new BookingService();
    List<String> seatsToBook = List.of("A1", "A2");

    boolean success = bookingService.reserveSeats(seatsToBook);
    System.out.println(success ? "Seats reserved successfully" : "Failed to reserve seats");

    // Simulate payment failure
    bookingService.releaseSeats(seatsToBook);
    System.out.println("Seats released after payment failure");
  }
}
Output
Seats reserved successfully Seats released after payment failure
โš ๏ธ

Common Pitfalls

Common mistakes when designing a movie ticket booking system include:

  • Not handling concurrent seat bookings, leading to double booking.
  • Failing to use transactions or locks when reserving seats.
  • Ignoring payment failures and not releasing reserved seats.
  • Not scaling the system to handle high traffic during popular movie releases.

Always use atomic operations or database transactions to lock seats during booking and release them if payment fails.

java
/* Wrong approach: No synchronization, can cause double booking */
class BookingServiceWrong {
  private Set<String> reservedSeats = new HashSet<>();

  boolean reserveSeats(List<String> seats) {
    for (String seat : seats) {
      if (reservedSeats.contains(seat)) {
        return false;
      }
    }
    reservedSeats.addAll(seats);
    return true;
  }
}

/* Right approach: synchronized method to prevent race conditions */
class BookingServiceRight {
  private Set<String> reservedSeats = new HashSet<>();

  synchronized boolean reserveSeats(List<String> seats) {
    for (String seat : seats) {
      if (reservedSeats.contains(seat)) {
        return false;
      }
    }
    reservedSeats.addAll(seats);
    return true;
  }
}
๐Ÿ“Š

Quick Reference

Key tips for designing a movie ticket booking system:

  • Use microservices for modularity: separate user, movie, booking, payment, and notification services.
  • Implement atomic seat reservation using database transactions or distributed locks.
  • Handle payment failures by releasing reserved seats promptly.
  • Use caching for movie listings to reduce database load.
  • Design for scalability with load balancers and horizontal scaling.
โœ…

Key Takeaways

Always use atomic operations or transactions to prevent double booking of seats.
Separate system components into services for better scalability and maintenance.
Handle payment failures by releasing reserved seats to avoid blocking availability.
Cache static data like movie listings to improve performance.
Design the system to scale horizontally to handle high user traffic.