What is the CAP theorem, and why does it matter for distributed systems?
The CAP theorem states that when a network partition happens, a distributed data store must choose between consistency (every read sees the latest write, i.e. linearizability) and availability (every request to a non-failed node gets a non-error response). Because partitions can always happen in a real network, it is not “pick any two of three”: partition tolerance is a requirement, and the real choice is how the system behaves while a partition lasts. It matters because it forces designers to decide, per feature, whether a stale or conflicting answer is worse than an error or a wait.
Read full answerHide full answer
Applies toreplicated databases, caches, coordination services and any service whose state is copied across nodes or data centres
The Three Properties (as Defined in the Proof)
| Property | Precise meaning |
|---|---|
| Consistency © | Linearizability: operations appear to happen in a single order that respects real time; a read returns the most recent completed write |
| Availability (A) | Every request received by a non-failing node returns a non-error response (no time bound, but it must eventually answer) |
| Partition tolerance (P) | The system keeps operating even though messages between nodes may be lost or delayed indefinitely |
Eric Brewer proposed it in 2000; Seth Gilbert and Nancy Lynch proved it formally in 2002.
A Concrete Scenario
Two data centres, DC-East and DC-West, each hold a replica of account balances. The link between them fails. A client in the east writes balance = 50.
-
CP choice: DC-West cannot confirm it has the latest value, so it rejects or blocks reads and writes for that data (or only the side with a majority continues). Users in the west see errors or timeouts, but nobody reads a stale balance.
-
AP choice: both sides keep serving. DC-West still returns the old
balance = 100, and may even accept its own writes. After the link heals, replicas reconcile (last-writer-wins, merge, or application logic).
Why “Pick Two” Is Misleading
-
Partitions are not optional: switches fail, cables are cut, GC pauses and overloaded nodes look like partitions. A system that “gives up P” simply has undefined behaviour when they occur.
-
A single-node database is often labelled “CA”, but that only means it is not distributed; if its one node fails, it is neither consistent-and-available nor partition-tolerant in any useful sense.
-
The choice is per operation and configurable: Cassandra and DynamoDB let you choose consistency levels per request, and MongoDB uses read and write concerns.
Beyond CAP: PACELC
CAP only describes behaviour during a partition. PACELC (Daniel Abadi) adds: Else, when the network is fine, the system trades Latency vs Consistency. Synchronous replication across regions gives strong consistency but adds a round trip to every write; asynchronous replication is faster but can serve stale reads.
Why It Is Important in Practice
| Design question | CAP-informed answer |
|---|---|
| Can a user see an old value briefly? | If yes (likes, feeds), AP plus eventual consistency is fine |
| Is a wrong value dangerous? | If yes (balances, inventory reservations, leader election), prefer CP behaviour |
| What happens during a partition? | Define it explicitly: errors, read-only mode, or accept writes and reconcile |
| How fast must writes be across regions? | PACELC: strong consistency costs latency even without failures |
Interview Tip
State CAP precisely (linearizability vs availability during a partition), reject the “pick two” framing, walk through the two-data-centre scenario, and add PACELC for the normal case. Mentioning that real systems tune this per operation shows practical understanding.