CodingNodes Space play · ←→ step · R restart

The problem

Sell a concert ticket and you decrement a fixed counter: the arena has so many seats, and once they are gone they are gone. Sell a hotel room and you do the opposite of that. A room is not consumed — it is loaned out for a stretch of time, and then it comes back. The same room is sold, again and again, once per non-overlapping stay. That single difference — inventory that is a function of a date range, not a fixed number — is what makes a reservation system hard, and it is what every decision below turns on.

The hard part is not the volume; it is the overlap. A booking is valid only if its nights do not overlap any booking that is already held or confirmed on that room. Two guests who pick the same room on the same weekend, at the same moment, will both pass a naive 'is it free?' check and both be booked — and the hotel now owes two guests one room. We will build the availability check as a date-range overlap, the hold that reserves a room before it is paid, the lost- update race and the compare-and-set that stops it, and the lifecycle that moves a room from available to held to confirmed to released.

Requirements

Functional — what it must do

  • Search: a guest picks a hotel and a date range, and the system returns which room types are free over those nights, at what rate.
  • Hold: a guest can reserve a room type for the range without paying yet, locking that availability for a short window while they finish checkout.
  • Commit: the hold is confirmed into a reservation, or it lapses and the room goes back to being bookable — never stuck.
  • Release: a cancellation or a completed stay frees the room's nights for future bookings.

Non-functional — how well it must do it

  • No double-booking: two concurrent guests cannot both be confirmed on the same room type for overlapping nights, no matter how they interleave.
  • A hold is cheap and bounded: it locks availability with a TTL of minutes, not a permanent lock, so a guest who abandons checkout does not strand a room.
  • The overlap check is exact across the range, not an approximation: a booking that ends the morning a stay begins must not block it, and two stays that share even one night must not both succeed.
  • It stays correct under the flash sale: a popular hotel and a holiday weekend produce a burst of simultaneous holds that the inventory check has to serialize without losing either guest's money.

Scale — how much it must take

  • On the order of hundreds of thousands of rooms across thousands of hotels, and millions of holds per day at peak (a holiday weekend), with the per-room-type overlap check on the critical path of every search.

Assumptions — the numbers this design is sized for

A rate limit means nothing without a scale. These are the figures every later part relies on: how much traffic arrives, how much work the limiter has to do per second, and how much latency and memory it may spend doing it.

Rooms
~400K across hotels
The unit of inventory is the room type, not the physical room — ten identical king rooms are one inventory of ten.
Hold
~15 min TTL
Long enough to finish payment, short enough that an abandoned checkout does not strand a room for a night.
Nights
per-night granularity
A stay is [check-in, check-out); a room is free on a night if no held or confirmed booking covers that night.
Overlap
exact, inclusive
Two stays sharing even one night conflict; a checkout morning before a check-in does not.
Holds/day
millions (peak)
A holiday weekend produces a burst of simultaneous holds; the overlap check sits on the critical path of each.
Price
per date range
The rate is set when the hold is placed and held fixed until confirm or lapse — it is not re-priced mid-hold.

Takeaways

Part 1

Inventory over time, not a fixed counter

A ticket is a seat; a room is a stretch of time. Sell a ticket and you decrement a number. Sell a room and you book a range — and the same room is sold again and again, once per non-overlapping stay.

  1. 1
    The room comes back

    The defining difference between the two: a seat is gone once it is sold, but a room is returned the morning after checkout. So the inventory of a room is not a number you decrement — it is a function of a date range, recomputed over the stays that overlap it. That is why 'how many are left' is the wrong question; the right question is 'is this range free?', and it has to be answered for every candidate room type on every search.

  2. 2
    Double-booking is the failure to design against

    Everything else in a reservation system is normal infrastructure. The one thing that is not is the double-booking: two guests both pass the 'is it free?' check, both are confirmed, and the hotel now owes two guests one room. That is the correctness target of the whole design — not throughput, not latency, but the invariant that the same room type is never confirmed to two parties for overlapping nights. Every part of the system is either on the path to upholding that invariant, or off it.

