Skip to content
Lesson 40

Design the booking engine

Redesign StayHub's core after a sale went wrong three ways: why search and booking need different rules, how to sell the last room exactly once, and why the payment must stay outside the lock.

The lesson in text

Click a paragraph to hear it.

01Saturday's sale

Monday, nine o'clock. Karim, the CTO, is reviewing Saturday's sale with the team. It went wrong in three different ways at once.

The main database hit 95% of its capacity, and every page slowed down. The last room in Porto was sold twice, to two different guests. And one guest was charged for a booking that doesn't exist.

The next sale is in five weeks, and StayHub plans for ten times the traffic. Karim wants a new booking engine before then, and you lead the design. This is StayHub's heart: if it's wrong, guests have nowhere to sleep and money disappears.

02The window and the till

Think of a shop. The window shows what's for sale. It's rearranged every so often, so sometimes it shows a jacket that sold an hour ago. Nobody is hurt: you look, you walk in, you ask.

At the till, it's different. Before taking your money, the clerk checks the stockroom. If the jacket is there, she puts it behind the counter with your name on it, while you find your card.

A booking engine has the same two halves. Search is the window: lots of people looking. Booking is the till: a few people paying. And the stockroom is the inventory database, the one true count of rooms left.

Two paths, two rules
Search: the window
  • · ≈ 180 searches a second at a sale
  • · ≈ 16,000 availability questions a second
  • · Reads a copy in Redis, refreshed within seconds
Can lie a little: it only shows.
Booking: the till
  • · ≈ 2.5 bookings a second at a sale
  • · One hold that checks and takes at once
  • · Payment outside the lock, then a short confirm
Can't lie: it decides.
The stockroom (the inventory database) only sees the few guests who book; the many who look read the copy.

The rule of this lesson fits in one line: search can lie a little; booking can't.

03Seventy lookers for every buyer

Now the numbers, at ten times today. About 300,000 guests a day, each searching around six times: 1.8 million searches a day. That's about 18 a second, and ten times more at the peak of a sale: 180 a second.

Each search checks about 30 hotels for three nights. So at a sale, the system answers around 16,000 availability questions every second.

Bookings are tiny in comparison: 25,000 a day, about two and a half a second at the peak. For every guest who books, about 70 only look. That's the look-to-book ratio.

The estimate at ten times StayHub's size
searches: 300,000 guests × 6 = 1.8 million a day÷ ~100,000 s ≈ 18 a second; sale peak ×10 ≈ 180
lookups: 180 × 30 hotels × 3 nights ≈ 16,000 a secondtoo many for the inventory database: use a copy
bookings: 25,000 a day≈ 2.5 a second at the peak: strictness is cheap here
look-to-book: 1.8 million ÷ 25,000 ≈ 70 to 1
Seventy lookers for every buyer: the load is in search, the risk is in booking.

Now Saturday's first problem makes sense. Every search asked the inventory database directly. Thousands of lookers crowded into the stockroom, and the database hit 95%.

05The last room, sold twice

Saturday's second problem happened at the till. Two guests clicked Book on the last double room in Porto, five milliseconds apart.

The old code first asked the database: how many rooms are left? One, it told both guests. Then each request said: good, take it. Two bookings, one room. Checking and taking were two separate steps, and the other guest slipped in between.

The last room in Porto: check, then take (broken) vs one step (fixed)
Before: two steps
Guest A
Database
Guest B
how many left?
how many left?
1
1
take it → booked
take it → booked (sold twice)
After: one step
Guest A
Database
Guest B
hold it, if one is free
hold it, if one is free (waits)
held ✓ (10 minutes)
nothing taken → similar rooms
When checking and taking are two steps, the other guest slips in between. One statement that does both lets the database decide.

The fix is to check and take in one single step. One database statement says: hold this room, but only if at least one is still free. The database runs them one at a time for the same room. The first guest gets it. For the second, nothing changes, and the code sees that no room was taken.

That's the hold: the jacket behind the counter. The room is set aside for ten minutes while the guest pays. About 30% of guests abandon checkout, so expired holds are released automatically, and the room goes back on sale.

You met this in the lessons on transactions and locking. The database decides who gets the last room, and it keeps its lock for milliseconds, not seconds.

06Never pay inside the lock

Saturday's third problem: the guest charged with no booking. At 10:04, the old code opened a transaction, locked Porto's nights, and then called the payment provider, the company that charges cards.

The provider was slow. After ten seconds, StayHub gave up, cancelled its own transaction, and told the guest: payment failed, try again. But the charge had already gone through at the provider. Cancelling inside StayHub's database can't undo a charge at another company.

