Design a ticket booking system where 5M users attempt to book 50K concert seats simultaneously, guaranteeing no double booking, fair queue ordering with position tracking, distributed seat locking with expiry, and bot detection.
Core challenge: 100:1 demand-to-supply ratio. 5M users hit "Buy" within seconds of sale opening. You must serialize access to 50K seats without losing fairness, crashing under load, or allowing double-booking.
Redis DECR returns new value · if <0, reject immediately
Locking strategy: Two-phase: Redis lock (fast, distributed, TTL-based) for real-time seat reservation + DB constraint (UNIQUE on seat_id+event_id WHERE status='BOOKED') as final safety net. If Redis and DB disagree, DB wins.
Queue fairness: Users get a queue position (Redis ZRANK) and estimated wait time (position · avg_processing_time). Position updates pushed via SSE every 5s. Users who abandon queue ? position freed for next in line.
Anti-patterns:No queue (direct DB hit) · 5M concurrent connections crash the DB. Infinite lock TTL · abandoned carts block seats forever. Optimistic-only (no lock) · high contention = most users get "seat taken" error. No idempotency · payment retries = double-charge.
Real-world:BookMyShow · Redis queue + seat locking for IPL/concerts. Ticketmaster · virtual waiting room (Queue-it). Amazon · flash sales use atomic inventory decrement. Airbnb · calendar locking with optimistic concurrency for date conflicts.
Interview Cheat Sheet
The 8 things to say for ticket booking design
1.Virtual queue (Redis sorted set) · absorb 5M users, process in order, show position 2.Distributed seat lock (Redis SETNX + TTL) · hold seat for 5 min during checkout 3.DB UNIQUE constraint as safety net · even if Redis fails, DB prevents double-booking 4.Idempotent payment · retry-safe charge with idempotency key (no double-charge) 5.Lock TTL expiry · abandoned carts auto-release seats (no manual cleanup) 6.Bot detection · CAPTCHA at queue entry, device fingerprinting, velocity checks 7.Real-time seat map (WebSocket) · push availability changes to all viewers instantly 8.Graceful degradation · if queue overflows, show "sold out" early rather than crash