Scrivio Interview library
CodingFoundations8 min read · updated 13 November 2026

A ticket booking service sells reserved seats for concerts and sports. A buyer picks seats, which are held for a few minutes while they pay; if payment does not finish in time, the seats go back on sale.

Design the API and data model for seat selection with timed holds, and write the SQL statement that places a hold on one seat so that two buyers can never hold the same seat.

We expose four calls: GET /events/{id}/seats for the seat map, POST /holds with seat ids and an idempotency key, POST /holds/{id}/purchase to pay and DELETE /holds/{id} to release. Seats live in one row each with a status, a hold_id and a hold_expires_at, and a hold is a single conditional UPDATE that only succeeds while the seat is available or its old hold has expired, so exactly one buyer can ever win the row.

Why this question is asked

Seat holds look simple on a whiteboard, but they are where ticketing systems sell the same seat twice. The interesting part is not the endpoints; it is the one statement that decides who owns a seat when two people click it in the same millisecond. Interviewers ask this to see whether we let the database settle the race or try to settle it in application code, which always loses. In this answer, we will sketch the requirements, design the API and the tables, write the hold statement and see why it is safe, extend it to several seats at once, and finish with how expired holds go back on sale.

1A Quick Recap of What a Hold Must Do

A hold is a short reservation. When a buyer picks seat A-12, nobody else may pick it for the next few minutes, while the buyer types card details. If the buyer pays, the hold becomes a ticket. If the buyer gives up or the clock runs out, the seat must become available again without anyone pressing a button.

So we have three requirements. Two buyers must never hold the same seat at the same time. A buyer asking for four seats should get all four or none, because nobody wants three seats together and one in another stand. And a hold must end by itself when its time is up. Rough numbers help: a 20,000-seat arena has 20,000 seat rows per event, which is tiny for PostgreSQL. The difficulty is concurrency, not size, so our design focuses on making each change atomic.

2Designing the API

The API follows the buyer's journey. The seat map is a read. Placing a hold creates a resource, so it is a POST that returns the hold's id and its expiry time. Paying acts on that hold, and releasing it deletes it.

Seat booking endpoints
CallBody or resultPurpose
GET /events/{id}/seatsseat id, section, row, tier, statusdraw the seat map
POST /holdsevent id, seat ids, Idempotency-Key -> hold id, expires_athold seats for 8 minutes
POST /holds/{id}/purchasepayment details -> order idpay and turn the hold into tickets
DELETE /holds/{id}nonebuyer gives the seats back

The idempotency key deserves a word. It is a random value the client creates once per attempt and sends with the request. If the network drops the response and the app retries, the server sees the same key, finds the hold it already created and returns it, instead of creating a second hold. Without it, a flaky mobile connection can lock seats twice for one buyer.

3The Data Model

We keep one row per seat per event, because the seat is the thing people fight over and a row is the unit the database locks. The holds table records who holds what and until when, and orders and tickets record what was finally sold.

SQL
CREATE TABLE seats (
event_id bigint NOT NULL,
seat_id text NOT NULL, -- 'A-12'
section text NOT NULL,
row_label text NOT NULL,
price_tier text NOT NULL,
status text NOT NULL DEFAULT 'AVAILABLE', -- AVAILABLE, HELD, SOLD
hold_id uuid,
hold_expires_at timestamptz,
PRIMARY KEY (event_id, seat_id)
);
CREATE TABLE holds (
id uuid PRIMARY KEY,
buyer_id bigint NOT NULL,
idempotency_key text NOT NULL UNIQUE,
expires_at timestamptz NOT NULL,
state text NOT NULL -- ACTIVE, PURCHASED, RELEASED, EXPIRED
);
-- orders (id, hold_id, buyer_id, total, paid_at) and tickets (order_id, event_id, seat_id)

Notice that the seat row carries hold_id and hold_expires_at itself. That is deliberate: the hold statement can decide everything by looking at a single row, without joining to holds, and a single-row check is what the database can make atomic cheaply. The unique constraint on idempotency_key is what makes the retry of POST /holds safe.

4The Statement That Places a Hold

The tempting version reads first and writes second: SELECT the seat, check in Java that it is available, then UPDATE it. Two requests can both run the SELECT before either runs the UPDATE, both see AVAILABLE, and both believe they won. This is the classic check-then-act race.

The fix is to put the check inside the write. The UPDATE below changes the row only if, at the moment it runs, the seat is free or its previous hold has expired.

Java
UPDATE seats
SET status = 'HELD',
hold_id = :holdId,
hold_expires_at = now() + interval '8 minutes'
WHERE event_id = :eventId
AND seat_id = :seatId
AND (status = 'AVAILABLE'
OR (status = 'HELD' AND hold_expires_at < now()));
-- buyer 1: UPDATE 1 -> the seat is held for buyer 1
-- buyer 2: UPDATE 0 -> the seat is taken, answer 409 Conflict