So the guest tried again and was charged a second time. Two charges, one room, and a refund that took five days. Meanwhile, every other Porto booking waited ten seconds behind the lock.

The new flow keeps them apart. First the hold, in a short transaction. Then the payment, outside any transaction. Then a second short step that turns the hold into a sale, saves the booking, and announces it, so the copy and the other sites are updated.

The new booking flow
Guest
Booking engine
Inventory DB
Payment provider
Book room, 3 nights
1 · hold (short transaction)
held for 10 min
2 · charge, key auth-{holdId} (no lock held)
authorised
3 · hold → sold, booking saved, event sent
confirmed ✓
Only the hold and the confirm touch the database lock, for milliseconds each. The slow, outside call sits between them.

And the payment carries a reference built from the hold, like a ticket number. If StayHub asks twice with the same reference, the provider charges once. That's an idempotency key.

A timeout now means unknown, not failed. StayHub keeps the hold, shows the guest "confirming", and asks the provider about that same reference until it knows.

Saturday 10:04: a payment timeout, old code vs new
MomentOld codeNew flow
The provider doesn't answer in 10 sRolled back, "payment failed, try again"Keep the hold, show "confirming"
The card was in fact chargedCharged with no bookingAsking by the same key finds the charge → confirm
The guest retriesA second charge (new request)Same key: the provider charges once
Other Porto guestsWaited 10 s behind the lockNo lock held during payment

07What the design can't promise

Is overbooking now impossible? Inside StayHub, yes. But hotels also sell their rooms on other sites, and those sites learn about a StayHub sale thirty seconds to two minutes later. The window shrinks. It never closes.

So the rest is a business decision. Karim says: publish every room everywhere; an empty room is money lost forever. If a hotel overbooks, it sends the guest to another hotel. That costs the hotel about €150 and a one-star review.

Omar, the on-call engineer, suggests: for small hotels, keep the last room off the slow sites. The cost: sometimes that room stays empty. For a hotel with twenty rooms, Omar's way wins. For two hundred rooms, Karim's often does.

The window StayHub can't close: other sites learn 30 s to 2 min late
PolicyGainCostFits
Every room on every site (Karim)No room left emptySometimes a guest is moved: ~€150 and a one-star reviewBig hotels (200 rooms): the last room is a tiny share
Last room off slow-syncing sites (Omar)Most cross-site overbookings disappearSometimes that room stays emptySmall hotels (under ~20 rooms)
Inside StayHub the hold makes double-selling impossible. What other sites sell late is a business policy, and a nightly check compares bookings, charges and other sites.

And every night, a check compares bookings, charges and what the other sites sold, so anything that slipped through is found before the guest arrives.

08Your job as lead

When Claude Code works on the booking engine, ask three questions. First: does anything book against the copy, or does every booking re-check the real inventory?

Second: are the check and the take one single statement, and is there a test where two guests race for the last room? Ask to see it pass many times in a row, because a race that passes once proves very little.

Third: is the payment call outside every transaction, with the same key on every retry? And on a timeout, do we keep the hold, or give the room away?

Let's recap. Search is the window: it reads a copy and can lie a little. Booking is the till: one hold that checks and takes at once, the payment outside the lock with a stable key, then a short confirm. What other sites do late is a business policy. Next, a new module: why every feature starts with a problem.

Check yourself

0/10 answered
  1. 1Guests search about 70 times for every booking. Where must availability be strictly correct?

  2. 2What caused Saturday's 95% database load?

  3. 3Old code: first read how many rooms are left, then take one. Two guests click Book on Porto's last room 5 ms apart. What happens?

  4. 4Claude Code proposes that checkout reads "rooms left" from the Redis copy to save a database query. What do you say?

  5. 5What is a hold?

  6. 6Claude Code writes: open a transaction, lock the nights, call the payment provider, then commit, "for atomicity". What's wrong?

  7. 7The payment provider doesn't answer within 10 seconds. What should StayHub do?

  8. 8Why does the payment carry a key built from the hold?

  9. 9Search shows "1 room left" from the copy, but the room sold seconds ago. The guest clicks Book. What should a good engine show?

  10. 10A 12-room hotel also sells on a site that learns about StayHub sales two minutes late. Which policy fits best?

Free account

Save your progress — create a free account

Free. No card. Your finished lessons follow you to any device, and you can ask questions in the community.

Create my free account
Founding members

Every lesson is free while we build. Lock in €9/month for later.

The first 100 members keep the founding price for as long as they stay. Reserving is free: no card, nothing is charged.

See the founding offer

Questions from the community

All questions →