Skip to content
Lesson 50

CAP and consistency

Why a booking vanished from "My bookings" though it was safely saved, what copies trade away every day and when the network splits, and how to choose a guarantee for each piece of data.

The lesson in text

Click a paragraph to hear it.

01The booking that vanished

Wednesday, 9:20. Maya forwards a support ticket. Sofia, a regular guest, booked a hotel in Porto. Then she opened My bookings, and the list was empty. So she booked again.

She's not alone. Since Monday, 63 guests have booked the same room twice. Omar checks the database: both of Sofia's bookings are there, saved correctly. Nothing failed.

So how can a booking be saved, and missing, at the same time? In this lesson, you'll learn why copies of data show the past, what they must choose when the network splits, and how to pick the right promise for each kind of data.

02Copies

StayHub now has about 150,000 guests a day. At the busiest moments, search asks the database about 3,000 questions a second: is this room free on these nights? One database machine can't answer all of that and take bookings too.

So there's one main database, the primary, which takes every change. And three copies, called replicas, which answer the reading. Copying data to other machines is called replication.

Here's the catch. The primary confirms a booking first, and sends the change to the copies just after, without waiting for them. That's asynchronous replication. For a moment, the copies are behind.

That delay is the replication lag. Most of the day it's about 50 milliseconds. But every morning at nine, hotels import their new prices, the copies get busy, and the lag reaches 2.5 seconds.

Asynchronous replication: the copies trail the primary
Sofia
Primary
Replica (copy)
book Porto, 2 nights
Confirmed ✓
+0.5 s: My bookings?
(none): the copy is 2 s behind
+2.5 s: the booking arrives
The primary confirms Sofia's booking, then streams the change to three copies without waiting. Lag is about 50 ms most of the day and up to 2.5 s during the 9:00 price imports, so My bookings, read from a copy half a second later, can show the past.

Now Sofia. At 9:20, her booking is saved on the primary. Half a second later, My bookings loads, from a copy that's two seconds in the past. In that past, she hasn't booked yet.

03The box office

Picture a big concert. One box office sells the tickets and keeps the master seating chart. Around the venue, three information kiosks hold copies of the chart, so fans can ask about seats without queuing at the box office.

After each sale, a runner carries the update to the kiosks. On a quiet day, the copies are seconds behind. On a busy day, minutes.

Sofia buys seat 14 at the box office, walks to a kiosk, and asks: is my ticket there? The runner hasn't arrived yet. No ticket, says the kiosk. So she goes back and buys again.

Nobody lied, and nothing broke. The kiosk answered from a copy of the past. That's the price of copies: they're fast and close, and they're never quite now.

04When the network splits

Lag is the everyday problem. There's a worse day. Imagine the runner can't get through at all: the corridor is closed. The kiosks keep working, but they can't hear from the box office.

With computers, that's a network partition: the machines are all running, but some can't reach the others. In the lesson on partial failures, you met the silence. A partition is the same silence, between your own copies.

During a partition, each copy that's cut off has only two choices. Refuse to answer until it can check: that keeps the data consistent. Or answer anyway, and maybe be wrong: that keeps the system available.

That's the CAP theorem, first stated by Eric Brewer in 2000. When the network splits, you can't have both: you choose consistency or availability. And across machines, splits will happen, so a system that claims to be both is only saying it hasn't decided.

The CAP choice, in one picture
Choose consistency
The cut-off side refuses: “try again in a minute”.
Never wrong; some requests fail while the split lasts.
Choose availability
The cut-off side answers from what it has.
Always answers; some answers are wrong and must be fixed later.
On normal days the same trade is smaller: a fresher answer or a faster one (PACELC).
During a network partition, the machines run but some can't reach the others. A cut-off copy must refuse (consistent) or answer and maybe be wrong (available). Partitions happen across machines, so “both” only means nobody decided.

And on normal days, the same trade comes back in a smaller form: a fresher answer, or a faster one. Sofia's problem wasn't a partition. It was that everyday trade, made without thinking about her.

05Price a stale read

So which is right, fresh or fast? Neither, for everything. You decide per piece of data, with one question: what does it cost if this answer is a few seconds old?

Rooms left, at the moment of booking: a stale answer sells the last room twice, and a guest is turned away at the desk. That needs strong consistency: the primary decides.

My bookings and the guest's profile: a stale answer makes the owner think her change was lost, so she does it again. That needs a narrower promise: you always see your own changes.

Reviews, hotel descriptions, search results: if they're a few seconds old, nobody notices. That's eventual consistency, the same catching up you met in the lesson on events, and it's cheap.

Price a stale read, per piece of data
Rooms left, when booking
The last room sold twice; a guest turned away
Strong: the primary decides
My bookings, profile
The owner thinks her change is lost and does it again
Read-your-writes
Reviews, descriptions, search
Nobody notices a few seconds
Eventual: copies and caches
The guarantee follows the cost of an answer that is a few seconds old, not how important the data feels. Strong consistency costs load and speed, so spend it only where being wrong costs money or trust.

