Design Ticket Booking

How a ticket booking system avoids selling a seat twice, holds seats while fans pay, handles a spike with a waiting room, and what interviewers ask.

Case study27 minLesson 5 of 8
On this page
  1. Clarify the problem
  2. Estimate the scale
  3. Define the API (optional)
  4. Model the data
  5. High-level design
  6. Deep dives
  7. Bottlenecks and failures
  8. What a strong answer sounds like
  9. Follow-up questions

A ticket booking system lets people browse events, choose seats on a seat map, pay, and receive tickets. Its central rule is that each seat is sold to exactly one buyer, and everyone who pays must end up with a ticket or a refund.

Interviewers ask this question because three hard problems appear together: several buyers can try to take the same seat at once, demand for a popular event arrives in one spike, and a seat must stay reserved for minutes while the buyer pays. The steps below follow A Framework for Any Design.

Clarify the problem

The first five minutes
Interviewer

Design a ticket booking system for concerts and sports.

You

Are the seats assigned, like row C seat 12, or is it general admission with no specific seat?

Interviewer

The seats are assigned, and fans choose them from a seat map.

You

How big is the largest on-sale, in seats and in people arriving at the same time?

Interviewer

The biggest shows have about 50,000 seats and two million people waiting when sales open. The platform sells about 5 million orders a month.

You

Then the normal load is small and the on-sale is the real problem. My priorities are that no seat is sold twice, that we never take money without giving a ticket, and that the site stays up during the spike, even if people have to wait in a line. I will also assume a fan gets about ten minutes to pay after choosing seats.

Functional requirements:

  • Fans can browse events, see a live seat map, hold seats for a few minutes, pay, and receive tickets.
  • Seats that are held but never paid for are released automatically.

Non-functional requirements:

  • A seat is never sold twice, so inventory needs strong consistency, and a paid order always ends with tickets or a refund.
  • The system stays available when the number of fans waiting is about 80 times the number of orders that can be sold.
  • A hold lasts 10 minutes.

Out of scope: resale, dynamic pricing, event creation and scanning tickets at the door.

Estimate the scale

On a normal day, 5 million orders a month is 5,000,000 ÷ 2,600,000 seconds, about 2 orders a second. Browsing adds about 200 reads a second, which one database handles easily, so the on-sale is what matters.

A 50,000-seat show sells about 25,000 orders at 2 seats per order, so with two million people waiting there are about 80 fans for every order. Storage is small, so the difficulty is contention on the same 50,000 seat rows and not size. If each waiting fan checks their place in line every 10 seconds, two million fans send 2,000,000 ÷ 10 = 200,000 requests a second. That is far too much for a database, so the waiting room must answer those checks without using one.

Estimate: How fast to let fans in

At most 10,000 fans may be in the booking flow at once, and each spends about 5 minutes there. How many new fans per second can you admit?

You know
  • At most 10,000 fans in the flow
  • About 5 minutes (300 seconds) per fan

Show the working
  1. Each slot frees up every 300 seconds
  2. 10,000 slots ÷ 300 s ≈ 33 fans a second, about 2,000 a minute

Estimate 33/s (anything from 10/s to 100/s is a good answer)

At this rate 25,000 orders sell out in about 750 seconds, roughly 12 minutes.

Your turn: Define the API

Write the endpoints to join the line, load the seat map, hold seats and pay. What should come back when the seats have already been taken?

Compare with a model answer

Joining returns a place in line, holding seats creates an order with an expiry time, and paying confirms it. When seats are gone, the error should include current availability so the fan can choose again.

Define the API Optional

Optional in the interview. Sketch the API only if the interviewer asks, if an API choice shapes the design, or if time permits. Otherwise say "I'll skip the detailed API unless you want it" and go to the data model.

HTTP API
POST /events/{id}/queue                     join the line
  200 { "ticket": "signed...", "position": 48213 }
 
GET  /events/{id}/queue/status              cached for 1 s at the CDN
  200 { "admitted_up_to": 40500, "sold_out": false }
 
POST /events/{id}/queue/pass                exchange an admitted ticket
  200 { "pass": "signed...", "expires_in": 900 }
  403 Forbidden     your number has not been reached yet
 
GET  /events/{id}/seats?section=C           needs a pass
  200 { "seats": [ { "id": "C-12", "status": "available", "price": 9500 }, ... ] }
 