Part 2

A booking is a date range

A booking is a room type, a party, a rate, and — the part that makes it a reservation — a range of nights. Availability is defined over that range, which is why the check is an overlap test, not a count.

  1. 1
    What a booking carries

    A booking is a room type, a guest, a rate, and the range: a check-in night and a check-out morning. The range is the load-bearing field. It is what the availability check tests against, it is what the hold locks, and it is what two bookings compare to decide whether they conflict. Get the range right and the rest of the system is bookkeeping; get it wrong and the invariant breaks.

  2. 2
    Free means non-overlapping, for every night

    A room type is free over a range if, and only if, no held or confirmed booking covers any night inside that range. That is a universal condition over the nights, not a single comparison. A booking that ends the morning a stay begins does not overlap — they can share the room. Two stays that share even one night do overlap — they cannot. The availability check is the statement of that condition, and it is the query the whole system is built to answer correctly under concurrency.

Part 3

The availability check

The check is a range-overlap query against the room type's held and confirmed bookings. It is exact by construction — it tests every night — which is what makes the later compare-and-set safe.

  1. 1
    A query over the room type's bookings

    To answer 'is this room type free over [check-in, check-out)?', the system looks at that room type's held and confirmed bookings and asks whether any of them overlaps the range. It is not a count of 'rooms left' — there is no such number, because the same room is reused. It is a set of ranges, and the query is an overlap test against that set. That is why the data model is a list of (room type, range, state) and not a counter.

  2. 2
    Why a per-night counter would lie

    A tempting shortcut is to keep a per-night free count and decrement it. It breaks exactly at the edges a hotel runs into: a stay that spans nights touches several of them, a checkout morning and a check-in on the 'same' night do not conflict, and a hold that lapses has to give the count back. The counter is an approximation of the range test, and it is wrong wherever the range is not a single night. The overlap query against the actual bookings is exact by construction, so it is the one we build the invariant on.

Part 4

The hold — reserve before you commit

Payment takes time and can fail, so a room cannot be confirmed the instant it is found. The hold locks the range for a short window, lets the guest finish checkout, and hands the room back if the window lapses.

  1. 1
    Lock the range, keep the window short

    The hold is a soft reservation: the room type's range is marked as taken by this guest, and it stays marked for a bounded window — fifteen minutes, say. During that window no other guest can be booked into the overlapping nights, because the hold is in the same inventory as the confirmed bookings. When the guest confirms, the hold becomes a confirmed reservation. When the window lapses without a confirmation, the hold is dropped and the range goes back to being free. The room is never stuck.

  2. 2
    The hold and the confirm share one inventory

    The subtlety is that a hold and a confirmed booking occupy the same inventory. The overlap check treats a held range exactly as it treats a confirmed one, so a guest who is holding a room blocks the same nights as one who has already paid. That is what makes the hold meaningful: it is not a separate 'wishlist' layer, it is a real claim on the inventory, distinguished from a confirmation only by its TTL and its pending payment.

Part 5

The double-booking race

Two guests pass the 'is it free?' check in the same window, before either commits. Check-then-act is the classic lost-update. The fix is to fold the check into the commit, so that only one of them can win.

  1. 1
    Check-then-act loses an update

    Booking A checks the range and finds it free. Before A commits, Booking B checks the same range and also finds it free — because A's commit has not happened yet. Both A and B then commit, and the same nights are now booked to two guests. Nothing in either guest's transaction was wrong; the failure is the gap between the check and the commit, and it is the gap that has to be closed.

  2. 2
    Fold the check into the commit

    The fix is to make the check and the commit one atomic step: a compare-and-set that says 'claim this room type for this range, but only if no booking overlaps it', executed by the storage layer in a single operation. Now A's claim succeeds and B's compare-and-set sees A's booking and fails, or vice versa. Only one can win, because the decision is made at the moment of commit, not at the moment of the earlier, stale check. The race is closed by construction, not by hoping the two requests do not interleave.

  3. 3
    The loser is told, not silently dropped

    A compare-and-set that fails is not an error to swallow — it is the answer 'that range is no longer free.' The service returns it to the guest as a stock-out, with the option to pick different dates or a different room type. The guest who lost the race is shown a live, current state, not a stale one from their earlier check. That is the difference between a system that is occasionally wrong and one that is always consistent and just sometimes 'sold out'.

