Design a Train Ticket Booking System (IRCTC-like)

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-02-03

Problem Statement

Design a low-level architecture for a train ticket booking system that allows users to search trains, check seat availability, book tickets, cancel bookings, and manage payments. The system should handle high concurrency, prevent overbooking, and ensure consistency in seat allocation.


Functional Requirements (FR)


User Management


•Users can register, login, and manage profiles.
•Maintain booking history and preferences.


Train & Schedule Management


•Admin can add/update trains, schedules, stations, and seat availability.
•Trains have different classes (Sleeper, AC, General, etc.) and quotas (Tatkal, General, Ladies).


Seat Availability & Booking


•Users can search trains between two stations on a given date.
•Check real-time seat availability per class and quota.
•Lock seats temporarily during booking to prevent double booking.
•Confirm booking after successful payment.


Ticket Cancellation & Refunds


•Users can cancel tickets before departure.
•Refund processed according to rules and time of cancellation.


Payment Handling


•Integrate multiple payment methods (credit/debit cards, wallets, UPI).
•Ensure secure transaction and confirm booking after payment.


Notifications


•Notify users about booking confirmation, cancellation, or reminders.


PNR Generation


•Generate unique PNR for every confirmed ticket.


Non-Functional Requirements (NFR)


•Concurrency Handling → Prevent overbooking during high-demand scenarios.
•Scalability → Handle millions of users and multiple train schedules concurrently.
•Low Latency → Fast search and seat availability checking.
•High Availability → Ensure uptime during peak booking hours.
•Security → Protect user data and payment details.


Core Classes & Entities


User


•userId, name, email, phone, bookingHistory


Train


•trainId, name, source, destination, schedules, classes


Schedule


•scheduleId, trainId, date, availableSeatsByClass, departureTime, arrivalTime


Seat


•seatId, classType, status (available/booked/locked), quota


Booking / Ticket


•bookingId, userId, scheduleId, seatIds, status, PNR, paymentId


Payment


•paymentId, bookingId, amount, status, method


Notification


•notificationId, userId, message, timestamp


Key Methods / Operations


User Service


•registerUser(), loginUser(), getBookingHistory(userId)


Train & Schedule Service


•addTrain(), addSchedule(), getTrainsBetweenStations(source, dest, date)
•checkSeatAvailability(scheduleId, classType, quota)


Booking Service


•lockSeats(scheduleId, seatIds) → temporary lock
•confirmBooking(userId, scheduleId, seatIds) → finalize after payment
•cancelBooking(bookingId) → release seats


Payment Service


•processPayment(bookingId, amount, method)
•verifyPayment(paymentId)


Notification Service


•sendNotification(userId, message)


Design Considerations


Concurrency & Seat Locking


•Use in-memory or distributed locks to prevent double booking, especially during peak hours.


Atomic Transactions


•Ensure payment success → seat confirmation → booking status update.


Data Storage


•Relational DB for transactional consistency (users, bookings, schedules, seats).
•Cache for fast seat availability checks.


Extensibility


•Support dynamic pricing (Tatkal), multiple quotas, waitlist, and future train expansions.


Fault Tolerance


•Handle payment failures, system crashes, and ensure idempotency in booking requests.


Example Flow


1.User searches for trains → Schedule Service returns available trains and seat availability.
2.User selects train and seats → Booking Service locks selected seats temporarily.
3.User makes payment → Payment Service verifies transaction.
4.On payment success → Booking Service confirms booking → PNR generated → Update seat status.
5.Send booking confirmation → Notification Service.


Solution

Comments