Guides · Architecture
How to build a booking system that never double-books: locks, holds and transactions.
Prevent double bookings in a web app: availability model, database transactions, row locks, temporary holds with expiry, unique constraints.
Key takeaways
- Double booking is a race condition: two requests see the same availability at the same time.
- Make the database enforce limits, using transactions, locks or unique constraints.
- Use a temporary hold that expires if the payment does not complete.
- Test with concurrent requests, not only with one user clicking at a time.
How double booking happens
Two customers open the same departure with one seat left. Both see "1 available". Both click Book. If the code reads the count, checks it, then writes a booking, both requests can pass the check before either writes. Now there are two bookings for one seat.
Model availability
Decide what is limited:
- Seats on a departure.
- A room on a night.
- A time slot for a person or resource.
Store capacity, and count booked or held units, either with a counter column or by counting booking rows.
Fix 1: transaction with a row lock
Lock the row that holds the capacity while you check and update it:
DB::transaction(function () use ($departureId, $seats) {
$departure = Departure::whereKey($departureId)->lockForUpdate()->firstOrFail();
if ($departure->seats_available < $seats) {
throw new NotEnoughSeats();
}
$departure->decrement('seats_available', $seats);
Booking::create([...]);
});
lockForUpdate() makes the second request wait until the first transaction finishes, then it sees the updated count. This works with databases such as MySQL InnoDB and PostgreSQL.
Fix 2: atomic update
Do the check and update in one statement:
$updated = Departure::whereKey($id)
->where('seats_available', '>=', $seats)
->decrement('seats_available', $seats);
if ($updated === 0) { /* not enough seats */ }
Only one request can succeed when seats run out.
Fix 3: unique constraints
For a single resource and slot, such as one room on one night, create a unique index on (room_id, date). The database refuses a second booking even if your code has a bug.
Holds for payment
Between choosing and paying, the seat should be reserved. Create the booking as pending and set expires_at, such as fifteen minutes ahead. Count pending, unexpired bookings as taken. A scheduled job releases expired holds. See Laravel queues and the scheduler.
When payment succeeds, convert the hold to confirmed. When it fails or expires, release it.
Payments arriving late
A customer may pay after the hold expired, or the webhook may arrive twice. Handle these cases:
- If seats remain, confirm the booking.
- If not, refund or put the customer on a waitlist, and tell them clearly.
- Make confirmation idempotent. See integrating Razorpay in Laravel.
Cancellations
When a booking is cancelled, return the seats inside a transaction, and record who and when.
Test the race
Write a test or script that fires many simultaneous requests at the last seat and asserts exactly one succeeds. Run it against a real database, not a mock.
Common mistakes
- Reading availability in one request and writing in another.
- Relying on the front end to hide full dates.
- Never releasing abandoned holds.
- Counting cancelled bookings as taken.
- Time zones that shift dates for midnight bookings.
Frequently asked questions
Is a cache safe for availability?
Use it only for display. Final checks must use the database.
What about very high traffic?
Queues or reservation systems with stricter locking may be needed. For most small businesses, transactions are enough.
Should I overbook on purpose?
Some industries do, with clear policies. Most tour operators should not.
These patterns sit behind my booking and tourism builds.
See the tourism platform