Sub topic 5
The CAP theorem: what it says and what it does not
This chapter assumes you know what linearizability is (a system that behaves like a single copy of the data, as covered earlier), that replication keeps copies of data on several machines, and that you can read short Python programs. CAP is quoted constantly and misquoted almost as often. The goal here is to state the theorem precisely, walk through why it holds, and list the claims it does not support.
The three terms, defined strictly
The CAP theorem was conjectured by Eric Brewer in 2000 and proved by Seth Gilbert and Nancy Lynch in 2002. The proof uses narrow definitions, and the theorem is only true for those definitions.
- Consistency (C) means linearizability. Every operation appears to take effect at one instant between its start and its end, and all clients see the same single order. It does not mean the "C" in ACID, which is about preserving application invariants.
- Availability (A) means every request that reaches a non-failing node eventually gets a non-error response. The response must be a real answer, not a timeout or an "unavailable" error. The theorem sets no latency bound, but a response that takes forever is not useful in practice.
- Partition tolerance (P) means the system keeps its promises when the network drops or delays an arbitrary number of messages between nodes.
The theorem says that when a network partition occurs, a system cannot be both linearizable and available. It must give up one of them.
Why the theorem holds
The proof is short. Take two replicas, A and B, that both store x, which starts at 0. A partition cuts the link between them. Client 1 sends write(x, 1) to A. Client 2 later sends read(x) to B.
B has three options:
- Return 0. This is available but not linearizable, because the write completed before the read began.
- Wait for A. This blocks until the partition heals. The system is not available.
- Return an error. This is also not available.
B cannot tell whether A received a write, because the network is the thing that failed. No algorithm can get around this.
A runnable demonstration
The following program models two replicas and a switch for the partition. In CP mode a replica refuses to act when it cannot reach its peer. In AP mode it serves requests from local state.
class Cluster:
def __init__(self, mode):
self.mode = mode # "CP" or "AP"
self.value = {"A": 0, "B": 0}
self.partitioned = False
def write(self, node, v):
if self.partitioned:
if self.mode == "CP":
raise RuntimeError("unavailable: cannot reach peer")
self.value[node] = v # AP: accept locally, replicate later
return
self.value["A"] = v # connected: both copies updated
self.value["B"] = v
def read(self, node):
if self.partitioned and self.mode == "CP":
raise RuntimeError("unavailable: cannot reach peer")
return self.value[node]
for mode in ("CP", "AP"):
c = Cluster(mode)
c.partitioned = True
try:
c.write("A", 1)
print(mode, "read from B returns", c.read("B"))
except RuntimeError as e:
print(mode, "write failed:", e)The output is:
CP write failed: unavailable: cannot reach peer
AP read from B returns 0The CP cluster refused the write. The AP cluster accepted it, and a later read from B returned the stale value 0, which is the history from the proof.
What the theorem does not say
"Pick any two of three" is misleading
You cannot choose to give up P. Partitions are not a feature you can decline. Networks drop packets, switches fail, and garbage-collection pauses look like partitions to peers. A single-node system avoids partitions only by not being distributed. In practice the choice is between C and A, and only while a partition is happening.
The choice is not permanent
When the network is healthy, a system can be both consistent and available. CAP constrains behavior during a partition. A CP system is not "always slow or down", and an AP system is not "always inconsistent". Many systems also let you choose per operation, for example a quorum read for one request and a local read for another.
Brewer's later clarification
Brewer later wrote that the "two out of three" framing hid the real design space. Systems can detect partitions, limit what operations are allowed while one lasts, and repair the state afterward. The choice is therefore more granular than a label for the whole database.
CAP says nothing about latency
CAP does not cover the cost of coordination when nothing is broken. A system that waits for a cross-region quorum on every write is slower even with no partition. The next chapter covers PACELC, which addresses this.
Consistency here is only linearizability
CAP's C is the strongest model. Weaker models can stay available during a partition. Sequential consistency in its general form needs coordination too, but causal consistency, covered later, can be achieved with all replicas available. So "AP" does not mean "no guarantees". Treating CAP as a two-way split between strong and eventual consistency throws away the middle of the spectrum.
Availability in CAP is stricter than uptime
In the theorem, A requires that every non-failing node answer. A CP system that keeps a majority partition running is "unavailable" by this definition, because nodes in the minority side must refuse requests. It can still have very good availability in the operational sense, such as 99.99% of client requests succeeding. When a vendor says "highly available" and a paper says "CP", they are using different definitions and are not necessarily contradicting each other.
Classifying real behavior
| Behavior during a partition | Category | Typical mechanism |
|---|---|---|
| Minority side rejects reads and writes, majority side continues | CP | Consensus with a leader and majority quorum |
| Every node accepts writes and reads from local state | AP | Eventual replication, conflict resolution later |
| Reads served locally, writes need a quorum | Mixed | Per-operation configuration |
Most databases fit one row by default, but their configuration can move them. Treat a product's CAP label as a statement about its defaults. To know what it actually does, read the documentation for each operation and test it (a later chapter covers testing).
Using CAP in design decisions
CAP is a tool for asking one specific question: what should this operation do if the replicas cannot talk to each other? Apply it per operation, not per system.
- A bank balance transfer might need to fail rather than risk a double spend. This is the CP choice.
- A "likes" counter or a shopping cart can accept writes on both sides and merge them later. This is the AP choice.
- A user's own profile read might be fine with slightly stale data, but a password change must reach every replica before it counts.
Write down, for each operation, which side you take, and what the user sees on the other side. Showing an error is a legitimate user experience, and so is showing a stale value with a label saying so.
Summary of corrections
- Partition tolerance is not optional, so the real choice is C or A during a partition.
- C means linearizability, not "any good consistency".
- A means every non-failing node answers, which is stricter than "mostly up".
- The trade-off applies only while a partition lasts. Latency costs in normal operation are a separate question.
- Classification is per operation and per configuration, not per product.
Exercise
A key-value store has three replicas. A partition isolates one replica from the other two. The two connected replicas keep serving reads and writes, and the isolated replica returns errors. Is this system CP or AP according to the CAP theorem, and why might its operators still claim high availability?
Show answer
It is CP. The isolated replica is a non-failing node that does not return a non-error response, so the strict availability definition is violated, and the majority side stays linearizable. The operators can still claim high availability in the operational sense, because most client requests (those that reach the majority side) succeed. The two uses of "availability" differ.
Exercise
Run the demonstration above in AP mode, but add a heal step: set partitioned = False and copy A's value to B. What does a read from B return afterward, and what does this tell you about the consistency the AP cluster provides?
Show answer
After the heal, B returns 1, so the replicas converge. The cluster gave up linearizability during the partition (B returned the stale value 0 after the write completed) but still reached the same state later. That is eventual consistency, not linearizability.