POST /events/{id}/holds                     needs a pass
  body: { "seat_ids": ["C-12", "C-13"] }
  201 { "order_id": "9001", "held_until": "10:31:04" }
  409 Conflict      one or more seats are taken; the body lists them
 
POST /orders/{id}/pay    Idempotency-Key: 9001
  200 { "status": "paid", "tickets": [...] }
  402 Payment Required   the card was declined; the hold stays until it expires
  410 Gone               the hold expired before payment started
 
DELETE /orders/{id}                         release a hold early

The API decision worth arguing about is the waiting-room status endpoint. The server publishes a single number per event, such as "admitted up to 40,500", and each fan compares it with the position in their own ticket. Every fan receives the same small response, so a CDN that caches it for one second absorbs nearly all of the 200,000 requests a second.

The pay endpoint uses an idempotency key, the order id, so a double-click or a client retry never charges the card twice.

Your turn: Model the data

Sketch the records for seats and orders. How can a hold disappear on its own if the fan walks away?

Compare with a model answer

Store the hold on the seat row as a status, the holding order and a held_until time. The claim query treats an expired hold as free, so no background job has to run for the hold to lapse.

Model the data

Inventory and orders
seats
  event_id     BIGINT
  seat_id      VARCHAR(16)     -- "C-12"
  section      VARCHAR(8)
  price_cents  INT
  status       VARCHAR(10)     -- available | held | sold
  order_id     BIGINT NULL     -- the order that holds or owns it
  held_until   TIMESTAMP NULL
  PRIMARY KEY (event_id, seat_id)
 
orders
  order_id     BIGINT PRIMARY KEY
  user_id      BIGINT
  event_id     BIGINT
  status       VARCHAR(10)     -- held | paying | paid | expired | refunded
  amount_cents INT
  payment_id   VARCHAR(64) NULL
  created_at   TIMESTAMP

Claiming two seats must succeed or fail as a unit, so the inventory belongs in a relational database with transactions, partitioned by event_id when one machine is not enough. A key-value store can do a conditional write on one row, but it cannot claim two seats atomically.

Your turn: High-level design

Sketch the boxes. Where do fans wait, what checks that they were admitted, where does the seat map come from, and where are holds and sales written?

Compare with a model answer

Fans wait in a waiting room that a CDN serves. A gateway checks a signed pass, and the booking service sits in front of a seat map cache, the inventory database and the payment processor.

High-level design

Fans wait in a cheap waiting room, and only admitted fans carrying a signed pass reach the booking service and the inventory database.Fan's browser to Waiting room (join, poll); Fan's browser to API gateway (request + pass); API gateway to Booking service; Booking service to Seat map cache (seat map); Booking service to Inventory DB (hold / sell); Booking service to Payment processor (charge); Booking service to Waiting room (slots freed), asynchronous.Fan's browserWaiting roomAPI gatewayBooking serviceSeat map cacheInventory DBPaymentprocessorjoin, pollrequest + passseat maphold / sellchargeslots freed

request and replyasynchronous message

