Skip to content
BytePatterns

Design Hotel Booking

System Design Cases: lesson 19 of 20

Four nights or none — a partial stay is not a booking.

Lesson 19 of 20 · 7 min

Design Hotel Booking

Step 1 of 10

A stay is not a night, it is a range of them — and every one has to be secured together.

The Idea

Sell a room for a date range exactly once. Two guests pressing book in the same second must not both win, and a stay only exists if every one of its nights was secured together.

Real-World Example

A wall planner with a row per room and a column per night. A four-night stay is four squares shaded at once, and a pen that can only reach three of them has not booked anything.

BEGIN
  UPDATE availability SET booked = booked + 1
  WHERE room_type = ? AND night IN (n1..n4) AND booked < total
  -- rows updated must equal nights requested, else ROLLBACK
  INSERT booking(...)   -- unique (room, night)
COMMIT

The Tradeoff

One transaction across the range makes a partial booking impossible and holds locks that the next booking wants, so contention grows with the length of the stay. Under that, a unique constraint on room and night refuses a duplicate even when the logic above it is wrong.

Your turn

Put the steps in the right order.

  1. Commit, so the guest holds every night or none of them
  2. Open a transaction over every night in the requested range
  3. Insert the booking row, which the unique constraint guards
  4. Roll back unless each night still has a room free

Mini quiz

1 / 3

Every night of a stay is written in one transaction because:

New lessons land every few weeks

Leave an address and we will tell you when the next one is up. That is the only reason we will use it.

One address, stored so we can email you. Nothing else, ever.