Lesson 2 · Minutes 10-20

Hotel Reservation: First Design

Problem

The board gets one database between minute ten and minute twenty, and every question goes to it. A search adds up reservations. A booking adds them up too, and if a room is free, it inserts another. Nothing is derived yet, so nothing can disagree.

You owe the interviewer three things in these minutes: a search and a booking walked through, a straight account of why this is a fair answer, and the exact place where it fails the question. That place is a gap inside the transaction, and you point at it first. The quiz asks what the interviewer will ask, so answer each one before you read its options.

One Database, Nothing Derived

Clients reach stateless application servers through a load balancer. A single relational database stores hotels, room types and reservations. There are no counts and no copies. What is free gets worked out from the reservations each time someone asks.

The card stays off the board, and you say so: "Treat the card as a box for now: the booking commits, the card is charged next, and a decline cancels it. I'll come back to what that window costs."

The first design
Clientsearch or book Load balancerany server App serversstateless One databasenothing derived search or book count each night insert reservation readwrite

One Search, One Booking

A search names a city and dates. For each hotel and type in that city, the server counts the reservations covering each night of the stay, and takes the busiest night away from the type's room count. What remains is what is free. Those are the 200 checks from Scope.

A booking is one transaction. It runs that same count for the type the guest wants, and if a room is free, it inserts the reservation and commits. A cancel marks the reservation cancelled, so the next count no longer sees it. Listing your reservations is a query by guest on the same database.

The Case for One Store

Search, booking and cancelling all run on one store with no derived data, so availability can never disagree with the reservations. It is correct while two bookings for one type and night do not overlap, and at peak a hotel sees one booking every 1,000 seconds, against a transaction of milliseconds. For a first few hotels, you could ship this.

Say that much out loud, then say at once that the question said never. That turns the overlap into the first limit, not a footnote.

The Gap in the Transaction

Two guests press Book on the last room. Each transaction counts the reservations and finds one room free, which is true when it counts, because neither can see the other's uncommitted insert. Each then inserts its own new row, and no row conflicts, so both commit. At read committed or snapshot isolation, where databases usually run, nothing went wrong inside either transaction, and the room is sold twice.

Serializable isolation would abort one of the two, and leave a retry on every conflict across tables that search is reading too. The next lesson settles the race with one row instead.

The gap: the last room, two transactions
Guest A countsone room free
→
Guest B countsone room free
→
A insertscommits
→
B insertscommits

What You Name and Leave

Four limits go on the board, unfixed. The check and the insert have a gap between them. A repeated request books again. Every search runs on the store bookings write, at up to 160,000 checks a second. And one database is one failure for 8,000 hotels.

Hand over the worst two yourself, in about ten seconds: "The transaction doesn't close the gap between the count and the insert, and the question says never, so I'd fix that first. After that the limit is search: at 160,000 checks a second, bookings start waiting behind searches on the same tables."

Interview Pitfalls
  • Saying "it's in a transaction, so it's safe." The interviewer asks what the second guest's count saw, and the design has no answer.
  • Adding a count table, a cache and shards before one booking has been walked end to end.
  • Charging the card inside the booking transaction, which keeps locks open across a call to an outside party.
What Interviewers Listen For
  • One search and one booking walked through, with a finger on the exact gap in the booking.
  • Payment parked by name, along with what is being deferred.
  • The limit stated at 160,000 checks a second, and how it would show up.

Knowledge Check

"Two guests press Book on the last room in the same millisecond. Walk me through your transaction: who gets it?"

  • "The transaction covers it: one of them commits and the other then sees no room left."
  • "Both can pass the check, so I keep a count per night and lower it only if rooms remain."
  • "Take a short lock in the cache on the hotel and night before the check and the insert."
  • "Let both commit, compare the two afterwards, and cancel the one that arrived second."

"The guest double-clicks Book, and two identical requests reach two different servers. What stops a second reservation?"

  • "One request ID sent by the page, held unique in the reservations table."
  • "A button that greys out on the first click, so that no repeat is ever sent."
  • "A rule that treats one guest's booking for the same dates as a duplicate."
  • "A hash that sends each guest to one server, which remembers their requests."

"Searches run at a hundred times bookings, and they read the tables your bookings write. Where do those reads belong?"

  • "On better indexes over the same tables, so that each search holds them for less time."
  • "On read replicas, along with the availability check each booking runs before it commits."
  • "On a copy of availability that may lag, while each booking keeps deciding on the store."
  • "On the same store, with a limit on search traffic to keep a share free for bookings."

"Thousands of hotels, times four room types, times 365 nights. You are asked to split this data. By what, and what for?"

  • "By night, so far-off dates sit on cheap nodes and the next few weeks on fast ones."
  • "By guest ID, so that one guest's stays sit together and load in a single read."
  • "By hotel ID, less for the bytes than to keep one failure from stopping every hotel."
  • "By a hash of hotel, room type and night, so the count rows spread out evenly."

You got correct