CAP stated precisely, and PACELC
What the theorem says
Section titled “What the theorem says”CAP is often repeated as “consistency, availability, partition tolerance: pick two”. The paper that proved it is narrower, and the narrowness is the point. Gilbert and Lynch define three things.
- Consistency is atomic, or linearizable, consistency. There must be a total order on all operations such that each looks as if it completed at a single instant, and a read that begins after a write completes must return that value or a later one. It is the model of the previous lesson, not a loose idea of “the data agrees”.
- Availability means that every request received by a non-failing node must result in a response. The definition puts no bound on how long the answer may take, but it does not allow a node to refuse.
- Partition tolerance is modelled by letting the network lose arbitrarily many messages between nodes. When it is partitioned, every message sent from one component to another is lost.
The theorem: in a network subject to communication failures, it is impossible for a web service to implement an atomic read/write shared memory that guarantees a response to every request.
Read that carefully. Partitions are not an option you take or leave: they are what the network may do. So the theorem is about what a system gives up while the network is cut. It says nothing about a healthy network. Gilbert and Lynch themselves describe it as the trade-off between consistency and availability in a partition-prone system.
Mental model. Two branch offices share one ledger and a phone line. The line goes dead. A customer asks the second office for the balance. The clerk can say “I cannot tell you until the line is back” (consistent, not available), read out the last balance they know, which may be old (available, not consistent), or accept a withdrawal on their own and sort it out later (available, and the two ledgers now disagree). There is no fourth answer.
Cut the network, and choose
Section titled “Cut the network, and choose”Two nodes, A and B, hold the same value x = 10. A client adds 5 through A, and a client adds 3 through B. Each then reads x. Turn on Cut the network between the nodes, and choose what each node does While the network is cut.
Two nodes hold the same value. Cut the network between them and choose what each side does with the requests it receives: refuse, serve stale data, or accept everything and reconcile later.
Without a cut, the consistent policies pay in latency to keep both copies in step. That is the other half of the PACELC trade-off.
- AReachablex = 15
- BReachablex = 15
The network heals. B catches up with A, and both hold x = 15.
Results of the run
- Consistent (reads return the latest write)
- Yes
- Every request got an answer
- No
- Requests refused
- 2
- Stale reads
- 0
- Writes lost after the network heals
- 0
- Final value of x
- 15
- Write latency with no cut
- 22 ms
| Node | State | Holds |
|---|---|---|
| A | Reachable | x = 15 |
| B | Reachable | x = 15 |
Every event in words
| At second | Event |
|---|---|
| 0 ms | Nodes A and B both hold x = 10. |
| 100 ms | The network between A and B is cut. Each node can still talk to its own clients. |
| 200 ms | A client adds 5 through A. A holds x = 15, and cannot tell B. |
| 300 ms | A client adds 3 through B. B cannot reach A, so it refuses the write. |
| 400 ms | A client reads x through B. B cannot tell whether its copy is current, so it refuses. |
| 500 ms | A client reads x through A and gets 15. |
| 600 ms | The network heals. B catches up with A, and both hold x = 15. |
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- Two nodes, A and B, hold one value x. Node A is the leader whenever requests must stay consistent.
- Latencies are round numbers: 2 ms to answer locally and 20 ms to reach the other node. With no cut, the policies that refuse or serve stale reads wait for the other node before answering. The policy that answers everything does not.
- Last write wins compares timestamps: the write made later through B beats the earlier write through A.
Simplifications
- Real systems have more than two nodes and more kinds of failure than a clean cut.
- One script is replayed: add 5 through A, add 3 through B, then read on both nodes. The merge works because a counter can be merged. Other data may not.
Parameter values are illustrative. They are not benchmarks of any real product.
With Refuse what cannot be answered correctly, node B cannot reach A, so it refuses the write of 3 and the read. The lab shows two requests refused, no stale read and no write lost, and x ends at 15. Every answer that was given is correct, and the system is not available in the sense of the theorem, because a non-failing node did not respond. Notice that A kept answering: availability is a property of every non-failing node, so one refusal is enough to lose it.
With Serve stale reads, B refuses the write but answers the read from its own copy. One request is refused and one read is stale, and x ends at 15. The read returned a value that was no longer the latest, so the system is not linearizable either. Cutting a corner on the read costs the consistency and still loses the availability of the write.
With Answer everything, both nodes answer every request. No request is refused, and the copies drift apart: B holds 13 while A holds 15. When the network heals, the two writes have to be reconciled, and the choice When the nodes meet again decides what survives.
| While the network is cut | Requests refused | Stale reads | Writes lost after healing | Final value of x |
|---|---|---|---|---|
| Refuse what cannot be answered correctly | 2 | 0 | 0 | 15 |
| Serve stale reads | 1 | 1 | 0 | 15 |
| Answer everything, last write wins | 0 | 0 | 1 | 13 |
| Answer everything, merge both writes | 0 | 0 | 0 | 18 |
Last write wins keeps the write with the later timestamp and drops the other. Here it drops the addition of 5 made through A, and x ends at 13, although the client was told it succeeded. Dynamo’s authors name this strategy, and say that it picks the object with the largest physical timestamp. Merge both writes keeps both additions and x ends at 18, but that works only because adding is something you can merge. For most data you have to write the merge yourself, or choose to lose a write.
Check your reading of CAP.
Check your understanding: CAP
This quiz needs JavaScript to check your answers.
What happens if the network is not cut?
Section titled “What happens if the network is not cut?”CAP is silent, and a different trade-off shows up. Abadi’s PACELC formulation says: if there is a partition (P), how does the system trade off availability and consistency (A and C); else (E), when it runs normally, how does it trade off latency and consistency (L and C)? He notes that the latency and consistency trade-off applies only to systems that replicate data.
The lab shows it with the cut turned off. A client’s write is acknowledged after 2 ms when node A answers at once and copies the write to B in the background, and after 22 ms when A waits for B first: 2 ms for the local write plus 20 ms of waiting for B. The slow path is the one that keeps both copies in step before the client hears back. The fast path is quicker by 20 ms, and during that time a read at B could still return the old value, which the model does not draw.
So the choice is there on a good day too, and it is paid by every request, while a partition is rare. Abadi argues that the consistency and latency trade-off could be more influential than the partition one, because it has a more direct effect on a system’s baseline operations. The 20 ms is the lab’s figure, not the speed of any product.
- CAP defines consistency as linearizability and availability as a response from every non-failing node, and says both cannot hold if the network may lose messages.
- It is about what happens during a cut, not a menu for normal operation. Partitions happen to you, they are not a feature you choose.
- In the lab, refusing keeps every answer correct and refuses 2 requests, serving stale reads still refuses 1 and returns 1 stale read, and answering everything loses 1 write under last write wins (x ends at 13) or keeps both when merged (x ends at 18).
- PACELC adds the other half: without a cut, a system trades latency against consistency. In the lab, a write is acknowledged after 2 ms or after 22 ms.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.