Fans wait in a cheap waiting room, and only admitted fans carrying a signed pass reach the booking service and the inventory database.
On-sale: admit, hold, a lost race, pay and expiryUse the arrows or ← → keys
  1. On-sale: admit, hold, a lost race, pay and expiry, step 1Fan's browser to Waiting room; Fan's browser to API gateway; API gateway to Booking service; Booking service to Seat map cache; Booking service to Inventory DB; Booking service to Payment processor; Booking service to Waiting room, asynchronous.Fan's browserWaiting roomup to 40,500API gatewayBooking serviceSeat map cacheInventory DBPaymentprocessor
    Ana's number
    48,213
    fans in flow
    10,000

    1Ana joined the line at 10:00 with number 48,213. Every 10 seconds her browser reads the cached status, which says fans up to 40,500 are admitted.

  2. On-sale: admit, hold, a lost race, pay and expiry, step 2Fan's browser to Waiting room; Fan's browser to API gateway; API gateway to Booking service; Booking service to Seat map cache; Booking service to Inventory DB; Booking service to Payment processor; Booking service to Waiting room, asynchronous.Fan's browserWaiting roomup to 48,300API gatewayBooking serviceSeat map cacheInventory DBPaymentprocessor
    Ana's number
    48,213
    fans in flow
    10,000

    2Admitted fans finish or time out, so the admitted number rises to 48,300. Ana exchanges her ticket for a 15-minute pass.

  3. On-sale: admit, hold, a lost race, pay and expiry, step 3Fan's browser to Waiting room; Fan's browser to API gateway; API gateway to Booking service; Booking service to Seat map cache; Booking service to Inventory DB; Booking service to Payment processor; Booking service to Waiting room, asynchronous.Fan's browserWaiting roomAPI gatewayBooking serviceSeat map cacheInventory DBPaymentprocessor
    C-12
    available
    C-13
    available

    3Ana opens section C. The gateway checks her pass locally, and the seats come from the seat map cache, which is about a second old.

  4. On-sale: admit, hold, a lost race, pay and expiry, step 4Fan's browser to Waiting room; Fan's browser to API gateway; API gateway to Booking service; Booking service to Seat map cache; Booking service to Inventory DB; Booking service to Payment processor; Booking service to Waiting room, asynchronous.Fan's browserWaiting roomAPI gatewayBooking serviceSeat map cacheInventory DB2 rows heldPaymentprocessor
    C-12
    held by 9001
    C-13
    held by 9001
    held until
    10:31

    4Ana picks C-12 and C-13. Order 9001 runs one conditional update that claims both seats only if each is available or has an expired hold. Two rows change, so she holds both.

  5. On-sale: admit, hold, a lost race, pay and expiry, step 5Fan's browser to Waiting room; Fan's browser to API gateway; API gateway to Booking service; Booking service to Seat map cache; Booking service to Inventory DB; Booking service to Payment processor; Booking service to Waiting room, asynchronous.Fan's browserWaiting roomAPI gatewayBooking serviceSeat map cacheInventory DB1 of 2: rollbackPaymentprocessor
    C-12
    held by 9001
    C-13
    held by 9001
    C-14
    available

    5A second later Ben, whose cached map still shows C-13 as free, tries C-13 and C-14. Only C-14 changes, so his claim rolls back and he receives a 409 with fresh availability.

  6. On-sale: admit, hold, a lost race, pay and expiry, step 6Fan's browser to Waiting room; Fan's browser to API gateway; API gateway to Booking service; Booking service to Seat map cache; Booking service to Inventory DB; Booking service to Payment processor; Booking service to Waiting room, asynchronous.Fan's browserWaiting roomAPI gatewayBooking serviceSeat map cacheInventory DBPaymentprocessorkey 9001
    C-12
    held by 9001
    C-13
    held by 9001
    order 9001
    paying

    6Ana pays. The order moves to paying and the hold is extended beyond the processor's longest response time. Order 9001 is the idempotency key.

  7. On-sale: admit, hold, a lost race, pay and expiry, step 7Fan's browser to Waiting room; Fan's browser to API gateway; API gateway to Booking service; Booking service to Seat map cache; Booking service to Inventory DB; Booking service to Payment processor; Booking service to Waiting room, asynchronous.Fan's browserWaiting roomAPI gatewayBooking serviceSeat map cacheInventory DBPaymentprocessor
    C-12
    sold
    C-13
    sold
    order 9001
    paid

    7The charge succeeds. One transaction marks both seats as sold and the order as paid, and Ana's slot in the flow is given to the next fan in line.

  8. On-sale: admit, hold, a lost race, pay and expiry, step 8Fan's browser to Waiting room; Fan's browser to API gateway; API gateway to Booking service; Booking service to Seat map cache; Booking service to Inventory DB; Booking service to Payment processor; Booking service to Waiting room, asynchronous.Fan's browsergoneWaiting roomAPI gatewayBooking serviceSeat map cacheInventory DBhold lapsedPaymentprocessor
    C-14
    held, expired
    C-15
    held, expired

    8Ben holds C-14 and C-15 and closes his laptop. Ten minutes later the claim query treats those holds as free.

  9. On-sale: admit, hold, a lost race, pay and expiry, step 9Fan's browser to Waiting room; Fan's browser to API gateway; API gateway to Booking service; Booking service to Seat map cache; Booking service to Inventory DB; Booking service to Payment processor; Booking service to Waiting room, asynchronous.Fan's browserWaiting roomAPI gatewayBooking serviceSeat map cacheInventory DBPaymentprocessor
    C-14
    available
    C-15
    available

    9A cleanup job runs every 30 seconds, sets lapsed seats back to available and marks the order expired, so the seat map shows them again.

