The CAP Theorem Explained Correctly
What consistency, availability and partition tolerance really mean, why partition tolerance is not optional, how CP and AP systems behave, and what PACELC adds.
The CAP theorem is one of the most quoted and most misquoted ideas in distributed systems. The popular summary is 'consistency, availability, partition tolerance: pick any two'. That summary is misleading, because it suggests three equal options and a free choice among them.
The accurate statement is narrower and more useful: when a network partition occurs, a distributed system must choose between consistency and availability. These notes define the three terms precisely and show how the choice appears in real designs.
The three properties
- Consistency (C): every read returns the most recent successful write, or an error. All nodes appear to hold the same data at the same time. This is stronger than, and different from, the C in ACID.
- Availability (A): every request sent to a node that has not crashed receives a non-error response, although the response may not contain the latest data.
- Partition tolerance (P): the system continues to operate even when the network drops or delays messages between nodes, splitting them into groups that cannot communicate.
Why partition tolerance is not optional
A network partition is not a design choice; it is a fault that happens. Cables are cut, switches fail, a data centre loses its link, a node pauses long enough to be considered unreachable. Any system that stores data on more than one machine will eventually experience a partition.
A system that 'chooses CA' would be one that stops working correctly whenever the network misbehaves, which is not a property anyone can rely on. The only true CA system is a single node, and that is not a distributed system. So for any distributed data store, P is a given, and the real question is what the system does about C and A when a partition happens.
The choice during a partition
Consider two replicas, N1 and N2, holding the same record, with a network failure between them. A client writes a new value to N1. N1 cannot send it to N2. Now another client reads the record from N2. There are only two possibilities.
N2 can answer with the value it has. The system stays available, but the answer is stale, so consistency is lost. Or N2 can refuse to answer because it cannot confirm that it has the latest value. The system stays consistent, but that request fails, so availability is lost. There is no third option that gives both, and this is what the theorem states.
Client A Client B
| write x = 2 | read x
v v
+------+ network partition +------+
| N1 | ---------- X ------------- | N2 |
| x=2 | (replication blocked) | x=1 |
+------+ +------+
CP behaviour: N2 rejects the read (or N1 rejects the write) -> correct, but unavailable
AP behaviour: N2 returns x = 1 -> available, but staleCP systems
A CP system chooses consistency. During a partition, the side that cannot reach a majority of nodes stops accepting writes, and often reads, and returns errors or times out. When the partition heals, there is nothing to reconcile, because no conflicting data was accepted.
This is the right choice when a wrong answer is worse than no answer: account balances, payments, inventory and seat booking, distributed locks, leader election and configuration. Coordination services such as ZooKeeper and etcd, and databases that replicate through consensus protocols, behave this way.
AP systems
An AP system chooses availability. During a partition every node keeps accepting reads and writes using the data it has. Replicas diverge for a while. When the partition heals, they exchange updates and converge to the same state. This model is called eventual consistency: if no new writes arrive, all replicas eventually return the same value.
This is the right choice when a slightly stale answer is acceptable and an error is not: social feeds, like and view counts, product catalogues, shopping carts, DNS. Dynamo-style databases such as Cassandra are typically run in this mode.
Divergent writes must be reconciled. Common strategies are last-write-wins based on timestamps, which is simple but can silently discard an update; version vectors, which detect conflicts and let the application merge them; and CRDTs, data types designed so that concurrent updates always merge deterministically.
It is not a single switch
CAP is frequently applied too coarsely. Three refinements matter in practice.
- The choice applies only during a partition. When the network is healthy, a system can provide both consistency and availability.
- The choice can differ per operation. In an e-commerce system, browsing the catalogue can be AP while placing the order and charging the card is CP.
- Many databases are tunable. With N replicas, a write acknowledged by W replicas and a read that consults R replicas are guaranteed to overlap when R + W > N, which gives strong consistency; smaller values give more availability and lower latency.
W = 2, R = 2 R + W = 4 > 3 reads always see the latest write; tolerates 1 node down W = 3, R = 1 R + W = 4 > 3 fast reads; a write fails if any replica is down W = 1, R = 1 R + W = 2 <= 3 fastest and most available; reads may be stale
PACELC: the trade-off when nothing is broken
CAP says nothing about normal operation, which is most of the time. The PACELC formulation completes the picture: if there is a Partition, choose between Availability and Consistency; Else, choose between Latency and Consistency.
Even with a healthy network, keeping replicas strictly consistent means waiting for them to acknowledge each write, which adds latency. Replicating asynchronously responds faster but allows stale reads. A system that favours availability and low latency is described as PA/EL; one that favours consistency in both situations is PC/EC. This latency-versus-consistency trade-off is the one engineers face every day, and mentioning it shows a deeper understanding than CAP alone.
Applying it in a design
In a design discussion, CAP is used by asking, for each kind of data: what should happen if replicas cannot talk to each other? If showing old data is harmless, prefer availability and accept eventual consistency. If acting on old data causes damage, prefer consistency and accept that some requests will fail during a partition. Then choose the database and its replication settings to match.
- Bank transfer, payment, stock decrement at checkout: consistency.
- Feed, likes, comments, view counts, recommendations: availability.
- User session and cart: usually availability, with merge on conflict.
- Leader election, locks, unique username registration: consistency.
Interview summary
- Partitions will happen, so P is mandatory for a distributed system.
- During a partition the choice is between C and A.
- CP: reject requests to stay correct. Use for money, inventory, locks.
- AP: keep serving, converge later (eventual consistency). Use for feeds and counters.
- PACELC: without a partition the trade-off is latency versus consistency.
Summary
The CAP theorem does not offer a choice of any two properties. Network partitions are unavoidable, so a distributed system must decide whether to stay consistent or stay available while a partition lasts. The decision is made per use case, many databases let you tune it, and PACELC extends it to the everyday trade-off between latency and consistency.
Puneet Tiwari
Full Stack Developer
