Seat holds for a rail ticket sale
Design the service that holds a seat while a passenger pays, for a sale where forty thousand people want the same four hundred seats. Concurrency, an estimate with a trap, and a payment that arrives after the hold has expired.
The brief
Kittering Rail opens advance fares for the summer timetable next Tuesday at eight. Design the service that holds a seat while a passenger pays for it. What we know so far is on screen, and I'd like you to draw while you talk.
What the candidate sees:
- About 1.8 million seats go on sale at once: roughly 4,500 trains of about 400 seats, around 55 a day for twelve weeks
- Last year about 220,000 people were on the site in the first ten minutes, and the site fell over twice
- A passenger picks a train, the site holds a specific seat, and they have ten minutes to pay through the payment provider
- Today a hold is a row in Postgres, checked and written by the booking service:
SELECTthe seat, and if it is free,INSERTthe hold - Goal: never sell the same seat twice, never lose a sale to a hold that should have expired, and stay up on Tuesday
Estimate before you design: what peak load must the hold path survive, and where?
The naive estimate is 220,000 people over 600 seconds, about 370 holds a second, which any Postgres handles. That number is right and irrelevant. Demand is not spread across 1.8 million seats: last year 80% of the first hour's searches were for about 40 Friday-evening and bank-holiday trains. Each search reads availability for about 12 trains, and people search about six times, so the read path sees roughly 26,000 availability lookups a second, while the write path's real problem is contention: tens of thousands of people competing for the same few hundred seat rows on one train. A strong candidate puts the risk on the hot train, not on the aggregate rate.
What the interviewer knows
The interviewer holds facts that are shared only when the candidate asks the question that unlocks them. Asking is part of the score.
- A seat is not one thing. The 17:30 to Edinburgh stops five times. A York to Edinburgh ticket uses only the last two legs of seat 42, so the same seat can be sold to two passengers on different legs. Unlocked by asking what a seat or a booking covers, or whether trains stop on the way.
- Payments can confirm late. About 1 in 200 card payments needs a bank challenge, and the provider sometimes confirms those twelve to fifteen minutes after the passenger started, after a ten-minute hold has lapsed. Unlocked by asking how payment works, or what happens when it is slow.
- Some seats are protected. Wheelchair spaces and 2% of each train's seats must stay bookable until the day of travel, by regulation. Unlocked by asking about seat types or rules on what can be sold.
- Bots. Last year about 30% of first-hour holds came from about 400 accounts that resold tickets. Unlocked by asking about abuse, fairness or limits per customer.
The follow-up ladder
Each rung is asked when its condition is met. Every rung tests named rubric signals; the twist is asked of every candidate.
- Deepen (always). Two people click the last window seat on the 17:30 in the same millisecond, on different servers. Walk me through exactly what guarantees only one of them gets it, and what the other sees.
- Twist (always, once they have a working hold path, or six minutes in). Change of plan. Marketing is launching the sale with a one-hour 30% discount and expects five times the traffic in the first two minutes. The hot train has 400 seats and 40,000 people who want one. What breaks, and what do you change? Reveals: the payment provider allows 150 new payment sessions a second for the whole account; the site can show a waiting page.
- Failure. A passenger's payment confirms fourteen minutes after they started. Their hold expired at ten, and the seat was sold to someone else at eleven. The money has been taken. What does your system do, and what should it have done?
- Numbers and proof. Convince me no interleaving of requests can ever sell a seat-leg twice. Where exactly is that enforced, and how would you know in production within minutes if it ever happened?
- Edge, for candidates tracking strong (staff bar). Step back. Is a first-come race the right way to sell 400 seats to 40,000 people? What would you measure to know Tuesday went well?
Strong and weak answers
On the race (rung 1):
- Strong: "The check and the write have to be one atomic step in the database. Either a conditional insert that fails on a constraint, or an update of the seat-leg rows where they are still free, and I read the row count. Under read committed, the second transaction blocks on the first one's row lock, then re-checks and matches nothing. The loser gets a clear 'just taken, here are similar seats'."
- Weak: "I'd wrap the select and the insert in a transaction." That doesn't stop the race at read committed: both selects see the seat free. Or: "A Redis lock per seat." A lock with a timeout can expire while the holder is paused, so two holders can each believe they own the seat. On its own it is not a guarantee.
On the twist (rung 2):
- Strong: "The bottleneck is one train's rows and the payment provider's 150 a second, not total throughput. I'd put a waiting room in front of the hot trains and admit people at the rate the payment step can absorb. I'd hold a count per train and fare rather than a specific seat, and assign the seat after payment, so 40,000 people aren't fighting over one row each. Read availability from a cache that may be a few seconds stale, and say so on screen."
- Weak: "Scale Postgres up and add replicas." Replicas don't help writes, and a bigger box doesn't remove contention on the same rows.
On the late payment (rung 3):
- Strong: "A hold with a payment in flight isn't expired. It's a pending state with its own longer deadline, set when we create the payment session with the hold id as the idempotency key. If the provider still confirms after we've released the seat, we have a compensating path: offer an equivalent seat, or refund automatically and say so. Either way, it's reconciled against the provider's report every hour."
- Weak: "Just refund them." True, but the design created the problem, and the candidate never connects the hold's lifetime to the payment's.
Rubric signals
Each signal is tagged with the earliest rung it can be shown at and, for advanced signals, the seniority bar it separates.
Core
- s1 (rung 0). Clarifies what a seat and a booking cover (legs), the hold lifecycle, volumes and fairness before drawing components.
- s2 (rung 1). Makes the hold an atomic conditional write in the database (conditional update or insert against a constraint, checking rows affected), never read-then-write.
- s3 (rung 1). Treats expiry as a real state transition with the database clock as the source of truth, released lazily on the next attempt or by a sweeper, idempotently.
- s4 (rung 1). Separates the read path (cached, possibly stale availability) from the authoritative write path, and says which one users see.
Advanced
- s5 (rung 2). After the twist, locates the bottleneck on hot-train contention and the payment limit, and admits traffic through a waiting room sized to the payment rate. Holds counts per train and fare with seat assignment deferred, and shards by train.
- s6 (rung 3, senior). Links the hold's lifetime to the payment session: a pending-payment state, the hold id as idempotency key, a compensating path, and reconciliation with the provider.
- s7 (rung 4, senior). States the invariant (at most one active hold or booking per seat-leg) and enforces it in the schema. For example, a Postgres exclusion constraint over seat and leg range for active rows, which means expiry must be a status change, because a constraint can't depend on the current time. Adds a continuous reconciliation query that alerts.
- s8 (rung 5, staff). Questions the first-come race: proposes a ballot or queue-and-allocate for the hottest trains and per-account limits against resellers. Measures seats sold to distinct genuine travellers and complaint rate, not requests served.
Red flags
- r1. Checks availability in application memory or a cache and writes the hold on that basis.
- r2. Relies on a distributed lock with a timeout as the only guarantee against double booking.
- r3. Extends every hold until payment finishes, with no bound, so abandoned holds take seats off sale for hours.
Level anchors
What each level of the scorecard looks like on this question.
- Not yet. Select-then-insert in a transaction, with no constraint. Believes a transaction alone prevents the race, and scales the database when traffic grows.
- Developing. Finds an atomic hold, but treats legs as whole seats, misses the hot-train contention, and handles a late payment with "refund them".
- Hire-ready. Clarifies legs and the hold lifecycle, makes the hold atomic and expiry a real state, and sees after the twist that contention and the payment limit are the problem, with a waiting room and deferred seat assignment. Ties the hold to the payment and states an enforceable invariant.
- Standout. All of level three, plus spots the hot-train problem from the estimate before the twist, enforces the invariant in the schema with a reconciliation alert, and questions whether a race is the right product, with a fairness metric.
Model answer
Clarify first: what does a booking cover (legs between stops), how long is a hold, what are the volumes, and are there rules on what can be sold. The estimate's lesson is that 370 holds a second is easy and misleading. The real load is about 26,000 cached availability reads a second, plus contention concentrated on about 40 trains.
Hold path. Model inventory as seat-leg rows, or as a seat with a leg range. A hold is one atomic statement: update the seat-leg rows for this seat and these legs where they are free, setting held, the hold id and an expiry from the database clock. Check the row count; anything less than all legs means rollback and "just taken". Under read committed the second concurrent update waits on the row lock, re-evaluates its condition against the committed row, and matches nothing. Back it with a constraint so no code path can break it. In Postgres, an exclusion constraint using btree_gist over seat id with equality and the leg range with overlap, for rows whose status is held or booked. Because a constraint can't reference the current time, expiry is a real status change: release lazily inside the next attempt's transaction, plus a sweeper.
Reads. Serve availability from a cache refreshed every few seconds and say "seats are going fast". The hold path is the only authority.
Twist. Five times the traffic on one 400-seat train, against a payment provider that takes 150 new sessions a second. Put a waiting room in front of hot trains and admit people at about the payment rate. Inside, hold a count per train and fare bucket (one row, decremented conditionally) and assign specific seats after payment, so admission is cheap and seat choice doesn't become a 40,000-way race. Shard inventory by train. Rate-limit per account and per card to blunt resellers.
Late payment. Create the payment session with the hold id as idempotency key and move the hold to pending payment with a deadline of about twenty minutes, so a challenge in progress doesn't release the seat. If the provider confirms after we've released it anyway, offer an equivalent seat or refund automatically and tell the passenger. Reconcile against the provider's report hourly.
Proof. The invariant is at most one active hold or booking per seat-leg. The exclusion constraint enforces it for every writer. In production, run a query every minute for overlapping active rows (it should always return nothing), and page on any result.
Staff. A race rewards bots and fast connections. For the hottest trains, a ten-minute ballot window with allocation afterwards is fairer and much cheaper to run. Success is the share of seats sold to distinct genuine travellers and complaint rate, with availability of the site as a guardrail.
Common mistakes
- Puts select-then-insert in a transaction and assumes that prevents double booking
- Treats a seat as indivisible and misses legs entirely
- Estimates the average load and never finds the hot train
- Answers the traffic twist with bigger databases or read replicas
- Lets the hold expire on a timer while the payment is still in flight
- Proves correctness with "we tested it under load" rather than an invariant and a constraint