Ticket Booking System Design (HLD & LLD)
Overview
Section titled “Overview”A ticket booking system (such as BookMyShow, airline, or train reservation systems) must support high concurrency while ensuring that the same seat is never booked twice. This document covers the functional requirements, architecture, database schema, concurrency control, APIs, scaling strategies, and implementation examples.
Functional Requirements
Section titled “Functional Requirements”Core Features
Section titled “Core Features”- Search events, movies, flights, or trains
- View seat availability
- Select seats
- Lock seats temporarily
- Process payment
- Confirm booking
- Cancel booking
- View booking history
- Send notifications (email/SMS)
Admin Features
Section titled “Admin Features”- Add events/shows
- Manage theaters/flights/trains
- Manage pricing
Non-Functional Requirements
Section titled “Non-Functional Requirements”| Requirement | Explanation |
|---|---|
| High availability | Booking should not fail |
| Strong consistency | Prevent double booking |
| High concurrency | Thousands of users booking simultaneously |
| Low latency | Fast seat selection |
| Fault tolerance | Handle payment failures gracefully |
| Scalability | Support millions of users |
Capacity Estimation
Section titled “Capacity Estimation”Assumptions:
- 10 million users/day
- Peak traffic: 5,000 requests/sec
- Bookings: 500/sec
- 100-500 seats per show
The primary technical challenge is preventing multiple users from booking the same seat.
High-Level Design
Section titled “High-Level Design”Core Services
Section titled “Core Services”- API Gateway
- User Service
- Search Service
- Booking Service
- Seat Inventory Service
- Payment Service
- Notification Service
- Show/Event Service
Architecture
Section titled “Architecture”flowchart TD
U[Users] --> G[API Gateway]
G --> US[User Service]
G --> SS[Search Service]
G --> BS[Booking Service]
G --> PS[Payment Service]
G --> NS[Notification Service]
BS --> SI[Seat Inventory Service]
SI --> DB[(Database)]
SI --> R[(Redis Cache)]
Search Service
Section titled “Search Service”Typically implemented using:
- Elasticsearch
- Redis
Example query:
Search MoviesLocation = BangaloreDate = TodaySeat Inventory Service
Section titled “Seat Inventory Service”Responsible for preventing double booking.
Possible approaches:
- Pessimistic locking
- Optimistic locking
- Distributed locking
- Queue-based booking
A common production approach is Seat Locking + TTL.
Booking Flow
Section titled “Booking Flow”sequenceDiagram
participant User
participant Booking
participant Inventory
participant Payment
participant DB
User->>Booking: Select seats
Booking->>Inventory: Lock seats
Inventory-->>Booking: Locked (TTL = 5 min)
Booking->>Payment: Process payment
alt Payment Success
Payment->>Booking: Success
Booking->>DB: Save booking
Booking->>Inventory: Mark seats BOOKED
else Payment Failed
Payment->>Booking: Failure
Booking->>Inventory: Release seats
end
Seat States
Section titled “Seat States”AVAILABLELOCKEDBOOKEDLocks are typically stored in Redis with a 5-minute TTL.
Database Design
Section titled “Database Design”User-----user_idnameemailphoneEvent------event_idnamelanguagedurationTheater
Section titled “Theater”Theater-------theater_idnamelocationShow----show_idevent_idtheater_idstart_timeend_timeSeat----seat_idshow_idseat_numbertypepricestatusSeat status:
AVAILABLELOCKEDBOOKEDBooking
Section titled “Booking”Booking-------booking_iduser_idshow_idstatustotal_pricecreated_atBooking status:
PENDINGCONFIRMEDFAILEDCANCELLEDBookingSeat
Section titled “BookingSeat”BookingSeat-----------idbooking_idseat_idpriceSeatLock
Section titled “SeatLock”SeatLock--------seat_iduser_idlock_expiryStored in Redis.
Example:
Seat A1 locked by user123Expiry = now + 5 minER Diagram
Section titled “ER Diagram”erDiagram
USER ||--o{ BOOKING : places
EVENT ||--o{ SHOW : has
THEATER ||--o{ SHOW : hosts
SHOW ||--o{ SEAT : contains
BOOKING ||--o{ BOOKING_SEAT : includes
SEAT ||--o{ BOOKING_SEAT : booked_as
Concurrency Handling
Section titled “Concurrency Handling”Two users may attempt to book the same seat simultaneously.
Optimistic Locking
Section titled “Optimistic Locking”Add a version column.
Seat----seat_idstatusversionSQL update:
UPDATE seatSET status = 'BOOKED'WHERE seat_id = 'A1' AND version = 2;Redis Distributed Lock
Section titled “Redis Distributed Lock”SETNX seat:A1 lockQueue-Based Booking
Section titled “Queue-Based Booking”Use Kafka or RabbitMQ to serialize booking requests.
Booking Service (Spring Boot)
Section titled “Booking Service (Spring Boot)”Controller
Section titled “Controller”@PostMapping("/book")public BookingResponse bookSeats(@RequestBody BookingRequest request) { return bookingService.book(request);}Service
Section titled “Service”public BookingResponse book(BookingRequest request) {
seatService.lockSeats(request.getSeatIds());
PaymentResponse payment = paymentService.processPayment();
if (payment.isSuccess()) { bookingRepository.save(); seatService.confirmSeats(); } else { seatService.releaseSeats(); }}Seat Lock Expiry
Section titled “Seat Lock Expiry”Redis example:
seat:A1 -> user123TTL = 5 minutesExpired locks automatically transition seats back to AVAILABLE.
Preventing Overselling
Section titled “Preventing Overselling”Common strategies:
- Atomic database updates
- Distributed locking
- Single-writer principle
- Queue-based processing
Typical production stack:
Redis + Queue + DatabaseScaling Strategy
Section titled “Scaling Strategy”Read-heavy Workloads
Section titled “Read-heavy Workloads”- Read replicas
- Redis cache
- CDN
Write-heavy Workloads
Section titled “Write-heavy Workloads”Shard by:
- show_id
- event_id
- theater_id
Example:
Show 1 -> DB1Show 2 -> DB2Caching
Section titled “Caching”Cache:
- Seat availability
- Show data
- Search results
Technology:
RedisFailure Handling
Section titled “Failure Handling”Payment Failure
Section titled “Payment Failure”Release locked seats.
Service Failure
Section titled “Service Failure”Use:
- Retry
- Circuit Breaker
- Dead Letter Queue
Technologies:
- Resilience4j
- Kafka
Observability
Section titled “Observability”Monitoring:
PrometheusGrafanaTracing:
Micrometer TracingZipkinAPI Design
Section titled “API Design”Search
Section titled “Search”GET /events?city=bangaloreGet Seats
Section titled “Get Seats”GET /shows/{showId}/seatsLock Seats
Section titled “Lock Seats”POST /seats/lockPOST /bookingAdvanced Enhancements
Section titled “Advanced Enhancements”Waiting Queue
Section titled “Waiting Queue”Users can join a waiting queue if seats are sold out.
Dynamic Pricing
Section titled “Dynamic Pricing”Pricing can vary based on:
- Demand
- Time
- Popularity
Anti-Bot Protection
Section titled “Anti-Bot Protection”Use:
- Rate limiting
- CAPTCHA
- Queue systems
Typical Technology Stack
Section titled “Typical Technology Stack”| Component | Technology |
|---|---|
| API Gateway | Kong / Nginx |
| Backend | Java Spring Boot |
| Search | Elasticsearch |
| Cache | Redis |
| Messaging | Kafka |
| Database | MySQL / Cassandra |
| Monitoring | Prometheus |
Interview Strategy
Section titled “Interview Strategy”A clear explanation order:
- Requirements
- High-Level Design
- Seat Locking
- Concurrency Handling
- Database Design
- Scaling Strategy
- Failure Handling
Seat locking and concurrency control are the most critical interview topics.
Summary
Section titled “Summary”- Strong consistency is essential to prevent double booking.
- Redis-based seat locks with TTL provide fast, temporary reservation handling.
- Queue-based processing improves correctness under heavy contention.
- Scale reads with caching and replicas; scale writes using sharding.
- Build resiliency using retries, circuit breakers, and asynchronous messaging.