1οΈβ£ Requirements
Functional Requirements
Core features:
π βUsers should be able toβ¦β
- Search for events
- View event details
- Book tickets
Non-Functional Requirements (WHERE MOST PEOPLE FAIL β οΈ)
β Wrong: Scalability, availability, reliability
β Correct (context-specific):
1. Consistency vs Availability
- Booking β Strong Consistency
- No double booking β
- Search/View β High Availability
- Slight delay acceptable
2. Read vs Write Ratio
- Reads >> Writes (β 100:1)
- Many users browse
- Few actually book
3. Traffic Pattern
- Normal traffic β low
- Event drops (Taylor Swift, World Cup) β HUGE spike
π System must handle bursty traffic
4. Low Latency search
2οΈβ£ Core Entities
Keep it simple initially:
EventVenuePerformerTicket
3οΈβ£ APIs Design
Map APIs β Functional Requirements
1. View Event
GET /events/{eventId} β Event & Venue & Performer & Ticket[]
2. Search Events
GET /events/search?term=&location=&date=&type= β Partial<Event> [ ]
3. Booking (2-Phase Process π₯)
POST /booking/reserve
Body: { ticketId }
POST /booking/confirm
Body: { ticketId, paymentDetails }
4οΈβ£ HLD
ticket-master.excalidraw
Seat blocking
Issue:
- User
reservesβ leaves β seat blocked forever β
Solution 1: Timestamp + Query
- Add column
reserved_at+ check > 10 mins β Messy logic
Solution 2: Cron Job
- Runs every 10 mins β release tickets from
reservestoavailable
β Delta Delay issue of 10 min
β Best Solution: Distributed Lock (Redis π₯)
π Goal: Prevent 2 users from reserving same seat at same time.
ticketId β locked (TTL = 10 min)
β Auto-expiry
β Clean design
β Scalable
SET ticket:101 userA NX EX 600
- User Opens Event Page
GET /events/101/tickets - Backend Checks DB
SQL QUERY
| Ticket | Status |
|---|---|
| 1 | available |
| 2 | booked |
| 3 | available |
- Backend Checks Redis Locks
ticket:3 = userA (TTL left 7 min)Final Response to User
| Ticket | Final UI State |
|---|---|
| 1 | available β |
| 2 | booked β |
| 3 | reserved β |
What Actually Happens If Redis Fails?
1. β Immediate impact:
- All active locks are lost
- System forgets reservations
π Result:
- Multiple users may try to book same ticket
- Temporary overbooking attempts
2. Why System Still Doesnβt Break Completely ?
Because: π Final consistency is enforced by database (PostgreSQL)
DB Protection:
- ACID transactions
- Unique constraint on ticket
Example flow:
- User A β payment success β tries to book
- User B β also tries
π DB ensures: Only one transaction succeeds
β One wins
β Others fail
3. User Impact (IMPORTANT β οΈ)
- Users who thought they reserved β will lose it
- Some users:
- Complete payment β get error β
- Bad experience π¬
π This is called:
Consistency preserved, UX degraded
4. How to Handle This (Real Systems)
β Option 1: Redis High Availability (Basic Fix)
- Use Redis Cluster / Sentinel
- Replication + failover
β Reduces failure chance
β Still possible edge cases
β Option 2: Graceful Degradation
When Redis is down:
- Skip reservation step
- Allow direct booking
π DB becomes single source of truth
β System still works
β Higher contention
β Option 3: Retry + Compensation
If payment succeeds but booking fails:
- Refund automatically
- Show message:
βSeat already taken, amount well be refundedβ
- β Its common in real systems
4οΈβ£ Deep Dive
1. Deep Dive 1: Search Optimization - low-latency search
Problem:
SQL LIKE β full DB scan β
Solution:
- Move Search to search optimized database β
Elasticsearch / AWS OpenSearch - It builds inverted index to searching document by terms really quickly
- Fast text search
- Supports:
- text
- location
- filters
Example
Event text:
Taylor Swift Live Concert Mumbai
Elasticsearch tokenizes into:
taylor
swift
live
concert
mumbai
Then maps:
swift -> [event1, event5]
concert -> [event1, event8]
...
So lookup becomes extremely fast.
Data Sync with primery data store:
- Dual write (DB + ES) β
- CDC (Change Data Capture) β better
Popular Query / Hot Query search:
- Cashing - CDN (fastest for common API call) e.g
GET /events/search?query=taylor+swift - AWS Elastic Search has option -
Node Query cashing - Redis cache - betwen search service and elastic search
Strong Interview Answer
Initially Iβd use SQL search for MVP, but for highly popular searches like Taylor Swift, full-table scans wonβt scale. Iβd move search to Elasticsearch/OpenSearch for inverted-index-based retrieval. Since search traffic is highly read-heavy and repetitive, Iβd add CDN and Redis caching to absorb spikes. Search services would scale horizontally behind a load balancer, and API Gateway would enforce rate limiting to protect the system.