Part 6

The lifecycle and the move of inventory

A room type's nights move through a small state machine: available, held, confirmed, released. The inventory is allocated at the hold, and it is released by a cancellation, a lapse, or a completed stay.

  1. 1
    The states, and where the inventory sits

    The lifecycle is short: available, held, confirmed, released. The inventory sits in the held state while the guest finishes checkout, moves to confirmed when payment lands, and returns to available on a cancellation, a lapse, or after the stay is checked out. Every transition is one of two moves — allocate the range, or release it — and the whole system is the management of those two moves under concurrency.

  2. 2
    The price is captured at the hold

    Rates are date-dependent — the same room is different prices on different weekends, and the rate a guest is quoted is the rate for their specific range. The rate is therefore captured the moment the hold is placed and held fixed until the hold is confirmed or lapses. A guest is not re-priced between finding a room and paying for it, and the bill at confirm is the price of the hold, not a fresh lookup. That keeps the 'price' and the 'availability' locked together for the duration of the hold.

  3. 3
    The whole lifecycle in one frame

    Put the states in a line and the design is clear. A hold allocates the range; a confirm moves it to the guest; a cancellation or a lapse, or the completion of the stay, releases it back to the pool. The overlap invariant is upheld at the allocation and the claim, and the lifecycle is just the bookkeeping that keeps the inventory honest while the room is in a guest's name.

★ Finale

End to end

One booking's life: searched, held, claimed against a rival, confirmed, checked in, checked out — with the invariant that the range was never confirmed to two guests held at every step.

  1. 1
    The whole system

    Every piece in one frame. A guest searches; the reservation service runs the overlap check against the room inventory; a hold allocates the range; a rival's claim is resolved by the compare-and-set; a confirm commits the hold; and the lifecycle moves the room from held to confirmed to released. One range, one guest, at every step.

  2. 2
    The hold, against a rival

    A guest places a hold on a room type for their range. At the same moment, a rival guest is booking the overlapping nights. Both claims reach the compare-and-set, and exactly one wins: the winner's range is allocated, and the rival is shown a live stock-out. The invariant held through the race — the same nights are held by exactly one guest.

  3. 3
    The hold becomes a booking

    The winner's hold is confirmed: payment lands, and the held range becomes a confirmed reservation. The rate is the one captured at the hold, and the room's nights are now owned by the guest. A rival's hold, if it had won, would have done the same thing — the path is the same, only the winner differs.

  4. 4
    Checked in, checked out, released

    The guest checks in, stays, and checks out. At checkout the range is released back to the pool, and the room type is free to be booked again for future dates. The room was loaned, not sold — and the invariant that it was never confirmed to two guests for overlapping nights held from the first hold to the last release.

🌐 In the wild

In the wild

Reservation systems are the core of every travel and lodging platform. The boxes below are anchored to the classes of well-known systems that run each piece — an illustrative mapping, not a claim about any one product's internals.

  1. 1
    The pattern, piece by piece

    Each box is a standard building block of a reservation system. Anchoring them to the classes of production systems that run that piece turns the abstract design into the shape behind the 'book now' button that has to be right for every guest, on every popular weekend.

  2. 2
    Why the same pieces keep winning

    Independent lodging platforms converge on the same pieces because each one removes a real failure: the date range removes the 'sold a room twice to the same night' error, the overlap check removes the stale-availability error, the hold removes the 'payment failed but the room is gone' error, and the compare-and-set removes the double-booking race. That convergence across independent systems is the evidence the design is load-bearing.