Let’s talk

Concurrency / Trap 01·Booking system series

The double-booked seat

Two users hit 'Book' on the last seat at the same moment. Your code checks availability first. Who gets the seat?

Animated from a real recorded run against Postgres. Press play, or drag the bar to scrub.

Why it breaks

Both requests read 'available' before either one writes. The check and the write are separate statements, so the gap between them is a window anyone can walk through.

The interview answer

Never check-then-act across two statements. Make the check part of the write (UPDATE … WHERE status = 'available'), and back it with a UNIQUE constraint.

Other fixes interviewers accept

Same problem, different trade-offs. Pick by situation.

UPDATE … WHERE status = 'available'
The default answer. Simple, fast, one round trip.
SELECT … FOR UPDATE inside a transaction
You need to read several rows and decide in code. Keep the transaction short and never hold it across a payment call.
UNIQUE constraint and catch the violation
Inserting the claim row is the natural write. Add it as a safety net either way.
Optimistic locking (version column)
Contention is rare and you do not want blocking.
Distributed lock (for example Redis)
The resource is not in one database. Usually overkill for this problem.

The 70-second version

The same trap as a short reel: the broken code, the fix, and the interview rule.

Make it fail on your machine

The proof fires 50 concurrent requests at a real Postgres. One command.

npm run 01:bug