SevyDevy

Module 5 · When Copies Disagree · Lesson 22 of 35

What is CAP theorem? Choosing what to do when the line is cut

13 minFree System design course · Beginner to intermediateLesson 5.3By Saket Bhatnagar

Researched and drafted with AI, reviewed and edited by Saket Bhatnagar.

In short

  • What is CAP theorem? It says when copies of data cannot reach each other, a system must refuse some requests to stay correct, or answer and risk stale data.
  • Links do break, so the real choice is made per feature, only while one is down.
  • Even with nothing broken, a stronger answer waits for a farther copy, so it is slower.
Soon

Video lesson · coming soon

What is CAP theorem? When the line is cut

Lala, your AI mentor, will walk you through this lesson in a short animated video. Until then, everything is here to read and try.

  1. 01 Both sides up, the link is cut
  2. 02 Two counters, one dead phone line
  3. 03 The majority side sells
  4. 04 Answer or refuse, feature by feature
  5. 05 Three plus three minus five
  6. 06 Recap, then draw your policy

SeatJaldi runs in two regions, with data centres in Mumbai and Hyderabad, on a ₹3 lakh monthly cloud bill. On Tuesday evening, a cable fault cuts the link between them.

Arjun Nair checks every screen, and nothing is down. Both data centres are up and serving fans. SeatJaldi now keeps 5 copies of the seat data, up from lesson 17’s primary and two replicas: 3 in Mumbai, 2 in Hyderabad.

“Keep selling or stop?” Meera Kulkarni asks. “What do I tell users?” Arjun is ready. “Let us pick two of the three CAP letters for the whole app, like in the slides.” Zoya Mirza shakes her head. “Per feature, not per app. What should each feature do while the link is cut?”

“If both sides sell seat A12,” Arjun says, “we could sell it twice, like those 37 seats. If both stop, nobody can book. Is there something in between?”

What is CAP theorem?

The CAP theorem is the full story behind lesson 21’s warning that strong answers can be refused. When copies of data cannot reach each other, a system must choose. It refuses some requests to stay correct, or answers them and risks stale data. The break has a name.

The letters stand for consistency, availability and partition tolerance, and the words are strict. Consistency means strong consistency: every read sees the latest finished write. Availability here is stricter than in lesson 5: every request reaching a working machine gets a real answer.

Keep one line in your head: when copies cannot talk, each feature must choose, answer or refuse.

  • Answer: reply from the copy you have, and accept that it may be old.
  • Refuse: say “please try again shortly”, and stay correct.
  • The choice exists only while the link is cut, and each feature makes its own.

A cut link and a majority of three

Mumbai holds 3 of the 5 copies, Hyderabad holds 2, and the link is cut. Only the majority side sells seats; both sides keep showing matches.

Diagram connections:

  • Mumbai side fans to Mumbai data centre (Browse and buy)
  • Hyderabad side fans to Hyderabad data centre (Browse, not buy)
  • Mumbai data centre to Cut link (Cannot reach)
  • Hyderabad data centre to Cut link (Cannot reach)
  • Mumbai data centre to Majority rule (Counts 3 of 5)
  • Mumbai side fans: Fans served by Mumbai.
  • Hyderabad side fans: Fans served by Hyderabad.
  • Mumbai data centre: 3 of the 5 copies: the majority side.
  • Hyderabad data centre: 2 of the 5 copies: the minority side.
  • Cut link: The network between the data centres is down.
  • Majority rule: Only the side with 3 of 5 copies may confirm a seat.

No. Mumbai holds a majority, more than half the copies, so it may confirm seats. Hyderabad keeps showing matches but refuses to sell, because it cannot know what Mumbai sold a second ago.

  • A booking app on weak signal says “Cannot confirm right now, please try again”. The server refused rather than risk a wrong answer.
  • On a train with no signal, you add a dress to a wishlist, and it appears later on your laptop. Your phone answered, and the copies merged.
  • A cricket app shows a score “updated 2 minutes ago”. An old answer was given on purpose, and labelled.

What does CAP theorem look like in real life?

It looks like a feature-by-feature policy for the minutes when the line is cut. At Sunanda’s counter, reading the timetable is fine and selling a berth is not. She may still take your name for a waiting list.

Try it yourselfAnswer or refuse? Six SeatJaldi featuresThe link is cut, and you are the Hyderabad side with 2 of the 5 copies. Choose what each feature does.
SeatJaldi’s partition policy, written by Zoya
FeatureMajority side (Mumbai)Minority side (Hyderabad)Why
Buy a seatSellsRefuses, says try againOr the seat sells twice
Browse matchesShowsShowsA slightly old list harms nobody
WishlistSavesSaves here, merges laterNothing limited is spent
PaymentCharges once the seat is heldDoes not chargeNo money without a held seat
Score bannerShowsShows, marked “updated 2 minutes ago”Old news is labelled, not hidden
Loyalty pointsAddsRecords, adds laterPoints can be merged

