CAP Theorem
By Ankur NayakReviewed
The CAP theorem says that when a network partition splits a distributed system, it must choose between consistency (every read sees the latest write) and availability (every request gets a non-error answer). It cannot guarantee both during the partition.
How it works
Formally proven by Gilbert and Lynch in 2002, CAP concerns three properties: Consistency (linearizability: the system behaves like a single up-to-date copy), Availability (every request to a non-failed node gets a response) and Partition tolerance (the system keeps working when messages between nodes are lost).
Partitions are not optional in real networks, so the practical choice is what to give up while one is happening. A CP system refuses some requests to avoid serving stale or conflicting data, as a Raft-based store does on the minority side. An AP system keeps answering on both sides and reconciles divergent writes later, as Dynamo-style stores do.
Outside partitions, the PACELC extension adds a second trade-off: even when everything is healthy, stronger consistency costs latency because replicas must coordinate.
Key points
- Choose C or A only during a partition
- CP: correct but may reject requests
- AP: always answers, may be stale or conflict
- Many systems tune the choice per operation
When to use it, and pitfalls
Use it for
- Choosing a datastore for each piece of data
- Explaining behaviour during region or zone outages
Watch out for
- Treating CAP as 'pick two' in normal operation
- Assuming a database is 'CP' or 'AP' regardless of configuration (quorums change it)
Trade-offs
Payments vs. likes
Use CP storage for money and inventory, AP for feeds, counters and presence.
Pros: Correctness where errors are costly; Availability where staleness is harmless
Cons: Two kinds of storage to operate
Used in
Related building blocks
Frequently asked questions
Is Cassandra CP or AP?
It is usually described as AP because it keeps accepting reads and writes during partitions, but its consistency level is tunable per query. Writing and reading at QUORUM gives much stronger guarantees at the cost of availability when too many replicas are unreachable.
Why can't a system have all three?
If two replicas cannot communicate, a write on one cannot reach the other. To keep the other replica consistent you must stop it from answering (losing availability); to keep it answering you must let it return stale data (losing consistency).