If you build anything that runs on more than one server, you will eventually hit the CAP theorem. It is the single most important constraint in distributed systems design, and every database, message queue, and microservice architecture is shaped by it — whether the engineers who built it know it or not.
The CAP theorem says: a distributed system can deliver at most two of three guarantees — Consistency, Availability, and Partition Tolerance. You cannot have all three at the same time. This is not an opinion or a best practice. It was proven mathematically by Seth Gilbert and Nancy Lynch at MIT in 2002, based on a conjecture by Eric Brewer in 2000.
The Three Guarantees
Before we talk about trade-offs, let us define each guarantee precisely. These definitions are more nuanced than most tutorials admit.
Why You Cannot Have All Three
Here is the thought experiment that makes the trade-off intuitive. Imagine two database nodes, A and B, that replicate data between each other. A client writes balance = 100 to Node A. Before Node A can replicate this to Node B, the network between them goes down. This is a partition.
Now a second client reads from Node B. You have two choices:
- Return the old value (say
balance = 0). You preserved availability (the request got a response) but sacrificed consistency (the client got stale data). - Refuse the request until the partition heals. You preserved consistency (every read returns the latest write) but sacrificed availability (the client got an error or timeout).
There is no third option. During a partition, you must choose between C and A. This is the core of the CAP theorem.
balance = 100
to Node A
A cannot reach B
Messages lost
from Node B
What do you return?
CP Systems: Consistency + Partition Tolerance
A CP system chooses consistency when a partition occurs. If a node cannot confirm it has the latest data, it refuses to serve reads. The system stays correct but becomes temporarily unavailable to some clients.
Real-world CP systems:
- MongoDB (with majority read/write concern) — if the primary node is unreachable, writes are rejected until a new primary is elected.
- Apache ZooKeeper — used for distributed coordination (leader election, config management). A ZooKeeper cluster that loses quorum stops accepting writes.
- etcd — the key-value store behind Kubernetes. Uses Raft consensus; a minority partition cannot serve reads or writes.
- Google Spanner — globally distributed SQL database that uses TrueTime (GPS + atomic clocks) to maintain strong consistency across continents.
When to choose CP: Financial transactions, inventory systems, leader election, configuration stores — anywhere stale data leads to incorrect decisions.
AP Systems: Availability + Partition Tolerance
An AP system chooses availability when a partition occurs. Every node keeps serving requests, even if it might return stale data. When the partition heals, nodes reconcile their differences.
Real-world AP systems:
- Apache Cassandra — every node can serve reads and writes independently. Uses tunable consistency levels (ONE, QUORUM, ALL) to slide between AP and CP behavior.
- Amazon DynamoDB — designed for "always on" availability. Uses eventual consistency by default; optional strong consistency available per-read.
- CouchDB — multi-master replication with conflict resolution. Designed for offline-first applications that sync later.
- DNS — the internet's naming system is a massively distributed AP system. DNS records propagate eventually; stale records are normal and expected.
When to choose AP: Shopping carts, social media feeds, analytics dashboards, caching layers, DNS — anywhere a slightly stale response is better than no response.
What About CA? (Consistency + Availability)
In theory, a CA system provides both consistency and availability — but gives up partition tolerance. In practice, CA systems do not exist in distributed environments because network partitions are inevitable. You cannot choose to ignore them.
A single-node PostgreSQL or MySQL database is effectively a CA system: it is always consistent, always available, and never has to worry about network partitions because there is only one node. The moment you add replication, you enter CAP territory and must choose between C and A during failures.
Eventual Consistency: The AP Compromise
Most AP systems use eventual consistency: if no new writes arrive, all replicas will eventually converge to the same value. The key question is how long is "eventually"?
- DynamoDB: typically converges within milliseconds (cross-region can take seconds).
- DNS: TTL-based; can take minutes to hours depending on cache settings.
- Cassandra: configurable via consistency levels.
QUORUMreads/writes give you strong consistency within the AP model.
Eventual consistency is not "broken" consistency. It is a deliberate trade-off that lets the system stay responsive during network issues, at the cost of temporarily serving slightly outdated data.
Tunable Consistency: The Modern Approach
Modern databases do not force you into a hard CP-or-AP choice. Many offer tunable consistency that lets you pick your trade-off per operation.
# Cassandra: tunable consistency per query
# Strong consistency (CP behavior):
SELECT * FROM users WHERE id = 123
USING CONSISTENCY QUORUM;
# Eventual consistency (AP behavior):
SELECT * FROM users WHERE id = 123
USING CONSISTENCY ONE;
# Rule of thumb: if R + W > N, you get strong consistency
# R = read replicas, W = write replicas, N = total replicas
# QUORUM = ceil((N+1)/2), so QUORUM + QUORUM > N
DynamoDB supports this too: every read can opt into ConsistentRead: true for strong consistency (at the cost of higher latency and no read from replicas).
PACELC: The Extension You Should Know
The CAP theorem only talks about what happens during a partition. But what about normal operation? The PACELC theorem extends CAP to cover both cases:
- PAC: During a Partition, choose between Availability and Consistency (this is CAP).
- ELC: Else (no partition), choose between Latency and Consistency.
Even when the network is healthy, maintaining strong consistency requires coordination between nodes (consensus protocols, synchronous replication) — which adds latency. Systems that optimize for consistency pay a latency tax. Systems that optimize for latency accept weaker consistency.
Partition?
A or C
(CAP theorem)
L or C
(Latency vs Consistency)
Common Misconceptions
- "CAP means pick 2 out of 3" — Misleading. Partition tolerance is not optional in any real distributed system. The real choice is between C and A during a partition.
- "MongoDB is CP, so it is always consistent" — Only if you use majority read/write concern. With default settings, MongoDB can return stale reads from secondaries.
- "Eventual consistency means data is wrong" — No. It means data might be temporarily stale. In practice, convergence is often sub-second.
- "CAP applies all the time" — The theorem only constrains behavior during partitions. When the network is healthy, most systems provide both C and A.
- "Single-node databases are immune to CAP" — True, but they have a single point of failure. The moment you replicate for high availability, CAP applies.
How to Choose: A Decision Framework
When designing a distributed system, ask these questions in order:
- Can I afford to show stale data? If a user sees a balance from 2 seconds ago, is that dangerous or just mildly annoying?
- Can I afford downtime during a network partition? If a data center goes offline, can users wait, or must the system keep serving?
- How long do partitions last in my infrastructure? Cloud providers have very brief partitions (seconds). Cross-region or edge deployments may have longer ones.
- Can different parts of my system make different choices? The payment service needs CP. The product recommendation feed needs AP. Design per-service, not per-system.
CAP in Practice: System Design Interview Patterns
In system design interviews, CAP shows up constantly. Here are the patterns:
- Banking/payments: CP (strong consistency). Use PostgreSQL with synchronous replication, or CockroachDB. Accept brief unavailability during failures.
- Social media feed: AP (eventual consistency). Use Cassandra or DynamoDB. A slightly stale feed is fine; a broken feed is not.
- Shopping cart: AP with conflict resolution. Amazon famously chose AP for its shopping cart (the paper that inspired DynamoDB). Merge conflicts — add both items, let the user resolve.
- Chat/messaging: CP for message ordering (you need causal consistency). AP for presence/typing indicators (stale is fine).
- Configuration store: CP (etcd, ZooKeeper). Wrong config is worse than temporary unavailability.
Summary
- The CAP theorem is a proven mathematical constraint, not a guideline.
- During a network partition, you must choose between consistency and availability.
- Partition tolerance is not optional in real distributed systems.
- Most modern databases offer tunable consistency — you choose per query.
- The PACELC extension adds the latency-vs-consistency trade-off during normal operation.
- Different parts of your system can (and should) make different CAP choices.