I built a booking app for gyms with React, TanStack Start and Supabase. The part that looked easy and wasn't: two people booking the same slot at the same moment.
This post explains the approach I ended up with, with a simplified version of the code.
The problem
A client opens the booking page, picks a service, a date and a time slot. Two clients can do this for the same trainer and the same slot within milliseconds of each other.
If your app does this in two steps:
SELECT to check the slot is free
INSERT the booking
then both requests can pass step 1 before either one reaches step 2. Result: a double booking.
Checking in the frontend or in your API code doesn't fix it, because the race happens between your two queries.
Why not just a unique constraint?
A unique constraint works if every booking uses exactly the same start time.…