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
Speed
Lab time 3.0 s
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
Night by night
Night
Bookings
Rooms
Rooms left, says the row
Result
Night 1
16
20
4
on 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
Time
Bookings
Rooms left, says the row
1s
2
18
1s
5
15
2s
8
12
2s
10
10
3s
14
6
3s
16
4
What just happened
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.