Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
The CAP theorem in plain English: consistency, availability and partition tolerance, why you choose CP or AP during a partition, plus PACELC and examples.
Quick answer: The CAP theorem says a distributed data store cannot guarantee all three of consistency (every read sees the latest write), availability (every request to a working node gets a response) and partition tolerance (the system keeps operating when the network between nodes fails). Since network failures will happen, partition tolerance is not optional. So the real choice is this: during a partition, do you refuse some requests to stay consistent (CP), or answer every request and risk returning stale or conflicting data (AP)?
The everyday meanings of these words are looser than the theorem's.
The idea was proposed by Eric Brewer in 2000 and proved formally by Seth Gilbert and Nancy Lynch in 2002. The Wikipedia article has the history.
Take two nodes, A and B, each holding a copy of the data. The network link between them breaks. That is a partition.
x = 5 to node A.x from node B.Node B has only two choices:
There is no third option. B cannot know about a write it never received.
CAP is often drawn as a triangle with "pick any two". That suggests you could choose CA and skip partition tolerance. In a system spread across a network, you cannot: cables are cut, switches fail, and a long garbage collection pause is indistinguishable from a network failure. Partitions happen whether you tolerate them or not.
A single-node database is "CA" only in the trivial sense that it has no network between replicas, and when that one node is down, it is simply unavailable.
A better statement: when a partition occurs, choose between consistency and availability.
A CP system prefers refusing requests to giving wrong answers. Typically, only the side of the partition that holds a majority of nodes continues to accept writes. The minority side rejects requests until the network heals.
An AP system answers every request, even if nodes cannot talk to each other. Both sides of the partition accept writes. When the network heals, the system must reconcile the differences.
The reconciliation is where the complexity goes. See eventual consistency explained.
An ATM network. Suppose an ATM loses contact with the bank.
Real banks often choose the second, because a limited overdraft costs less than angry customers. The business decides how much inconsistency is tolerable.
DNS. When a record changes, resolvers around the world keep serving the cached old value until it expires. DNS is always available and only eventually consistent. See how DNS works.
CAP only describes behaviour during a partition, which is rare. Most of the time the network is fine, and a different trade-off applies. The PACELC formulation adds it:
If there is a Partition, choose Availability or Consistency; Else, choose Latency or Consistency.
To guarantee consistency, a write must be confirmed by several replicas before it is acknowledged, and reads may need to check with a majority. That takes time. Skipping the coordination is faster but allows stale reads. This latency cost is paid on every request, so in practice it shapes system design more than partition behaviour does.
| System | During a partition | Otherwise |
|---|---|---|
| Cassandra, DynamoDB (default settings) | Availability | Low latency |
| Spanner, etcd, CockroachDB | Consistency | Consistency (at a latency cost) |
| MongoDB (default settings) | Consistency | Tunable |
Martin Kleppmann's article Please stop calling databases CP or AP explains why the labels are too crude:
Treat CAP as a way of starting the conversation, not as a classification scheme.
For how data is copied between nodes in the first place, see how database replication works.
Consistency, Availability and Partition tolerance.
Not a distributed one. Network partitions cannot be ruled out, so you must decide how to behave when one happens.
With default settings it behaves as CP: during a partition, only the side with a majority elects a primary and accepts writes. Its read and write settings can relax this.
No. Many offer strong consistency as an option, and several distributed SQL databases provide it by default. CAP describes a trade-off, not a property of any category.
The CAP theorem captures one unavoidable fact: when nodes cannot communicate, a system must either stop answering or risk answering wrongly. Which is worse depends on the data. Use CAP to frame that decision, PACELC to remember the everyday latency cost, and your database's real documented behaviour to make the final call.