CAP, PACELC and consistency models
When the network splits, every copy of your data must choose between answering and being right. The rest of the time, it chooses between fast and right.
Updated since you finished it. We changed this lesson after you marked it done. Your progress is kept.
What broke
A shoe brand runs a flash sale in two regions, Mumbai and Singapore. There are 500 pairs. The stock count lives in a database that copies data between the regions. Both regions accept orders.
Ten minutes into the sale, the link between the regions breaks for four minutes. Both sides keep selling. Each side thinks it has the full count. When the link comes back, the team finds 620 orders for 500 pairs. Support spends a week on refunds and apologies.
In the same company, the payments team uses a different database. During the same four minutes, it refused every write in Singapore. Those users saw "Payment failed, try again".
Both teams wrote the same thing in the incident channel: "the database broke". It did not. Each database did exactly what it was built to do. Nobody had written down which choice the business wanted. This story is made up, but each part of it happens in real systems.
The idea in simple words
You keep copies of your data on several machines, called replicas. Sometimes the network between them fails, but the machines keep running. This is a network partition.
During a partition, a replica that gets a request has only two options:
- Answer now. It may return old data, or accept a write that conflicts with the other side. The system stays available.
- Wait or refuse. It does not answer until it can check with the other side. The data stays consistent.
That is the CAP theorem. Gilbert and Lynch proved it in 2002. No system can promise both consistency and availability when messages between nodes can be lost. 1
People often say CAP means "pick 2 of 3". Eric Brewer presented the idea in a keynote talk in 2000. 2 In 2012 he wrote that "2 of 3" is misleading. 2 You cannot simply pick "no partitions", because networks can fail. 2 But partitions are rare. When there is no partition, a system can be both consistent and available. 2 3
The key idea. CAP is a rule for one bad moment: the network is split. In that moment, each cut-off replica chooses between answering and being right. The rest of the time, the real choice is between fast and right.
How it works
Step through the shoe sale with one item left. Use Next and Back, or the ← and → keys.
- Two replicas, A and B, hold the stock count for one item. There is 1 left. A copies every change to B, so they agree.
- The network link between A and B breaks. Both replicas are still running. They just cannot talk to each other. This is a network partition.
- Ravi's app is connected to B. He taps Buy on the last item. B must choose now: answer him, or stay correct.
- A CP system says no. B cannot check with A, so it returns an error. The data stays correct, but Ravi cannot buy for now.
- An AP system says yes. B sells the item to Ravi. On the other side, A has sold the same item to Asha. Both got an answer, but one item was sold twice.
- The link comes back. In the CP system, B catches up with A and nothing needs fixing. The AP system must merge the two histories and fix the conflict, for example by cancelling one order.
What C and A mean in CAP
The words in CAP are narrow. Consistency means linearizability: the system must act as if there is one copy and each operation happens at one instant. 1 3 It is not the C in ACID. 2
Availability means every request that reaches a working node gets an answer that is not an error. Not "most requests", and not "some node answers". 1 4
With these strict meanings, many real databases are neither CP nor AP. Kleppmann gives an example: one leader with async followers. A client cut off from the leader cannot write, so it is not available. Reads from a lagging follower can be stale, so it is not linearizable either. 4 So do not label a whole database. Ask what each operation does during a partition.
PACELC: the trade you make every day
Abadi pointed out a second trade-off that matters more day to day. 3 He called the full picture PACELC:
- If there is a Partition, choose Availability or Consistency.
- Else, in normal running, choose Latency or Consistency.
Why is there a trade when the network is fine? As soon as you copy data, a consistent read or write must coordinate with other replicas or a leader. That takes time. Over long distances, you cannot avoid this wait. 3
Abadi gives an example from a study of Cassandra. Reads that check several replicas were compared with reads from any one replica. The latency difference could be a factor of four or more. 3
Who chooses what
Abadi classified well-known systems by their original default settings: 3
- PA/EL: Dynamo, Cassandra and Riak. They give up consistency during a partition, and for lower latency in normal running.
- PC/EC: VoltDB/H-Store, Megastore, BigTable and HBase. They pay in availability and latency to stay consistent.
- PC/EL: Yahoo's PNUTS. Fast and less consistent normally, but it refuses updates to data whose master node is cut off by a partition.
- PA/EC: MongoDB, as it worked in 2012.
Many of these systems let you change the trade. In Cassandra-style stores, you can ask more replicas to answer each read and write. You get more consistency, though still not linearizability, and pay with latency. 3
GitHub saw the cost of a short partition in 2018. A 43-second network cut caused a failover. Both data centres then had writes the other did not have. 10 During recovery, they chose "to prioritize data integrity over site usability and time to recovery". The site was degraded for 24 hours and 11 minutes. 10
The consistency ladder
"Consistent" and "eventually consistent" are not the only options. There is a ladder of consistency models in between. Each step down promises less, and is easier to keep available and fast.
- Linearizable. Once a write is done, every later read, by anyone, sees it. The system acts like one copy. 5 Use it for a lock, a unique username, or the last item in stock. It cannot stay available during a partition. 5
- Sequential. Everyone sees all operations in the same order, and each client's own order is kept. But that order does not have to match real time. 6 Like linearizability, it cannot stay available during a partition. 6
- Causal. If one operation may have caused another, everyone sees them in that order. A reply never appears before the comment it answers. Unrelated operations may appear in different orders to different people. 7
- Read-your-writes. After you save something, your own later reads show it. Other users may see it later. 8 9
- Eventual. If writes stop for a while, all replicas end up the same. The basic promise says nothing about when, or what you see before that. 9
Causal consistency and read-your-writes can stay available during a partition, if each client keeps talking to the same server. 7 8 That makes them useful for feeds, comments and profiles.
Trade-offs
| Model | During a partition | Normal latency | Good for |
|---|---|---|---|
| Linearizable | No Cut-off nodes must wait or refuse. | Highest. Operations must coordinate with other replicas or a leader. | Locks, unique names, stock counts, money moves. |
| Sequential | No Same as linearizable. | High. | Cases where all must agree on one order, and a short delay is fine. |
| Causal | Yes , if each client stays on one server. | Low to medium. | Comments and replies, chat, feeds. |
| Read-your-writes | Yes , if each client stays on one server. | Low. | Profiles and settings a user just changed. |
| Eventual | Yes | Lowest. | Like counts, view counts, caches, search indexes. |
Simple rule: decide per operation, not per database. Ask what a wrong answer costs and what an error costs, then pick the weakest model the business can live with.
At work: how a senior uses this
- For each write path, ask: "If the network splits right now, should this fail or should it accept and fix later?" Write the answer in the design doc.
- Treat "CP" and "AP" in vendor docs as a hint, not a fact. Read which operations wait, which refuse, and what the default settings are. 4
- Measure the latency cost. A consistent read in another region costs a round trip on every request, partition or not. 3
- If you choose AP, design the repair before launch: how to detect conflicts, which side wins, and who tells the customer. Brewer calls this partition recovery. 2
- Use stronger models only where they are worth the cost. A stock count may need linearizable writes. The product description next to it does not.
- Test with a real partition. Cut the network between replicas in a test environment, and watch what your app does.
With AI
What to ask the agent
"Here is our order service. List every database write. For each, tell me what happens if the replicas in two regions cannot talk for 5 minutes, with our current settings."
"We use Cassandra with replication factor 3. Explain which read and write consistency levels give us read-your-writes for one user, and what each choice costs in latency."
"Write a test that starts two replicas, cuts the network between them, writes on both sides, heals the network, and checks that our conflict rule keeps both orders."
What to check yourself
- The agent's claims about a database's behaviour match that database's own docs and version. CAP labels on blogs are often wrong.
- The settings it suggests are the ones your code really sends. Many clients have their own default consistency level.
- The partition test cuts the network between replicas. Killing a node is a different failure.
- The conflict rule never silently drops a paid order or a payment.
- Every operation that needs linearizable behaviour is listed, and a person agreed to its latency cost.
Check yourself
Your progress is saved only in this browser. Export it to keep it safe.
You have finished 5 lessons. Your progress lives only in this browser. Export a copy so you don’t lose it.
Go deeper
Show 10 sources and related lessons
- Seth Gilbert and Nancy Lynch, “Brewer's Conjecture and the Feasibility of Consistent, Available, Partition-Tolerant Web Services”, ACM SIGACT News, 2002 (opens in a new tab)
The proof of CAP. Section 2 defines consistency, availability and partition tolerance exactly. Theorem 1 is the result. - Eric Brewer, “CAP Twelve Years Later: How the ‘Rules’ Have Changed”, IEEE Computer, 2012 (opens in a new tab)
Brewer explains why "2 of 3" is misleading, and how to detect a partition, limit some operations, and recover after it. - Daniel J. Abadi, “Consistency Tradeoffs in Modern Distributed Database System Design”, IEEE Computer, February 2012 (opens in a new tab)
The PACELC paper. Explains the latency versus consistency trade-off and classifies Dynamo, Cassandra, PNUTS, MongoDB and others. - Martin Kleppmann, “Please stop calling databases CP or AP” (2015) (opens in a new tab)
Why most real databases are neither CP nor AP in CAP's strict sense, and what to say instead. - Jepsen, Consistency models: Linearizability (opens in a new tab)
A short, exact definition, and why it cannot stay available during a partition. - Jepsen, Consistency models: Sequential consistency (opens in a new tab)
Like linearizability, but without the real-time rule. - Jepsen, Consistency models: Causal consistency (opens in a new tab)
Related operations appear in the same order everywhere. Available during a partition if clients stay on one server. - Jepsen, Consistency models: Read Your Writes (opens in a new tab)
A short, exact definition of the read-your-writes guarantee. - Werner Vogels, “Eventually Consistent – Revisited” (2008) (opens in a new tab)
Defines eventual consistency, read-your-writes and other client-side guarantees. - GitHub, “October 21 post-incident analysis” (opens in a new tab)
A 43-second network cut left writes in two data centres. In recovery, GitHub chose data integrity over a fast recovery.
Real incident: GitHub, 21 Oct 2018: GitHub outage: a 43-second network cut caused a day of trouble
Case studies: Amazon: How Dynamo kept the shopping cart always writable · Google: Spanner: a global database built on known clock error
Related: Replication · Quorums and leaderless replication · Time, clocks and ordering · Consensus and Raft · Multi-region systems and data locality · CRDTs and coordination avoidance (coming soon)
Before this: Replication explains replication lag and read-your-writes in detail. Next: Quorums and leaderless replication, and Multi-region systems and data locality for what these choices cost across regions.