Case study demo

Hotel reservation

Book rooms without double-booking when requests race each other.

An interactive demo of how this system behaves. It runs in your browser.

Case study · Live model

How a room booking behaves

A hotel sells 20 rooms for each night, and more guests than that want one. The count of rooms left is one row in a database table. For each guest the service reads it, then writes one fewer. A guard is a rule that keeps two guests from doing this at the same moment. Take the guard away and guests get rooms that do not exist. Hold a lock too long and guests leave while rooms are free.

Try it

Lab time 3.0 s
read, writeGuests, 8 a secondGuests8 a secondnight 1 is on saleBooking service, check the versionBooking servicecheck the version1 bookings openRow lock, not used, IDLERow locknot usedno guest waits hereIDLERooms table, night 1: 4 leftRooms tablenight 1: 4 left16 bookings
  • Guests — 8 a second — healthy — night 1 is on sale
  • Booking service — check the version — healthy — 1 bookings open
  • Row lock — not used — IDLE — no guest waits here
  • Rooms table — night 1: 4 left — healthy — 16 bookings
  • A guest presses Book
  • Read the rooms left
  • Write the new count and the booking
  • Wait for the lock
  • Booking confirmed
  • No room for this guest
Booked
16/ 17
guests with a confirmed booking
Overbooked
0
bookings past room 20 of a night
Sold out
0
guests told no room is left
Writes refused
16
0 guests told to try again later
Gave up in line
0
waited 3 s for the lock, then left
Time to answer
272 ms
from Book to yes or no, last 3 s

Numbers add up:17 guests = 16 booked + 0 sold out + 0 gave up + 0 told to try again + 1 in progressThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

The guard
How the service books a room

Each write says which version of the row it read. If the row has changed since, the write is refused and the guest starts again.

The rush
8 a second
0.10 s

The time the service spends on the price and the payment, after it has read the rooms left.

A new night goes on sale every 8 s. Guests who are part-way through a booking finish it for their own night.

Night by night

NightBookingsRoomsRooms left, says the rowResult
Night 116204on sale
The night on sale: bookings, and rooms left by the row's count
BookingsRooms left, says the row20 rooms
06.312.518.8251s2s3s
Show data table
TimeBookingsRooms left, says the row
1s218
1s515
2s812
2s1010
3s146
3s164

What just happened

  1. 0.3sWarning: A write was refused: it had read version 1, and the row is now at version 2.
What this model simplifies
  • Every guest wants one room for one night, and each night has one row: "rooms left". A real hotel has room types, stays of many nights, and one count for each type and night.
  • One read or one write takes 20 ms, and a new night goes on sale every 8 s. Real sales are slower, but the race is the same.
  • With the version check, a guest whose write is refused reads the row again. After 3 refused writes the guest is told to try again later, even if rooms are still free.
  • A guest waits at most 3 s for the lock. The lock here lives in the database, on one row. A lock shared by many servers has more ways to fail, such as a holder that crashes.
  • An overbooked guest is counted, not helped. A real hotel must find that guest another room or pay the money back.
  • Nobody cancels, and a booking is never held while the guest fills in a form. Real systems hold a room for some minutes and free it if the guest does not pay.