Design a Bus Booking System

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-06-24

Problem Statement

Design a system to allow users to search, view, and book seats on available buses between two locations on a selected date. The system should support real-time seat availability, payments, and ticket generation.


🎯 Functional Requirements:

1.User Management
•Sign up / login / logout
•View and manage bookings
2.Bus Listing & Search
•Search buses by source, destination, and date
•View available seats, timings, and fare
•View bus operator info, ratings, amenities
3.Seat Selection & Booking
•Select seat(s) and passenger details
•Lock seats temporarily during booking (to prevent race conditions)
•Confirm booking and generate ticket
4.Payment Integration
•Integrate with payment gateways (Razorpay, Stripe)
•Handle payment status callbacks
5.Ticketing & Booking Management
•Generate e-ticket after successful payment
•View/cancel booking
•Apply refund policy (if allowed)
6.Operator/Agency Panel
•Add/update bus schedules
•Define seat layout
•View bookings and fill rate
7.Admin Panel (Optional)
•Manage users, operators
•View logs and transactions


⚙️ Non-Functional Requirements:

•Scalability to handle high search and booking traffic
•High availability (especially during peak hours)
•Real-time seat locking to avoid overbooking
•Fault tolerance and retry mechanisms (e.g., payment, seat lock)
•Security (data encryption, PCI compliance)


🧱 Key HLD Components:

1.API Gateway
•Rate limiting, authentication, routing
2.User Service
•Authentication, profile, booking history
3.Search Service
•Index buses, locations, timings
•Support filters (AC/Non-AC, sleeper/seater, etc.)
4.Bus Management Service
•Manage bus routes, schedules, seat layouts, operators
5.Seat Inventory Service
•Real-time seat availability tracking
•Seat locking for concurrency control (Redis-based locks)
6.Booking Service
•Create booking request
•Link with inventory and payments
•Generate ticket and update status
7.Payment Service
•Payment gateway integration
•Callback validation and booking confirmation
8.Notification Service
•Send SMS/email with ticket and booking info
9.Database Layer
•SQL (PostgreSQL/MySQL) for bookings, users, buses
•Redis for seat lock mechanism
•Elasticsearch for fast search (optional)


🗂️ Data Models (Simplified):

User

1json 2CopyEdit 3{ 4 "user_id": "uuid", 5 "name": "John Doe", 6 "email": "john@example.com" 7}

Bus

1json 2CopyEdit 3{ 4 "bus_id": "uuid", 5 "operator_id": "uuid", 6 "route": { 7 "source": "Bangalore", 8 "destination": "Hyderabad" 9 }, 10 "departure_time": "2025-07-24T08:00:00Z", 11 "seats": [ "1A", "1B", "2A", "2B", ... ] 12}

Booking

1json 2CopyEdit 3{ 4 "booking_id": "uuid", 5 "user_id": "uuid", 6 "bus_id": "uuid", 7 "selected_seats": ["1A", "2B"], 8 "status": "CONFIRMED", 9 "payment_id": "pid123" 10}


🔁 Booking Flow Sequence:

1.User searches for buses (Search Service)
2.User selects a bus and seats → Seat lock created (Redis)
3.User enters passenger info → Booking created (PENDING)
4.Payment processed → On success:
•Seat lock becomes permanent
•Booking status updated to CONFIRMED
•Ticket generated and sent


🧠 Concurrency Handling:

•Use Redis with TTL to lock seats temporarily
•Use DB transactions to ensure atomic seat assignment
•Periodically clean expired locks


📈 Scalability & Performance:

•Use load balancer and auto-scaling
•Horizontally scale seat inventory service
•Use caching for popular search routes
•Use async queues (Kafka) for non-critical tasks like notifications


Solution

Comments