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, but they cannot send messages to each other.
Imagine five machines that hold copies of the same data. One network link fails. Three machines are now on one side of the break and two are on the other. Users can still reach both sides, but 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 receive that write. 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. ACID is a set of four guarantees for database transactions, and the C in ACID is a different idea. It is about database rules, such as a balance never going below zero. The two meanings are not related. ACID vs BASE covers the C in ACID.
Availability
In the CAP theorem, availability means that every machine that is still working gives an answer. A machine that is running must not refuse a request, and must not make the request wait forever. This kind of availability 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. The percentage is a number you report every month. The CAP meaning is a property that is either true or false during one minute when the network is broken.
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 keep its data correct and must not give two answers that it claims are both current.
Partition tolerance is not like the other two properties. Consistency and availability are promises you choose to make, but 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 centers fail.
So any system that runs on more than one machine has to deal with partitions. No such system can avoid them.
"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, a system labeled CA must also either answer or refuse. So the partition forces it to choose between availability and consistency, like any other system.
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 the CAP diagram, a triangle with C, A, and P at its corners, label a relational database as CA for that reason, and that label misleads people. A single MySQL server has no place on the triangle at all. A group of MySQL machines with replicas is distributed, and it must make the same choice as every other distributed system.
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 make it wait. 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 the copy that each machine 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 works again, the copies must be merged, and a rule decides which write is kept.
Neither choice is better in general, because the right choice 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 center is very rarely partitioned.
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.
CAP therefore says nothing about normal operation. What does a system trade off while the network is healthy? The PACELC Theorem lesson answers that question.
Systems Called CP and Systems Called AP
Some data stores are usually described as CP: HBase, ZooKeeper, etcd, and Google Spanner. During a partition, the side that cannot reach a majority of the machines stops serving requests. 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 machine that a user can reach keeps answering from its own copy. The copies are repaired afterward.
These labels need one warning: most real databases are configurable, not fixed.
Cassandra is called AP, but you can require a read and a write to reach a majority of replicas, which are the machines that hold copies of the data. Those operations then behave like a CP system. DynamoDB is called AP, but it also offers a strongly consistent read, which returns the most recent write. MongoDB behaves differently depending on its write and read settings.
All of these settings use the quorum, which is the number of replicas that must confirm an operation before the operation is treated as successful. Quorum covers how the numbers are chosen.
So a label like AP describes the default behavior, not a fixed rule. The real behavior is often chosen for each operation.
Key Takeaways
- A network partition is a break in the network between machines that are still running. Both sides keep running but cannot send messages 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 comes from the Key Characteristics lesson.
- 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 means the user sees an error or a wait. Choosing availability means the user sees an answer that may be old.
- With no partition, a system can be consistent and available. The choice is needed only while the network is broken.
- CP and AP are defaults, not fixed rules. Most databases let you choose the behavior for each 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 works again. That second part is the one people forget, and it is the harder part of the design.
Marc Schlossberg
· a year 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
· a year 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
· 2 years 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