· 4 min read
Two clients, one slot: how TimeUp avoids double-booking
The bug every booking app ships first, and the combination of a transaction, a lock and a claim step that closes it.
Two people open the same barbershop page. Both see 4:00 pm free. Both press Book within the same second. If the code is "check if free, then save", both checks pass and the shop has two clients for one chair.
Why checking first is not enough
The gap between the check and the write is tiny, but it exists, and public booking pages are exactly where many people act at once. This is a classic check-then-act race, and it only shows up under load, which is the worst time to find it.
Both got the same slot: a double booking.
Three pieces that close the gap
- A SERIALIZABLE transaction, so the database rejects results that could not have happened one after the other.
- An advisory lock keyed by staff member and day, so two commits for the same person and date queue up instead of racing.
- A claim step on the booking intent that moves it from PENDING to CONSUMING exactly once, so a repeated request cannot book twice.
Why a lock on the day
Locking one row is not enough, because the conflict is about a time range, not a single record. Locking per staff member and day is narrow enough that different people never wait for each other, and wide enough to cover every slot that could overlap.
The lesson I keep: concurrency bugs are not fixed by being careful in application code. They are fixed by making the database the referee.