CAP Theorem
Overview
The CAP theorem states that a distributed data store can provide at most two of three guarantees at the same time: consistency, availability, and partition tolerance. Because network partitions are unavoidable in any real distributed system, the practical meaning of CAP is a forced choice between consistency and availability whenever a partition occurs.
Key Concepts
CAP describes three properties:
- Consistency (C): every read receives the most recent write or an error. This is linearizability, a stricter notion than the "C" in ACID.
- Availability (A): every request sent to a non-failing node receives a non-error response, though not necessarily the newest data.
- Partition tolerance (P): the system keeps operating even when the network drops or delays messages between nodes.
The subtle point is that P is not optional. In a system that spans more than one machine, partitions will happen, so you cannot trade P away. The real decision surfaces only during a partition: keep serving requests (choose A) or refuse requests that cannot be made safe (choose C). This is why systems are usually labeled CP or AP rather than "picking two of three."
The choice interacts directly with the consistency models a store exposes: a CP system typically offers strong consistency, while an AP system leans on eventual consistency and reconciles divergent replicas later.
| Choice | Behavior during a partition | Gives up |
|---|---|---|
| CP | Blocks or errors requests that cannot guarantee the latest value | Availability |
| AP | Serves every request from reachable nodes | Consistency |
Trade-offs
A CP system protects correctness: no client ever reads stale data, which matters for balances, inventory, and locks, but some clients see errors or timeouts during a partition. An AP system protects the user experience: every request gets an answer, which suits feeds, catalogs, and telemetry, but clients may read outdated values that converge later. Many production stores are tunable per operation, so a single database can behave CP for a critical write and AP for a cheap read.
| Prefer CP when | Prefer AP when |
|---|---|
| Stale reads cause real harm (payments, stock levels) | Temporary staleness is acceptable (likes, view counts) |
| Coordination or uniqueness must hold (locks, leader election) | Uptime and low latency dominate the requirements |
Interview Tips
- State clearly that CAP forces a choice only during a partition; in normal operation a system can be both consistent and available.
- Reach for PACELC to describe the latency-versus-consistency trade-off that exists even when the network is healthy.
- Clarify that "consistency" in CAP means linearizability, not the transactional "C" in ACID.
- Anchor the choice to the product: money and locks lean CP, engagement metrics lean AP.
Summary
- The CAP theorem allows at most two of consistency, availability, and partition tolerance simultaneously.
- Partition tolerance is mandatory for real distributed systems, so the practical choice is C versus A under a partition.
- CP systems sacrifice availability to avoid stale reads; AP systems sacrifice consistency to keep answering.
- The right choice depends on whether stale data causes harm in the specific product.
- CAP speaks to partition scenarios only; PACELC covers the normal, no-partition case.