1 / 9

Deep dives

Preventing double booking

Suppose two requests for C-13 arrive within a millisecond of each other. If each one reads the seat as available and then writes it as held, both succeed and the seat is sold twice, so the database has to make exactly one of them fail. There are three ways to do that, and a fourth that moves the hold out of the database. The mistake to avoid in all of them is checking in one statement and writing in another, because the check and the write must be one atomic step.

Pessimistic locking. The transaction runs SELECT … FOR UPDATE on the seat rows, which makes every other transaction that touches those rows wait. It then checks the status and writes. This works when a decision needs several reads, but buyers queue behind the lock, so the transaction must be short and must never stay open while a fan decides or pays.

Optimistic version check. Each row has a version number. The code reads the row, and later writes with WHERE version = 7, so the write changes nothing if another transaction wrote first, and the code retries. This is good when conflicts are rare, but during an on-sale most attempts hit the same rows, so most writes fail and retry.

Conditional update. A single UPDATE statement claims the seats only if they are free, and the caller checks how many rows changed. It takes one round trip, and the row lock lasts only for that statement. It fits whenever the rule can be written in a WHERE clause, as it can for claiming a known set of seats.

Holds in an in-memory store. A store such as Redis can create a hold with "set if absent, expire in 10 minutes", which is fast and expires on its own. It is a second source of truth, though: if a failover loses a recent write, two fans can hold one seat, so the database must still confirm the sale.

Summary of the options for preventing double booking
OptionGood atBad atPick it when
Pessimistic lockIt is easy to reason about, and it supports checks that need several reads.Other buyers wait behind the lock, and holding it while a fan pays would freeze seats for minutes.The check needs several reads and the transaction is short.
Optimistic version checkIt takes no read locks and is cheap when conflicts are rare.Under heavy contention most attempts fail and retry, which wastes work.Conflicts are rare, as on a normal day.
Conditional update Usual defaultIt claims the seats in one statement and one round trip, and the lock lasts only for that statement.The rule must fit in a WHERE clause, and the caller must check how many rows changed.Claiming a known set of seats, which is the usual answer here.
Holds in an in-memory storeIt is very fast, and holds expire without any cleanup job.It is a second source of truth, so the database must still confirm every sale.The database cannot handle the hold rate even behind a waiting room.

I would choose the conditional update, because it needs one round trip and no lock is held while anyone is thinking. The seats are claimed like this:

Claim two seats, all or nothing
BEGIN;
UPDATE seats
   SET status = 'held', order_id = 9001,
       held_until = now() + interval '10 minutes'
 WHERE event_id = 77 AND seat_id IN ('C-12', 'C-13')
   AND (status = 'available'
        OR (status = 'held' AND held_until < now()));
-- rows changed = 2: COMMIT.  fewer than 2: ROLLBACK and report 409.
COMMIT;

This is safe because the second transaction to update a row waits for the first one's row lock. At the read-committed level in databases such as PostgreSQL, it then re-checks its WHERE clause, sees the seat is held, and changes nothing. At repeatable read or serializable it fails with a serialization error, which you treat like a 409. Databases differ here (see transactions and isolation), so name the isolation level you rely on. Two orders that claim overlapping seats in opposite order can deadlock, and sorting the seat ids before claiming avoids that.

Handling the on-sale spike with a waiting room

Adding booking servers does not help, because all 50,000 seats live in one partition and more servers only send it more conflicting writes. Returning a 429 error to the excess traffic protects the database, but it rewards whoever retries fastest, which usually means bots. A virtual waiting room limits how many fans reach booking and gives the rest a fair place in line.

The waiting room works in five parts:

  • When a fan joins, they receive a number in a signed ticket. Numbers handed out before 10:00 are shuffled at 10:00, so arriving early at the page gains nothing, and after that numbers are assigned in order of arrival.
  • While waiting, the browser reads only the cached "admitted up to N" value, so 200,000 checks a second cost almost nothing.
  • A controller keeps at most 10,000 fans in the booking flow and raises N as orders complete or passes time out. The database therefore sees about 33 new fans a second, however large the crowd is.
  • An admitted fan exchanges their ticket for a signed 15-minute pass tied to their account, and the gateway verifies it without any shared state.
  • When no seat is available or held, the event is announced as sold out and waiting fans are told at once.

