Ticket/Booking System - System Design
CREATED 2021/11/09 19:40
Functional Requirements
Infra
Search Service
Ticket Transaction Service
Payment Service
Dataflows
Search a movie
zip code (converted to geo hash)
Book a ticket
1 Show the map and availability of all the seats in a specific theatre of a cinema
2 Reserve an available seat
3 User has to make the payment in 15 minutes window
4 If success, the booking is completed. Otherwise, the reserved seat is given back to be available again.
Give seat back to available again
1 use a message queue (15 minutes delay) + consumer service (deal with seat status)
2 DB lazy loading. where status = available or (status = pending and current_timestamp - modified_time > 15 mins)
DB Schema
Movies (movieId, name, startDate)
Cinema (cinemaId, name, zipcode, address, cinemaHalls)
CinemaHall (cinemaHallId, id, cinemaId)
Events (movieId, cinemaHallId, cinemaId, startTime, geoHash, ...)
Tickets (ticketId, eventId, row, column, price, status, creationTime, modifyTime) - strong consistency
Transaction (ticketId, userId, status)
Users (userId, name, phone, email, membership)
Sharding
according to geoHash (convert 2 dimensional numbers (Latitude Longtitude) to string format)
Failure
What if payment is successful but the ticket server is down?
We need a Transaction manager (a coordinator) to co-ordinate this. Roll back payment or retry update ticket status.
2-phase commit
Coordinator -> Participant1 (undo log and redo log)
-> Participant2
-> Participant3
Prepare -> -> ->
Vote <- <- <-
Decision -> -> ->
ACK <- <- <-
Distributed SQL - Leader-follower mode replica (Both DB and Cache)
Disaster Recover
stateless service, kafka as distributed log, Transaction coordinate