0% completed
CAP Theorem
On This Page
A Network Partition
Consistency
Availability
Partition Tolerance
"Pick Two of Three" Is the Wrong Way to Say It
What the Choice Looks Like to a User
Most of the Time There Is No Partition
Systems Called CP and Systems Called AP
Key Takeaways
A distributed system keeps its data on more than one machine, and those machines talk over a network. Networks fail. The CAP theorem describes what a system can still promise when that happens.
The short version is this. When the network between the machines breaks, a distributed system can keep answering every request, or it can keep every answer correct. It cannot do both.
The name comes from three words: consistency, availability, and partition tolerance. This lesson explains each word, and then explains why the real choice is only between two of them.
A Network Partition
A network partition is a break in the network that stops some machines from reaching others. The machines are still running. They just cannot talk.
Picture five machines that hold copies of the same data. One network link fails. Three machines end up on one side and two on the other. Users can still reach both sides. Neither side can send anything to the other.
Now a user writes a new value to a machine on the left side. The machines on the right side never hear about it. A user reading from the right side gets the old value.
That is the whole problem. Everything else in this lesson is about what you decide to do in that moment.
Consistency
Consistency means every read returns the most recent write, no matter which machine answers. The system behaves as if there is only one copy of the data.
Be careful with this word. The C in ACID is a different idea. It is about database rules, such as a balance never going below zero. The two are not related. ACID vs BASE covers that one.
Availability
In the CAP theorem, availability means that every machine that is still working gives an answer. A machine that is up must not refuse, and must not wait forever. It is a yes or a no, not a percentage.
The Key Characteristics of Distributed Systems lesson uses this word for something else. There, availability is the percentage of the promised time that the system can answer, such as 99.9 percent. Both meanings are common, so it helps to say which one you mean.
The difference matters. One of them is a number you report every month. The other is a property that either holds or does not hold during one broken minute.
Partition Tolerance
Partition tolerance means the system keeps operating correctly when the network loses messages between machines. It does not promise that every request is answered. A partition tolerant system may refuse requests on one side of the break, but it must not lose track of its data or give two answers that it claims are both current.
This one is not like the other two. Consistency and availability are promises you choose to make. Partition tolerance is a fact you have to accept. You cannot ask a network not to fail. Cables break, switches restart, and links between data centres go down.
So any system that runs on more than one machine has to deal with partitions. There is no version that opts out.
"Pick Two of Three" Is the Wrong Way to Say It
You will often read that CAP means picking two of the three properties, and that the options are CA, CP, and AP. That sentence causes more confusion than it removes.
CA would mean a system that keeps consistency and availability by not tolerating partitions. No such distributed system exists. When the network breaks, that system still has to answer or refuse. So it still makes the choice.
A database on a single machine is a different case. It has no network between machines, so it has no partition to survive. It is also not a distributed system, so CAP does not describe it. Old versions of this picture label a relational database as CA for that reason, and it misleads people. A single MySQL server is not a point on the triangle at all. A group of MySQL machines with replicas is distributed, and it faces the same choice as everything else.
Here is the accurate statement:
Partition tolerance is required. During a partition you choose between consistency and availability. When there is no partition, you can have both.
What the Choice Looks Like to a User
A system that chooses consistency stops answering on at least one side of the partition. A machine that cannot check with the others will refuse the request or hold it. The user sees an error, or a request that takes a long time. No user ever sees a wrong value.
A system that chooses availability answers from whatever copy it has. The user always gets a response. That response may be old, and two users on different sides may see different values. When the network comes back, the copies must be merged, and some rule decides which write wins.
Neither choice is better in general. It depends on the data.
For a bank transfer, an error is much better than a wrong balance. For the number of likes on a video, a count that is a few seconds old is fine, and an error page is not.
Most of the Time There Is No Partition
Partitions are real, and they are also rare. A healthy network inside one data centre partitions very seldom.
When there is no partition, a system can be consistent and available at the same time. CAP does not forbid it. The theorem describes one moment only: the moment the network breaks.
That leaves an obvious gap. What does a system trade off during all the normal time? The PACELC Theorem lesson answers that question.
Systems Called CP and Systems Called AP
Some stores are usually described as CP: HBase, ZooKeeper, etcd, and Google Spanner. During a partition, the side that cannot reach a majority stops serving. You get an error instead of an old value.
Others are usually described as AP: Cassandra, Amazon DynamoDB, Riak, and CouchDB. During a partition, every reachable node keeps answering from its own copy. The copies are repaired afterwards.
One warning about these labels. Most real databases are configurable, not fixed.
Cassandra is called AP, and you can still require a read and a write to reach a majority of replicas. Those operations then behave like a CP system. DynamoDB is called AP, and it also offers a strongly consistent read. MongoDB behaves differently depending on its write and read settings.
The tool behind all of this is the quorum, which is the number of replicas that must confirm an operation before it counts. Quorum covers how the numbers are chosen.
So a label like AP describes a default, not a law. The real behaviour is often picked per operation.
Key Takeaways
- A network partition is a break in the network between machines that are still running. Both sides stay alive and cannot talk to each other.
- Consistency in CAP means every read returns the most recent write. It is not the C in ACID.
- Availability in CAP means every working machine gives an answer. It is a yes or a no. The percentage meaning belongs to Key Characteristics.
- Partition tolerance is not optional. Any system on more than one machine has to survive a broken network.
- "Pick two of three" is the wrong way to say it. There is no CA distributed system, and a single-machine database is not on the triangle at all.
- Choosing consistency looks like an error or a wait. Choosing availability looks like an answer that may be old.
- With no partition, a system can be consistent and available. The choice only appears while the network is broken.
- CP and AP are defaults, not laws. Most databases let you pick the behaviour per operation with a quorum setting.
In an interview, do not say "this is an AP system" and then stop. Say what a user sees during the partition, and say what happens to the two copies after the network comes back. That second part is the one people forget, and it is where the real work is.
Marc Schlossberg
· 9 months ago
In the earlier section 'Key Characteristics of Distributed Systems', it reads, 'By definition, availability is the time a system remains operational to perform its required function in a specific period. However, in this section it reads "Availability means every request received by a non-failing node in the system must result in a response."
One of these is a metric and the other is a non-functional requirement. This is a big difference! It's exactly the kind of difference that an interviewer will notice because it telegraphs that the candidate doesn't truly understand what they're talking about.
Marc Schlossberg
· 9 months ago
The diagram inaccurately shows RDBMS as the CA combo and NoSQL solutions for both CP & AP. In reality, there are no true CA options in a distributed system, RDBMS's are better for CA & NoSQL for AP.
Additionally, the last paragraph's usage of the word partition is confusing and inaccurate. At times it is correctly used to reference a network failure between nodes, but then other times it's used to refer the nodes themselves?
Simon Gigli
· a year ago
Why can't a system guarantee consistency and availability?
Let's say a node goes down, then wouldn't the system be able to return data from an active node? This would mean the system is available (able to return requests) and consistent (all users see the same up-to-date data).
designgurus
· 2 years ago
The diagram says RDBMS is C/A, but if it's at all distributed it's C/P
Aditya Bhave
· 2 years ago
It seems to me that unless the system is a read-only service (in which case, full data replication can be done across every node), you cannot really have CP. If the system is partitioned due to network loss, writes to one partition cannot be seen on the other partition till the network is restored. In other words, while the network is lost, consistency is lost
Reading Progress
0%
On This Page
A Network Partition
Consistency
Availability
Partition Tolerance
"Pick Two of Three" Is the Wrong Way to Say It
What the Choice Looks Like to a User
Most of the Time There Is No Partition
Systems Called CP and Systems Called AP
Key Takeaways