Design a Hotel Reservation System: Concurrency Control, Optimistic Locking & Overbooking
Preventing Double-Booking Under Flash Traffic
- Two guests book the last remaining room simultaneously (double-booking).
- Clicking "Confirm Booking" twice on flaky mobile Wi-Fi charges the guest twice (duplicate reservation).
Hotel Reservation Concurrency Architecture
1. Key Data Modeling Choice: Reserve Room Types, Not Physical Room Numbers
room_type_inventory tracks (hotel_id, room_type_id, date, total_inventory, total_reserved) rather than individual room rows—shrinking table size by 20× and simplifying range updates cleanly.Supporting Controlled Overbooking (e.g., 10%)
sqlCHECK (total_reserved <= FLOOR(total_inventory * 1.10))
2. Three Ways to Solve Double-Booking Concurrency
| Strategy | Mechanism | Pros | Cons |
|---|---|---|---|
Pessimistic Locking (SELECT ... FOR UPDATE) | Holds DB row lock during transaction | Simple, impossible to overshoot | Contention / deadlocks under slow clients |
Optimistic Locking (version column) | UPDATE ... SET reserved = reserved + 1, version = version + 1 WHERE version = :v | Zero long-held locks; fast under low/medium contention | Needs client retry on 0 rows affected |
Database Constraint (CHECK + Atomic UPDATE) | UPDATE room_type_inventory SET total_reserved = total_reserved + 1 WHERE ... AND total_reserved + 1 <= total_inventory | Single atomic SQL statement; ultra-fast | Coupled to SQL row update |
Recommendation for Production
UPDATE / Optimistic version lock for the reservation step with an Idempotency Key (reservation_id UUID generated on the checkout screen) enforced via a UNIQUE index on reservations(reservation_id) so network retries never create duplicate bookings!3. Caching Room Search Without Stale Inventory Disasters
- Serve hotel search (
GET /v1/hotels/search) from read replicas + Redis Inventory Cache synced asynchronously via Change Data Capture (Debezium + Kafka) from the primary Reservation DB. - If a room shows available in Redis from a 500ms stale cache just as someone else grabs the last unit, the final ACID check in the primary DB rejects the checkout cleanly with
"Room just sold out"—never risking an inconsistent write!