Once Hyderabad refuses a booking, SeatJaldi is not available in CAP’s strict sense, even while Mumbai sells. That is fine. CAP does not say you must stay available. It tells you what each choice costs.

What is a quorum?

A quorum is the smallest group of copies that must agree before an answer counts, usually a majority. It is how Mumbai knows it may sell.

Now say the railway has five counters, three in Dadar and two in Secunderabad. A booking counts only if three agree. When the line dies, Dadar can still go on; Secunderabad cannot.

  • A majority of 5: 5 / 2 = 2.5, so the smallest group bigger than half is 3.
  • Two groups of 3, picked from the same 5 copies, share at least 3 + 3 - 5 = 1 copy.

What just happened? The shared copy is the referee. Any second group of 3 meets a copy that saw the first sale, so seat A12 cannot be confirmed twice.

Each side counts the copies it can still reach.

What does PACELC add when nothing is broken?

PACELC adds that even with no network partition, a stronger answer costs latency. Read the name as a sentence: if Partition, choose Availability or Consistency; Else, choose Latency or Consistency.

Even with the phone line working, a call takes time. A counter that rings the other town before every sale is always right, and always slower.

A round trip inside one data centre takes about 0.5 ms, the usual order of magnitude. Say Mumbai to Hyderabad takes about 30 ms (assumed, not measured). A strongly consistent read, which must check the other region, costs about 30 / 0.5 = 60 times the local trip.

How do real systems keep one side in charge?

  • A primary chosen by majority vote: with Raft, 5 servers keep working if 2 fail. If more fail, they stop rather than answer wrongly.
  • Read and write quorums: with 5 copies, write to 3 and read from 3. 3 + 3 = 6, more than 5, so every read meets the latest write. Overlap alone is not strong consistency; concurrent writes need a vote.
  • Three places, not two: if Mumbai itself goes dark, Hyderabad has 2 of 5 and nobody sells. With 2 + 2 + 1 copies in three places, losing any one place still leaves 3 or 4.

Spanner, a distributed SQL database, gives strong answers across regions by waiting for a majority of copies, often elsewhere, on every write.

json
{
  "buy_seat": "majority_only",
  "payment_capture": "after_seat_held",
  "browse_matches": "always_answer",
  "wishlist": "answer_then_merge",
  "live_score": "answer_label_old",
  "loyalty_points": "answer_then_merge"
}

What just happened? Each feature names its behaviour during a network partition, and the app follows it when it reaches fewer than 3 of 5 copies. To test, cut the link in a test setup, try each feature on both sides, and check no seat sold twice.

Try it yourself

  1. Pick three features

    Write the minority-side rule for (a) cancelling a booking, (b) the seat map, (c) a one-time coupon.

  2. Answer or refuse

    For each, write answer or refuse, and one line on why.

  3. Use the test

    Could both sides say yes and make a mess that cannot be undone? Then refuse.

  4. Check your answer

    (a) Refuse: both sides could refund one booking. (b) Answer, marked “last updated”: booking checks again anyway. (c) Refuse: the coupon could be used once on each side. You are right if every feature where two yeses cannot be undone is refused, and the rest answers.

Back to Tuesday evening. Zoya’s policy goes live: Mumbai sells, and Hyderabad shows “Booking is paused for a few minutes, please try again”.

When the cable is repaired, the copies compare notes. No seat was sold twice. Fans who waited were never charged.

So there was something in between: a rule per feature, like the notice at each railway counter. Meera tells users booking may pause, but browsing never stops. Then Zoya spots the next problem. The booking path is correct but slow: each booking waits for payment, ticket PDF, SMS and email. Checkout takes 6 seconds.

Check yourself

Check yourself

A teammate says, “CAP means we pick two of three, so let us pick CP for the whole app.” Best reply?

Pick an answer to see why it is right or wrong.

Check yourself

5 copies, 3 in Mumbai and 2 in Hyderabad, and the link is cut. Why can only Mumbai confirm seats?

Pick an answer to see why it is right or wrong.

Try it yourselfModule 5 recap: when copies disagreeSix situations from this whole module. Pick the best move and read why.

Remember these three

  1. When copies cannot talk, each feature must choose: answer or refuse.
  2. Only a majority side may confirm something limited, because two majorities always share a copy.
  3. PACELC: even with no network partition, a stronger answer costs latency.

Questions people ask

What is CAP theorem in simple words?
When copies of data cannot reach each other, a system must refuse some requests to stay correct, or answer and risk stale data. It is a choice made during a network partition, not a menu of two from three.
Can you give a CAP theorem example in real life?
Two railway counters lose their phone line. If both keep selling, one berth may be sold twice. If one stops, nobody is cheated, but people walk away. A good policy treats each service differently.
What is a quorum?
A quorum is the smallest group of copies that must agree before an answer counts, usually a majority. With 5 copies, 3 is a majority, and two groups of 3 must overlap, so two sides cannot both confirm.
What does PACELC add to CAP?
It adds that even without a network partition, stronger consistency costs latency, because a read or write waits for a farther copy. During one, answer or refuse; otherwise, fast or fresh.