The seat map cache is rebuilt about once a second. The conditional update is the source of truth, so a stale map costs a fan one 409 and never causes a double sale.

Holds, payment and releasing seats

The hardest cases in a booking system happen between the "held" and "sold" states. Storing the hold in the seat row, with an expiry time, means an abandoned hold becomes claimable without any cleanup, because the claim query already treats it as free. The cleanup job only refreshes the seat map and the order status.

The second problem is that a hold must outlive the payment. If a hold lapses at 10:31:04 and the charge succeeds at 10:31:05, another fan may already have claimed the seat. So when the fan presses pay, the order moves to paying and held_until is extended beyond the longest time the processor can take. The sale then runs as UPDATE seats SET status = 'sold' WHERE order_id = 9001 AND status = 'held' and must change both rows. If it does not, the order is refunded automatically.

The third problem is that a timeout is not a failure. After a timeout the charge may still have succeeded, so the order stays in paying, the booking service asks the processor for the result using the idempotency key, and only then sells or releases the seats. Design a Payment System covers this in more detail.

Bottlenecks and failures

If demand grows 10 times to twenty million fans, the waiting room answers 2 million checks a second for one cached number, which is a CDN problem and not a database problem. The booking side does not change, because the admission cap fixes its load. When many big on-sales overlap, partition the inventory by event_id so each show has its own partition.

When this failsWhat happensWhat you do
A booking serverRequests in progress fail, but committed holds are safe in the database.The fan retries and the request goes to another server.
The seat map cacheThe 10,000 admitted fans refresh every 5 seconds, which sends 10,000 ÷ 5 = 2,000 reads a second to the database.Serve a slightly stale copy from each booking server's memory.
The inventory primaryHolds and sales stop until a replica takes over.Use synchronous replication, because with asynchronous replication a replica that missed an acknowledged hold would sell the seat again. Pause admissions during the failover.
The payment processorFans cannot pay and their holds start to lapse.Pause admissions and extend unexpired holds, because fans should not lose seats during an outage they did not cause.
The waiting room originThe CDN keeps serving the last admitted number, so nobody new is let in.Run it in several zones. Fans who were already admitted keep booking.

What a strong answer sounds like

LevelWhat the answer does
JuniorDraws a booking service and a database, and with prompting wraps "check, then book" in a transaction.
Mid-levelAdds holds that expire, chooses between a lock and a conditional update and explains why, and caches the seat map.
SeniorRecognises that the spike lands on one partition, adds a waiting room with a cheap status endpoint, protects the gap between hold and payment, and names the cost of each choice.
Handling the spikeStrong answer
Interviewer

Two million people arrive at 10:00. What do you do?

You

All of that demand lands on one event's rows, so I cannot scale it out. I would use a waiting room that admits about 33 fans a second and keeps 10,000 in the booking flow. It publishes only an "admitted up to N" number, so the 200,000 checks a second are served by a CDN.

Interviewer

Why not just return a 429 when you are overloaded?

You

That protects the database, but it rewards whoever retries fastest, which is usually bots. A line is fairer to real fans and gives the database a predictable load.

Follow-up questions

How would you handle general admission?

Keep a count of remaining tickets per section and decrement it with a conditional update such as SET remaining = remaining - 2 WHERE remaining >= 2. All claims then wait for the lock on that one row, so split the count across about 10 rows and have each request try a random row that still has tickets.

How do you stop bots from buying everything?

Allow one place in line per verified account, and add a challenge such as a CAPTCHA when a fan joins the line. Also limit the seats one account can buy per event. These measures reduce bots without removing them.

Why not use a distributed lock on each seat?

A lock service adds a second system that can disagree with the database about who owns a seat, and a lock that expires while its holder is paused can let two buyers in. The database already provides an atomic update on the seat row, so the claim can be made there with no extra component.

A group wants six seats together. What changes?

Offer a "best available" option in which the booking service searches for six adjacent free seats and claims them with the same all-or-nothing update. If one was taken in the meantime, fewer than six rows change, so it rolls back and searches again.

Self-review: ticket booking0 of 12

After your attempt, tick what you really did. Be strict: "I thought about it" is not "I said it out loud".

Before drawing

Numbers and interfaces

Design and depth

Failure