You've seen this rule in the booking engine design: search can lie a little, booking can't. Now you know why. It's not about how important the data is. It's about what being wrong costs.

06Read your own writes

Back to the kiosk. The fix is simple: if you've just bought a ticket, the kiosk phones the box office before answering you. Everyone else still reads the copy.

StayHub does the same. That promise is called read-your-writes. After Sofia books, her session remembers a position in the primary's change log, for 10 seconds. In that window, her own data is read from a copy only if it has caught up past that position. Otherwise, from the primary.

Read-your-writes, step by step
Sofia books → primary at log position 0/7A41F0the write
session: remember 0/7A41F0 for 10 sher receipt
My bookings: copy replayed ≥ 0/7A41F0 ? copy : primaryinside the window
after 10 s: any copyas before
Only the guest who just wrote pays for the check, and only for 10 seconds. Everyone else, and Sofia after the window, reads the copies as before.

Only the guest who just wrote pays for the check, and only for a few seconds. The 3,000 questions a second still go to the copies.

Why not simply make the primary wait for all three copies before confirming? Then every booking waits for the slowest copy: seconds, at nine in the morning. And if one copy is cut off, nobody can book at all. One guest's confusion would become everyone's problem.

07The quick win

A different team found a quick win on a Friday: check rooms left on a copy, not the primary. Checkout got faster, from about 140 milliseconds to 90.

Monday, 9:05, during the price imports. Hotel Ribeira in Porto has one room left. Two guests book it a second apart. The copy is 2.5 seconds behind, so both checks see one room. Both guests get confirmed. One is turned away at the desk.

In one week, it happened at 14 hotels. A check on one machine and a booking on another aren't protected by one transaction. Each machine only guards what it can see.

The fix: the primary decides, in one step. Take the room only if rooms left is still above zero. Copies are for showing. The primary is for deciding.

08Refuse or risk it

Now the bad-day choice, for real. StayHub is moving room inventory to three machines in three separate data centre zones. A booking counts when two of the three agree: a majority.

If one zone is cut off, it can't reach a majority. Karim wants that side to keep accepting bookings: refusing is revenue walking away. We'll fix overbookings by hand.

Lina disagrees. An overbooking is a guest turned away and a relocation StayHub pays for. She'd rather show: try again in a minute.

The team sides with Lina, for inventory. Partitions last minutes, a few times a year, so the loss is small and bounded. Hotels overbook when they choose to, not when StayHub surprises them. If partitions were long and frequent, the answer could flip, and that's worth writing down.

Inventory across three zones: refuse or risk it
Zone A
majority: books
Zone B
majority: books
Zone C
cut off: refuses
Karim: accept everywhere, fix overbookings by hand. Revenue keeps flowing.
Lina (chosen): refuse on the cut-off side. An overbooking is a guest turned away and a relocation we pay for.
A booking counts when 2 of 3 machines agree. If zone C is cut off, it can't reach a majority. StayHub refuses there (“try again in a minute”) rather than risk selling a room twice. Partitions last minutes, a few times a year; if they were long and frequent, the answer could flip.

09Your job as lead

You ask Claude Code to fix the vanishing bookings. It proposes to send every read to the copies, including the rooms-left check, and explains: the copies are only 50 milliseconds behind.

Usually 50 milliseconds hides the 2.5 seconds at nine, exactly when the doubles happened. Ask it to keep the rooms-left decision on the primary, add read-your-writes for My bookings and the profile, list every query it moves, and test with three seconds of fake lag.

For any change that reads data, ask four questions. What does it cost if this answer is three seconds old? Who decides: the primary, or a copy? What does a guest see right after her own change? And when the network splits, does this refuse, or risk being wrong, and who chose?

Let's recap. Copies trade freshness for speed every day, and when the network splits, each copy must refuse or risk being wrong. So price a stale read for each piece of data: strong for what decides, read-your-writes for a guest's own data, eventual for the rest. Next: a phone loses signal mid-payment, the app retries, and the bank shows the same charge twice.

Check yourself

0/10 answered
  1. 1Bookings are written to the primary; My bookings reads an asynchronous copy. Why was Sofia's list empty half a second after booking?

  2. 2What is replication lag?

  3. 3During a network partition, a copy that's cut off is asked a question. What are its real choices?

  4. 4Which StayHub data needs strong consistency?

  5. 5What does read-your-writes promise?

  6. 6Why not make the primary wait for all three copies before confirming each booking?

  7. 7A "quick win" moves the rooms-left check to a copy. Hotel Ribeira has one room; two guests book a second apart while the copy is 2.5 s behind. What happens?

  8. 8Claude Code proposes sending every read to the copies, including the rooms-left check: "the copies are only 50 ms behind". What do you answer?

  9. 9In the inventory cluster, one zone is cut off from the other two. What does StayHub's cut-off side do with bookings?

  10. 10A teammate says: "Our database picked consistency and availability, so partitions don't affect us." What's wrong?

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 →