CAP Theorem Explained
IntermediateThe CAP theorem says a distributed data store can guarantee at most two of three properties: Consistency (every read sees the latest write), Availability (every request gets a response), and Partition tolerance (the system keeps working despite dropped messages between nodes). Since network partitions are unavoidable in real systems, the real choice is between consistency and availability when a partition happens — making systems either CP or AP.
Think of two shopkeepers who lose their phone line
Two branches of a shop normally sync inventory by phone. One day the line drops (a partition). Now each branch must decide: refuse to sell until the line is back so counts never disagree (consistency, but unavailable), or keep selling and reconcile later, risking overselling (available, but temporarily inconsistent). They cannot both stay open AND guarantee identical counts while the line is down. That trade-off is CAP.
Step by Step
Key Concepts
Consistency (in CAP)
Every read reflects the most recent successful write across all nodes — a single, up-to-date view. Note this is stronger than the "C" in ACID, which means valid state.
Availability
Every request receives a non-error response, even if it is not the latest data. The system never refuses to answer.
Partition Tolerance
The system continues operating despite the network dropping or delaying messages between nodes. Mandatory in practice, since networks are unreliable.
CP vs AP
During a partition, a CP system sacrifices availability to stay consistent (returns errors), while an AP system sacrifices consistency to stay available (returns possibly-stale data).
Key Facts
- CAP is about behaviour during a partition; when the network is healthy a system can offer both consistency and availability.
- The "two of three" phrasing is a simplification — because partition tolerance is mandatory, the real dial is consistency vs availability.
- Real databases are rarely purely CP or AP; many offer tunable consistency so you can choose per operation.
Real-World Applications
Choosing a database for payments
A payment ledger must never show conflicting balances, so it favours a CP store that returns an error during a partition rather than risk inconsistent money — correctness over uptime for that data.
A shopping cart
An e-commerce cart favours AP: staying available and letting a user keep adding items is more important than perfect cross-region consistency, which reconciles moments later.
Frequently Asked Questions
What does the CAP theorem actually say?
It states that a distributed data store can simultaneously guarantee at most two of three properties: consistency (reads see the latest write), availability (every request gets a response), and partition tolerance (it keeps working despite network failures). Because partitions are inevitable, the practical choice is between consistency and availability during a partition.
Can I really only have two of the three?
The "pick two" framing is a simplification. Since network partitions are unavoidable, partition tolerance is effectively required, so the real trade-off is consistency versus availability — and only during a partition. When the network is healthy, a system can provide both.
What is the difference between a CP and an AP system?
During a network partition, a CP system stays consistent by refusing requests it cannot serve correctly (sacrificing availability), while an AP system stays available by answering with possibly-stale data that reconciles later (sacrificing consistency). Banks lean CP; shopping carts and feeds often lean AP.
What is PACELC?
PACELC extends CAP: if there is a Partition, you trade Availability vs Consistency; Else (normal operation) you trade Latency vs Consistency. It captures that even without a partition, stronger consistency generally costs more latency — a trade-off CAP alone ignores.