BOOK DEEP READ · System Design Interview Vol 2 · Ch 7

Hotel Reservation System

System Design Interview Vol 2 · Ch 7 · Alex Xu & Sahn Lam · 2022

中文 →

What is this chapter about?

You pick a room in a booking app and press "Reserve". In the second or two that follows, the system has to do something genuinely dangerous: confirm the room is still there, put it in your name, and not also sell that same room to the person pressing the same button right now. This chapter builds that system from scratch.

Here's the odd part

You probably assume the hard part is crowds. Do the arithmetic: five thousand hotels, a million rooms; even at 70% occupancy with an average three-night stay, that's only a bit over two hundred thousand bookings a day — about three per second. Three! Less traffic than a small forum's posts. The difficulty was never volume. It's that two of those three might be reaching for the same room, and getting it wrong costs real money.

Why the old world struggled

Picture the paper register at the front desk. A guest calls, the clerk turns to March 5th, sees one room left, writes down a name. The trouble is that in the same second a colleague is looking at the same page, also seeing "one left", also writing a name. Two people paid; there is one room. That's overselling. What's true of a paper ledger is exactly true of two servers reading the same row.

How it pulls this off

Step one: change what you sell. A hotel doesn't sell "room 301" — it sells "king room · March 5th · how many left", a single cell of allowance. Like a cinema selling "how many seats remain for Saturday night" and sorting out where you sit at the door. Finding an empty room among tens of thousands turns into subtracting one from a number.

Step two: govern that cell. Two ways to live. Either you hold the page face-down while you change it and nobody else may look until you're done — safe, but a queue builds up; or you note which revision of the page you read, check on writing whether that revision still holds, and if someone cut in front of you, start over — faster most days, but during a scramble you redo it again and again.

Step three: cure the other kind of double. You fumbled and clicked "Submit" twice. The fix is to hand you a ticket number when you open the booking page, and let one ticket number be redeemed exactly once — like taking a numbered slip in a queue: shout twice, you still count as one person.

So what should you pick?

For an ordinary hotel the second way (check the revision) is plenty, and you can even let the database itself guard the rule "never book more than you have". Only a scramble — the night before a concert, a whole city chasing one hotel — earns that cell its own queue. The price you pay: now and then you'll reach the last step and read "just taken". The system would rather waste one of your clicks than sell one room twice.

Remember this

A reservation system is hard not because of traffic (it's a few writes per second) but because of correctness over finite inventory: turn rooms into one cell of "room type × date", then guard that cell with "only one writer at a time" and "one ticket, one redemption".

Want the mechanisms, the notation and the diagrams? → Switch to the deep read