CAP Theorem
In a distributed system, a network partition forces a choice between consistency and availability — you cannot have both while partitioned; PACELC adds the latency-vs-consistency tradeoff in normal operation.
By Ratish Jain · Updated
Definition
The CAP theorem (Brewer) states that a distributed data store can guarantee at most two of three properties simultaneously: Consistency (every read sees the most recent write), Availability (every request gets a non-error response), and Partition tolerance (the system keeps working despite dropped or delayed messages between nodes).
Because network partitions are unavoidable in real systems, P is not optional. The real decision, when a partition occurs, is between C and A.
Why It Exists
Networks fail — links drop, packets are lost, nodes get isolated. When two replicas can't talk, the system faces a dilemma: either refuse requests to avoid returning inconsistent data (sacrificing availability), or keep serving from each side and risk divergence (sacrificing consistency).
CAP names this fundamental, unavoidable tradeoff so architects stop expecting a distributed store to be perfectly consistent, always available, and partition-proof all at once.
How It Works
During a partition, a CP system (e.g. HBase, etcd, ZooKeeper, single-leader RDBMS with strict quorum) rejects or blocks requests on the minority side to preserve a single correct view — clients see errors or timeouts but never stale data. An AP system (e.g. Cassandra, DynamoDB, Riak with eventual consistency) accepts reads and writes on both sides and reconciles later via mechanisms like last-write-wins or version vectors.
PACELC extends CAP: if Partitioned, choose A or C; Else (normal operation), choose between Latency and Consistency. This captures that even without partitions, strong consistency costs coordination latency — e.g. Cassandra is PA/EL, while a strongly consistent store is PC/EC.
What It's Used For
- Classifying databases as CP or AP to match application requirements.
- Deciding whether a service should error out or serve stale data during outages.
- Choosing quorum/consistency levels (ONE vs QUORUM vs ALL) in Dynamo-style stores.
- Justifying eventual consistency for shopping carts, feeds, and metrics.
- Reasoning with PACELC about the latency cost of strong consistency in the normal case.
Under Partition
Tradeoffs
- CP: reads never return stale or conflicting data — correctness first.
- AP: stays writable and readable through partitions — resilience first.
- PACELC surfaces the everyday latency-vs-consistency cost, not just failures.
- CP sacrifices availability: clients see errors during partitions.
- AP sacrifices consistency: clients may read stale or conflicting values.
- AP requires conflict-resolution logic (version vectors, LWW, CRDTs).
- CAP is a simplification — real systems tune consistency per operation.
When to Use / Avoid
- Use when
- Choose CP for money, inventory, and correctness-critical data; choose AP for feeds, carts, metrics, and always-on UX.
- Avoid when
- Don't treat CAP as a strict two-of-three checkbox — modern stores offer tunable, per-request consistency.
- Real-world
- ZooKeeper and etcd are CP; Cassandra and DynamoDB are AP with tunable quorums; PostgreSQL primary is CP.
Interview Questions
Because partitions are caused by the network, not a design choice — in any real distributed system links will eventually fail or delay messages. A system that isn't partition-tolerant simply breaks when that happens. So P is a given, and the actual runtime choice is between consistency and availability during a partition.
CAP only describes behavior during a partition. PACELC says that Else — when the system is healthy — there is still a tradeoff between Latency and Consistency, because strong consistency requires cross-node coordination that adds latency. It classifies systems like PA/EL (Cassandra) or PC/EC (strict stores), capturing the everyday cost CAP ignores.
- →CAP: pick 2 of Consistency, Availability, Partition tolerance.
- →Partitions are unavoidable, so the real choice under partition is C vs A.
- →CP refuses requests to stay correct; AP serves stale data to stay up.
- →PACELC: else (no partition) trade Latency vs Consistency.
- →Modern stores offer tunable, per-request consistency rather than a hard binary.
Related
Everything on Archprimer is connected — follow the graph.