Booking backend project · Chapter 8 of 12
Seat holds
By the end of this chapter you can hold seats for a member inside one transaction, all or nothing; read what Postgres does when two transactions insert the same row at the same moment (one waits for the other's lock, then fails with a unique violation); and turn that violation into a clean 409 that Rukhsana's seat map can show. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Selling seats
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Booking backend project are free. This one comes with the all labs plan.
Why you need this
This is the chapter Parda's backend exists for. On Saturday 17 October the old booking script sold seat E7 of the 9:30 pm Ghar Wapsi Express to two people, and 11 more seats twice that weekend, because it checked "is the seat free?" and then wrote "it is theirs" as two separate steps. Datta refunded 12 people. For Diwali week Sayali has one rule: no seat is ever sold twice.
Rukhsana's seat map works like every cinema site: you tap E7 and E8, and they are held for you for 10 minutes while you pay; nobody else can tap them. Chapter 7 already shows held seats on the map. What is missing is the code that holds them. That code has three ways to go wrong:
- It holds E7, then fails on E8, and leaves E7 held: half a pick.
- Neha picks again, her old seats are deleted, the new pick fails, and she has nothing.
- Neha and Pranav tap E7 in the same second, and both get it, or one gets a 500.
Chapter 1 put the rule in the database: the holds table has a unique constraint holds_showtime_seat on (showtime_id, seat_id). This chapter uses it.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
holds.service.ts: hold seats, all or nothing
Write holdSeats(userId, { showtimeId, seatIds }) in src/modules/holds/holds.service.ts. It keeps the seats for this member for config.holdMinutes (10) minutes, every step inside one transaction, in this order: no such showtime is an AppError 404; a showtime whose status is not scheduled is a 409 with code showtime_cancelled; any seat id that is not a seat of the showtime's screen is a 422 with code bad_seats; then delete this showtime's holds that are this member's own (a new pick replaces the old one) or that have expired (expires_at at or before now); then any seat already sold (a booking_seats row for this showtime) is a 409 with code seat_taken; then insert one holds row per seat with expiresAt now plus 10 minutes.
Return { showtimeId, seats, expiresAt }: seats are the seat labels in the order asked, such as ["E7", "E8"] (seatLabel makes them), and expiresAt is the Date you stored.
The Check holds E7 and E8 (seats 79 and 80) of showtime 9 for Neha (user 2), tries showtime 999, a cancelled showtime and seat 500 of another screen, lets Neha pick E9 instead, holds a seat whose hold expired a minute ago, and finally asks for E6 and E7 when E7 is sold while Neha holds E5: that is refused, E6 must not be held and E5 must still be hers. Then POST /holds as Neha must answer 201 and the seat map must show E7 as held.
- Task 2 · interview question
Interview: holds.service.ts, two people and one seat
When two members hold the same seat at the same moment, Postgres lets one insert in and refuses the other with a unique violation (code 23505) on the constraint holds_showtime_seat. Make holdSeats turn that into an AppError 409 with code seat_taken, never a 500. Read the code and the constraint with the helpers in src/lib/pg-errors.ts (isUniqueViolation or pgErrorCode, and pgConstraint), not from the error's message: Drizzle wraps the driver's error, and the code is on its cause.
The Check opens a transaction of its own that holds E7 for Pranav (user 3) and keeps it open while your holdSeats holds E7 for Neha; your insert waits, then the Check commits and your call must reject with the AppError 409 seat_taken, and E7 must have one hold, Pranav's. Then Neha and Pranav hold E8 at the same moment: exactly one gets it and the other gets the 409. Then POST /holds as Asha for a seat Neha holds must answer 409 with the code seat_taken.
Nikhil will ask: Neha's insert waited until Pranav's transaction ended. What was it waiting for, and why did it fail afterwards instead of going in?
- Task 3
holds.service.ts: let go of my seats
Write releaseHolds(userId, showtimeId). It deletes this member's holds on that showtime, and nobody else's, and returns how many it deleted, as a number (0 when there were none).
The Check gives Neha two holds on showtime 9 and one on showtime 4, and Pranav one on showtime 9: releaseHolds(2, 9) must return 2 and leave Pranav's hold and Neha's hold on showtime 4. Then DELETE /holds/9 as Neha, holding one seat, must answer 200 with { "released": 1 }.
What you will learn
- A hold: a row that keeps a seat for one member until expires_at.
- One transaction, all or nothing: every step commits together or none does.
- Row locks and the unique index: what Postgres does when two transactions insert the same seat.
- Error mapping: turning Postgres's 23505 into a 409 seat_taken.
- Why checking first is not enough, shown with a real lock.
More in Selling seats
Build "Seat holds" yourself
Parda sells tickets for Pune's own cinemas and stages. You build its booking API, one module a chapter, and take the whole codebase home. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Booking backend project are free. This one comes with the all labs plan.