Skip to content
HLD Learn/Consistency & Availability
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

The CAP Theorem

5 min read

You'll learn to

  • -State the CAP theorem in plain words and define consistency, availability and partition tolerance
  • -Explain why partition tolerance is not optional, so the real choice is consistency or availability during a partition
  • -Walk through a partition between two replicas and see what a CP system and an AP system each do
  • -Choose consistency or availability for a given piece of data, and say what is traded when there is no partition

Replication, in the previous module, creates copies of your data for safety and read scale. It also raises a new question: what happens when the copies cannot talk to each other, even briefly? Cables get cut, switches fail, and a link between two regions can drop for seconds or minutes. The CAP theorem is the precise answer, and it is one of the most quoted ideas in system design, so it is worth understanding properly rather than as a slogan.

An Analogy: Two Bank Branches and a Broken Phone Line

A couple share an account with $100 in it. The account is kept in two branches, and the branches keep each other up to date by phone. Then the phone line between the branches goes down. The wife asks Branch A for $100, and at the same moment the husband asks Branch B for $100. Each branch has two options. It can refuse until the phone line is back, so that it never gives out money the other branch may already have paid. Or it can go ahead with its last known balance, so the customer is served, and sort out any overdraft later.

  • -The two branches are two replicas of the same data.
  • -The broken phone line is a network partition: the replicas are alive but cannot reach each other.
  • -Refusing until the line is back keeps the data correct (consistent) but turns a customer away (not available).
  • -Serving from the last known balance keeps every customer served (available) but the two branches may now disagree, so the account could go to minus $100 (not consistent).

The Statement

The CAP theorem says that when a network partition happens, a distributed data store has to choose between consistency and availability. Its three letters mean:

  • -Consistency (C): every read returns the most recent write, or an error. All clients see the same data at the same time.
  • -Availability (A): every request to a node that is working gets a real response, not an error, even though it might not be the latest data.
  • -Partition tolerance (P): the system keeps working even when messages between nodes are lost or delayed.

Why Partition Tolerance Is Not Optional

CAP is often shown as pick any two of three, which makes it sound like a menu. It is not. The moment your data lives on more than one machine, a partition is something that will happen: a failed switch, a congested link, a long pause that makes a healthy node look dead. You cannot promise it never occurs, so partition tolerance is not something you can choose to drop. A system that claims to be CA is really one that runs on a single machine, or one that simply stops working when a partition happens. The real decision is what to do when the partition arrives: protect consistency, or protect availability.

The diagram shows the situation: the link between the leader and its replica in another region has been cut.

Leader (Region A)
Replica (Region B)

A network partition splits the leader from a replica. Do you keep both sides serving traffic (favoring Availability), or halt the isolated side (favoring Consistency)?

Leader (Region A)Replica (Region B)- replication link - severed by the partition

Walking Through a Partition

Say a user's balance is 100 on both replicas, and then the link is cut. A client asks the isolated replica to withdraw 100, while another client asks the leader to withdraw 100.

  • -A CP system protects consistency. The isolated replica cannot confirm it has the latest data, so it returns an error (or waits) for the withdrawal. No overdraft is possible, but some requests fail until the partition heals. ZooKeeper and etcd lean this way.
  • -An AP system protects availability. Both sides accept the withdrawal, so everybody is served. When the partition heals, the balance has become minus 100 and the system must merge the two histories using some rule. Cassandra and DynamoDB lean this way, and both let you tune the behavior per request.

Choosing Per Piece of Data

The choice is not made once for a whole product. It is made for each kind of data, by asking which is worse: an error, or a wrong answer for a while?

  • -Account balances, the last seat on a flight, a unique username: a wrong answer causes real harm, so choose consistency and accept some errors during a partition.
  • -Like counts, view counts, a shopping cart, a social feed: a slightly stale answer harms nobody, but an error loses a sale or annoys a user, so choose availability and reconcile later.

One more thing the theorem does not say: even with no partition, there is a trade-off. Keeping replicas strictly in step means waiting for them, which costs latency. This extension is sometimes called PACELC: if there is a partition, choose availability or consistency; else, choose latency or consistency. The Wiki card below walks through CAP with real systems:

From the Wiki⚖️ CAP Theorem

In a distributed system, during a network partition, you must choose between Consistency and Availability - you cannot have both.

You run two bank branches. The network connecting them breaks (Partition). A customer deposits $1,000 at Branch A. Now Branch B is contacted for the balance. Do you: (A) show the correct $1,000 balance but pause service until sync completes (Consistency), or (B) immediately show the old balance but keep serving customers (Availability)? You just lived CAP Theorem.

CAP stands for Consistency, Availability, and Partition Tolerance. Proved by Eric Brewer in 2000, CAP Theorem states that a distributed data store can only guarantee two of these three properties simultaneously.

Consistency (C): Every read receives the most recent write or an error. All nodes see the same data at the same time. Reads are never stale.

Availability (A): Every request receives a (non-error) response, but it might not contain the most recent data. The system is always responsive.

Partition Tolerance (P): The system continues to operate despite arbitrary network partitions (messages being dropped or delayed between nodes).

The critical insight: in any real distributed system, network partitions are inevitable. Networks fail. Packets drop. Cables get cut. So P is not optional - you must tolerate partitions. This means your real choice is between C and A when a partition occurs.

  • -P (Partition Tolerance) is mandatory for real distributed systems.
  • -The real choice is CP vs AP during failures.
  • -CP: Bank transactions, inventory management, leader elections.
  • -AP: Social feeds, shopping carts, DNS, caching systems.
  • -PACELC is a more nuanced extension: even without partitions, there is a Latency-Consistency tradeoff.
Read the full Wiki deep-dive (+2 more)

In one line: partitions will happen, so the real question is what to do during one. A CP choice returns errors rather than wrong data, an AP choice stays up and reconciles later, and you decide per kind of data which is worse.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!