Why is this safe? PostgreSQL takes a row lock before it changes a row. If buyer 2's UPDATE reaches the row while buyer 1's transaction is still open, it waits. When buyer 1 commits, PostgreSQL re-checks buyer 2's WHERE clause against the new version of the row, finds status HELD with an expiry in the future, and updates nothing. So the application only has to read the row count: one means success and zero means somebody else got there first. There is no window between checking and writing, because they are the same step.

5Holding Several Seats at Once

A buyer usually wants several seats together. We run one UPDATE per seat inside a single transaction, and if any of them returns zero rows we roll the whole transaction back, so the buyer gets all the seats or none.

But there is a catch: deadlocks. Suppose buyer 1 asks for A-12 then A-13, and buyer 2 asks for A-13 then A-12. Each locks its first seat and then waits for the other's, forever, until PostgreSQL detects the cycle and kills one transaction. The cure is to always lock seats in the same order, for example sorted by seat_id. Then both buyers try A-12 first, one waits behind the other, and no cycle can form.

Java
List<String> ordered = seatIds.stream().sorted().toList(); // fixed order: no deadlock cycles
tx.executeWithoutResult(status -> {
for (String seat : ordered) {
int n = jdbc.update(HOLD_SQL, holdId, eventId, seat);
if (n == 0) throw new SeatTakenException(seat); // rolls back every seat in this hold
}
});
// seats [A-13, A-12] -> locks A-12 then A-13 -> all held, or none
Step throughPlacing a hold on several seats
  1. Seen this Idempotency-Key?unique index on holds
    YesReturn the existing holdNoStart a transaction
  2. Sort the seat idssame order for every buyer
  3. Conditional UPDATE for each seatAVAILABLE or expired hold
  4. Every update returned 1 row?check the counts
    YesInsert the hold and commitNoRoll back and return 409

4 steps in order. Play them, or step through them one at a time.

6Letting Expired Holds Go Back on Sale

Notice that the hold statement already treats an expired hold as free: the clause status = 'HELD' AND hold_expires_at < now() lets the next buyer take the seat. So correctness never depends on a timer firing on time. If the cleanup job is down for an hour, no seat is lost; the first new buyer simply takes over the expired hold.

We still run a background job every few seconds that sets expired seats back to AVAILABLE and marks their holds EXPIRED. Its job is honesty, not safety: without it, the seat map would show seats as held long after their holds ended, and buyers would skip seats they could actually have. The job uses the same conditional style, UPDATE ... WHERE status = 'HELD' AND hold_expires_at < now(), so it can never release a hold that is still valid or one that has just been purchased.

7Follow-up questions

Why not use SELECT ... FOR UPDATE and then an UPDATE?

It works, because the lock taken by FOR UPDATE stops the second buyer until the first commits. But it needs two round trips and an explicit transaction for a single seat, and it is easy for a later change to drop the FOR UPDATE and reintroduce the race. The conditional UPDATE expresses the whole rule in one statement, which is shorter, faster and harder to break.

Should the hold live in Redis instead of the database?

Redis with SET key value NX PX 480000 also gives an atomic, self-expiring hold, and it is faster. The cost is that the hold and the sale now live in two systems, and a Redis failover with asynchronous replication can lose a hold. For a normal event the database handles the load comfortably, so keeping one source of truth is simpler. Redis becomes attractive only at extreme on-sale peaks, and then it is usually a cache in front of the database rather than the record.

How long should the hold be?

Long enough for most buyers to pay and short enough not to starve others. Eight to ten minutes is common; we choose it from the measured time between hold and payment, for example covering 95 percent of successful purchases, and we may shorten it during peak on-sales.

8Conclusion

In this answer, we designed a seat booking API around the buyer's journey, with an idempotency key so retries never create duplicate holds. We stored one row per seat carrying its own hold id and expiry, and we placed a hold with a single conditional UPDATE whose row count tells us who won. For several seats we used one transaction in a fixed seat order to get all or nothing without deadlocks, and we saw that the hold condition itself releases expired holds, while a background job only keeps the seat map truthful.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start from the buyer's journey: GET /events/{id}/seats for the map, POST /holds with the seat ids and an idempotency key that returns a hold id and an expiry, POST /holds/{id}/purchase to pay, and DELETE /holds/{id} to give the seats back.

Then, if they want more

In the database I keep one row per seat per event with status, hold_id and hold_expires_at, a holds table with the buyer, expiry and state, and orders and tickets for what was sold.

The key decision is that the hold is one conditional UPDATE: set status to HELD, the hold id and now plus eight minutes, where the event and seat match and the seat is either AVAILABLE or HELD with an expiry in the past. If it updates one row, the buyer has the seat; zero rows means someone else has it.

PostgreSQL locks the row and re-checks the condition for the second writer, so there is no check-then-act race. For several seats I run the updates in one transaction, sorted by seat id so two buyers cannot deadlock, and roll back if any returns zero, so it is all or nothing.

Because an expired hold already counts as free in the condition, correctness does not depend on a timer, but I still run a cleanup job every few seconds so the seat map shows the truth.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Design the API and data model for seat selection with timed holds, and write the SQL...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/ebd56d8162/