CAP and PACELC
What the CAP theorem really says, what a system must choose when the network splits, and how PACELC extends the trade-off to latency when nothing is broken.
The problem
A retailer runs its order database in two data centres, London and Frankfurt. Each holds a full replica, so that either can serve customers if the other fails. One afternoon a routing mistake cuts the link between them. Neither site is down. London’s customers can reach London and Frankfurt’s can reach Frankfurt, but the two replicas cannot talk to each other.
A customer in London buys the last unit of a product. Should London accept the order? If it does, Frankfurt may sell the same unit to someone else, because nobody can tell it. If it refuses, a healthy site is turning away a paying customer because of a fault somewhere else.
Engineers looked for a design that avoided the choice: redundant links, faster failure detectors, cleverer replication protocols. Redundant links make partitions rarer but never impossible, and the question of what to do during one remains.
In 2000 Eric Brewer, in a keynote at the PODC conference, conjectured that the choice cannot be avoided. Seth Gilbert and Nancy Lynch of MIT proved a formal version in 2002. It is known as the CAP theorem.
Step 1 of 8
Two replicas hold the same value, x equals 1. Each client talks to the replica nearest to it, and the replicas keep each other up to date over the network.
Read the steps as text
- Two replicas hold the same value, x equals 1. Each client talks to the replica nearest to it, and the replicas keep each other up to date over the network.
- On a healthy network a write is easy. Client 1 sets x to 2 on replica A. A copies the change to B and only then confirms. Both replicas agree, and both stay available.
- Now the link between the replicas fails. Both replicas are still running, and each can still hear its own client. Neither can tell whether the other has crashed or is merely unreachable.
- Client 1 asks replica A to set x to 3. A cannot copy the change to B, so it has exactly two options. The CAP theorem says there is no third.
- Option one is to refuse. A returns an error because it cannot keep B in step. No client ever sees conflicting data, but a healthy replica has turned a request away. This is choosing consistency, the CP behaviour.
- Option two is to accept. A stores 3 and confirms. Client 2 then reads from B and gets 2, a stale answer. Every request was answered, but the replicas now disagree. This is choosing availability, the AP behaviour.
- When the link returns, an AP system has to reconcile. If B also accepted writes, something must pick a winner or merge the values, and a write may be lost. A CP system has nothing to repair, because the refused write never happened.
- CAP is only about that moment. With no partition you can have both consistency and availability, and the trade becomes consistency against latency. Real systems with three or more replicas let the majority side carry on and stop the minority.
What CAP actually says
The theorem is about three properties, each defined more strictly than in everyday speech.
- Consistency means linearizability: every read returns the most recent completed write, as if there were a single copy. See consistency models.
- Availability means every request received by a node that has not failed gets a response that is not an error. Not most requests, and not eventually.
- Partition tolerance means the system keeps its guarantees even when the network drops or delays any number of messages between nodes.
The theorem: while a partition lasts, no system can provide both consistency and availability. A node that is cut off and receives a write must either accept it, in which case the other side does not know and the system is not consistent, or refuse it, in which case it is not available.
Why partitions are not optional
“Pick two of three” suggests that you could pick consistency and availability and do without partition tolerance. For a system that runs on more than one machine, you cannot. Switches fail, cables are cut, firewall rules are misconfigured, and cloud zones lose contact with each other. From the inside, a partition is also indistinguishable from slowness. A node that pauses for 30 seconds of garbage collection looks the same as one that is unreachable, because all its peers ever observe is a timeout.
So read CAP this way: partitions will happen, and when one does, you choose between consistency and availability.
CP and AP behaviour, concretely
CP: refuse instead of being wrong. etcd, ZooKeeper and Consul, and consensus-based databases such as Spanner and CockroachDB, need a majority of replicas to commit a write. Split a five-node cluster into three and two, and the three carry on. The two cannot elect a leader or commit anything, so their clients get errors or timeouts. Nothing inconsistent is ever acknowledged.
Choose CP when a wrong answer costs more than no answer: locks and leader election, account balances, stock that must not be oversold, unique usernames.
AP: answer instead of being unavailable. Dynamo-style stores such as Cassandra and Riak, at low consistency settings, let any reachable replica accept a write. Both sides of the partition carry on, and the replicas diverge. After it heals, they reconcile. Cassandra keeps the write with the latest timestamp for each column. Riak can keep both versions as siblings for the application to merge. Amazon’s Dynamo paper of 2007 used the shopping cart as its example: “add to cart” must always succeed, and divergent carts are merged later, with the admitted side effect that a deleted item can reappear.
Choose AP when no answer costs more than a slightly wrong one: baskets, feeds, likes, presence, metrics and DNS.
For the retailer, the answer can differ by operation. Browsing and adding to a basket should stay available. Taking the last unit of stock should either go through a CP store or stay available with a business remedy for the rare oversell, such as an apology and a refund. Which of those is right is a product decision as much as a technical one.
Cut the link in the simulator, write to one side and read from the other in each mode, then heal the partition and see what has to be reconciled.
Common misreadings
- “CAP consistency is ACID consistency.” It is not. CAP’s C is linearizability. ACID’s C is about invariants within a transaction.
- “AP means highly available, and CP means it goes down.” CAP availability is all or nothing and concerns every non-failed node. A CP system can still deliver 99.99% uptime, because partitions are rare and the majority side keeps serving. An AP system can have poor uptime for unrelated reasons.
- “Every database is one or the other.” Many are neither. A single-leader database with asynchronous replicas is not linearizable if you read from the replicas, and not CAP-available either, since a client cut off from the leader cannot write.
- “CAP governs normal operation.” It says nothing about the case with no partition, which is nearly all of the time.
PACELC: the rest of the time
Daniel Abadi proposed PACELC, in a 2010 blog post and a 2012 paper, to cover that gap. If there is a Partition, choose Availability or Consistency. Else, choose Latency or Consistency.
The second half exists because consistency means waiting. To guarantee that a read sees the latest write, a write must reach several replicas before it is acknowledged, and a read must check with a leader or a quorum. If the replicas are in regions 70 ms apart, every strongly consistent write pays at least one cross-region round trip. Replicating asynchronously and reading locally answers in a few milliseconds, at the price of stale reads and of losing recent writes if the leader dies.
| Systems | During a partition | Otherwise |
|---|---|---|
| Dynamo, Cassandra, Riak | Availability (PA) | Latency (EL) |
| Spanner, CockroachDB, etcd | Consistency (PC) | Consistency (EC) |
Tunable consistency
Many stores let each request choose its own point on this scale. In Cassandra with three replicas:
- ONE answers from a single replica. It is the fastest, and may be stale.
- QUORUM needs two of three. With quorum writes and quorum reads, every read overlaps the latest write.
- ALL needs every replica, and fails if any is down.
- LOCAL_QUORUM needs a quorum inside the local data centre only, which avoids waiting on another region.
One cluster can therefore serve a feed at ONE and a payment status at QUORUM. Tuning moves a request along both PACELC axes at once: asking more replicas makes it more consistent, slower, and more likely to fail during a fault.
When CAP is the wrong lens
Use CAP when data is replicated across machines that can lose contact, most obviously across regions, and writes can arrive on both sides. It is less useful for a single-region, single-leader design. There the practical questions are failover time, replication lag and how much data a failover can lose, covered in availability and reliability and replication.
In the interview
Do not recite “CAP says pick two”. When you choose a data store or add a region, say what each key operation does during a partition and what the choice costs the rest of the time.
For example: “Seat reservations go through a consensus-backed store. During a partition the minority region cannot book, which I accept, because double-booking is worse. The cost is a majority round trip on every booking. Event pages are served from regional replicas and caches, so they stay up and may be slightly stale.” Those four sentences cover CAP, PACELC and the per-operation choice.
If asked whether a database is CP or AP, give the label together with the configuration that makes it true.
Key takeaways
- CAP says that when a network partition separates replicas, a system must either refuse some requests to stay consistent or answer with possibly stale or conflicting data to stay available.
- Partition tolerance is not something you choose. Networks fail, so the real choice is between consistency and availability while a partition lasts.
- In CAP, consistency means linearizability and availability means every non-failed node answers. Both are stricter than everyday usage.
- CP systems keep the majority side working and stop the minority. AP systems accept writes on both sides and reconcile afterwards.
- PACELC adds the everyday case. With no partition, stronger consistency costs latency, because operations wait for more replicas.
- CP and AP labels are simplifications. Behaviour depends on configuration and often on the individual request.
Try it
Check yourself
0 of 5 answered
Flashcards
These 6 cards are in your review deck. Tap a card to see the answer.
What does the CAP theorem state?
During a network partition, a distributed data store cannot be both linearizable and available on every non-failed node. It must give up one of the two for as long as the partition lasts.
Why is "CA" not a real option?
You cannot choose for the network never to fail. A distributed system that claims CA has undefined or broken behaviour when a partition happens.
CP behaviour during a partition
The side with a majority carries on. Nodes on the minority side refuse writes and consistent reads, so their clients get errors or timeouts.
AP behaviour during a partition
Every reachable node keeps answering. Replicas diverge, and after the partition heals they reconcile with last write wins, version vectors or merge logic.
What does PACELC stand for?
If there is a Partition, choose Availability or Consistency. Else, choose Latency or Consistency. Dynamo-style stores are usually PA/EL. Spanner and etcd are PC/EC.
What is tunable consistency?
Choosing per request how many replicas must respond, as with Cassandra's ONE, QUORUM and ALL. More replicas means stronger guarantees, higher latency and less tolerance of failed nodes.