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.
- · ≈ 180 searches a second at a sale
- · ≈ 16,000 availability questions a second
- · Reads a copy in Redis, refreshed within seconds
- · ≈ 2.5 bookings a second at a sale
- · One hold that checks and takes at once
- · Payment outside the lock, then a short confirm
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.
searches: 300,000 guests × 6 = 1.8 million a day÷ ~100,000 s ≈ 18 a second; sale peak ×10 ≈ 180lookups: 180 × 30 hotels × 3 nights ≈ 16,000 a secondtoo many for the inventory database: use a copybookings: 25,000 a day≈ 2.5 a second at the peak: strictness is cheap herelook-to-book: 1.8 million ÷ 25,000 ≈ 70 to 1Now 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%.
04Search reads a copy
The fix for the window: search reads a copy. A fast store, Redis, keeps the rooms left and the price for each hotel, room type and night. The sixteen thousand questions a second go to the copy. The stockroom only sees the two or three guests a second who actually book.
The copy is updated each time something sells, within seconds. As a safety net, every entry also expires after a minute, so a missed update can't stay wrong for long.
Sometimes the copy says one room left, and it has just sold. That's fine, because the copy only shows; it never decides. The guest clicks Book, the real check happens at the till, and the guest sees: just sold out, here are similar rooms.
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 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.
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.
| Moment | Old code | New flow |
|---|---|---|
| The provider doesn't answer in 10 s | Rolled back, "payment failed, try again" | Keep the hold, show "confirming" |
| The card was in fact charged | Charged with no booking | Asking by the same key finds the charge → confirm |
| The guest retries | A second charge (new request) | Same key: the provider charges once |
| Other Porto guests | Waited 10 s behind the lock | No 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.
| Policy | Gain | Cost | Fits |
|---|---|---|---|
| Every room on every site (Karim) | No room left empty | Sometimes a guest is moved: ~€150 and a one-star review | Big hotels (200 rooms): the last room is a tiny share |
| Last room off slow-syncing sites (Omar) | Most cross-site overbookings disappear | Sometimes that room stays empty | Small hotels (under ~20 rooms) |
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 answered1Guests search about 70 times for every booking. Where must availability be strictly correct?
2What caused Saturday's 95% database load?
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?
4Claude Code proposes that checkout reads "rooms left" from the Redis copy to save a database query. What do you say?
5What is a hold?
6Claude Code writes: open a transaction, lock the nights, call the payment provider, then commit, "for atomicity". What's wrong?
7The payment provider doesn't answer within 10 seconds. What should StayHub do?
8Why does the payment carry a key built from the hold?
9Search shows "1 room left" from the copy, but the room sold seconds ago. The guest clicks Book. What should a good engine show?
10A 12-room hotel also sells on a site that learns about StayHub sales two minutes late. Which policy fits best?
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 accountEvery 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