The CAP Theorem
Overview
When your data lives on several machines (replicas), those machines sometimes lose contact with each other. The CAP theorem says that while this is happening, the system must choose: stay correct or stay responsive. It cannot be perfect at both.
This lesson gives you the vocabulary to describe that choice clearly.
Detailed Notes
What is CAP
CAP names three properties of a distributed data system:
- Consistency (C): every read returns the most recent write (or an error). Everyone sees the same latest data, as if there were one single copy.
- Availability (A): every request to a working node gets an answer (not an error), even if the answer might not be the latest.
- Partition tolerance (P): the system keeps working even when the network between nodes breaks or drops messages.
A network partition means the machines are alive but cannot talk to each other, for example a cut cable or a broken switch.
Note: this "C" is not the "C" in database transactions. Here it means replicas agree with each other.
The problem it solves
Replication (in the Redundancy and Replication chapter) puts copies of data in several places. That is great, but it raises a question: what should a copy do when it cannot reach the other copies? Without a clear answer, teams make vague promises like "our database is always correct and always available". CAP shows that such a promise is impossible during a partition.
How it works
Networks fail, so partitions will happen in any multi-machine system. That means P is not really optional. The real choice, when a partition happens, is:
- CP (consistency over availability): the side that cannot confirm the latest data refuses requests (or goes read-only). You never see wrong data, but some users get errors. Systems that use a quorum (previous chapter) behave this way: a node cut off from the majority stops accepting writes.
- AP (availability over consistency): every node keeps answering using its local copy. Users are never turned away, but data might be stale or two copies might disagree. After the network heals, the copies must be reconciled (merged).
When there is no partition, a system can offer both C and A. CAP only describes the failure moment.
Common misunderstandings:
- "Pick any two of three" is misleading. You cannot choose to skip P on a real network.
- "CA databases" only exist when nothing is distributed, such as a single-node database.
- The choice is not forever. Many systems let you choose per feature or even per request.
A simple example
Two bank branches share one ledger, and the phone line between them goes down. A customer asks Branch West to withdraw money.
- CP: the teller says "sorry, I can't verify your balance right now." Safe, but the customer is unhappy.
- AP: the teller pays out based on the last known balance and sorts it out later. The customer is happy, but if Branch East also paid out, the account could go negative.
System design example
An online store keeps stock in an East and a West data centre. The link between them breaks and a customer in West tries to buy the last item.
- For payments and stock counts, choose CP: show "please try again" rather than sell the same item twice or charge twice.
- For the shopping cart, likes, and activity feeds, choose AP: always accept the action and merge later. A cart that briefly shows an old item is better than a cart that does not work.
In interviews, say it per feature: "payments are CP, the feed is AP."
Summary
- A partition (machines cannot talk) will happen, so the real choice is C or A during that time.
- CP refuses some requests to stay correct. AP keeps answering and fixes disagreements later.
- CAP's consistency means all replicas show the latest write, which is different from transaction consistency.
- Decide per feature, not per whole company.
Architecture Diagram
When the link fails, West must choose between refusing and risking divergence.
- 1A client writes to the West replica.
- 2The link to East is cut, so West cannot confirm the write with East.
- 3CP: West rejects or delays the request to stay consistent.
- 4AP: West accepts the write and reconciles with East after the link heals.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

