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


2️⃣ Core Entities

Keep it simple initially:

  • Event
  • Venue
  • Performer
  • Ticket

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 reserves to available

❌ 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

  1. User Opens Event Page GET /events/101/tickets
  2. Backend Checks DB SQL QUERY
TicketStatus
1available
2booked
3available
  1. Backend Checks Redis Locks ticket:3 = userA (TTL left 7 min) Final Response to User
TicketFinal UI State
1available βœ…
2booked ❌
3reserved ❌

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:
  1. User A β†’ payment success β†’ tries to book
  2. 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

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:

  1. Dual write (DB + ES) ❌
  2. CDC (Change Data Capture) βœ… better
  • 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.