CAP Theorem
A rule that says a distributed system can't fully guarantee consistency and availability at the same time during a network problem.
What is it?
When a database's data is spread across multiple machines (for reliability or scale), a real question arises: what happens if the network connection between those machines breaks — a partition? The CAP theorem states that, during a network partition, a distributed system must choose between consistency (every node — each machine holding a copy of the data — gives the exact same, most up-to-date answer) and availability (every node keeps responding to requests at all) — it cannot fully guarantee both at the same time.
Partition tolerance (surviving a network split at all, rather than the whole system failing) is treated as mandatory for any real distributed system — so in practice, CAP is really a choice between consistency and availability specifically when a partition happens.
Explain like I'm 10
Imagine two branches of a bank that can no longer communicate with each other. If a customer tries to withdraw money at one branch, the bank has two choices: refuse the withdrawal until the branches can confirm with each other (choosing consistency over availability), or allow it based on local information and risk the two branches disagreeing later (choosing availability over consistency). It can't guarantee both a confirmed, agreed-upon answer and an instant response while the branches are cut off from each other.
Examples
Two systems, two different choices during a partition
// CP system: refuses to answer rather than risk an inconsistent one
if (!canReachOtherNodes()) {
throw new Error("Cannot guarantee consistency — refusing request");
}
// AP system: answers anyway, using whatever local data it has
if (!canReachOtherNodes()) {
return localData; // might be stale, but still responds
}How it works
During normal operation, with no network partition, a well-designed distributed system can offer both strong consistency and full availability. The CAP theorem only bites specifically when a partition happens: at that moment, a node that can't confirm with the rest of the system must decide whether to refuse to answer (protecting consistency) or answer anyway using only what it has locally (protecting availability).
Why does it exist?
CAP theorem exists to make an unavoidable tradeoff explicit: distributed systems that span multiple machines will eventually experience network partitions, and designers must decide in advance which guarantee — consistency or availability — matters more for their specific use case when that happens.
When to use it
Use CAP thinking specifically when designing or choosing a distributed database or system that spans multiple nodes — it tells you what question to ask ("what should happen here during a network partition?") rather than a specific answer, since the right choice depends entirely on the application.
When not to use it
Don't treat CAP as a strict, permanent label ("this database is CP" or "this database is AP") for every situation — many real systems make different tradeoffs for different operations, and the theorem is really about behavior specifically during partitions, not all the time.
Common mistakes
Treating CAP as 'pick 2 of 3 permanently' — partition tolerance isn't really optional for a real distributed system, so it's actually a choice between C and A specifically during a partition.
Assuming a system must be either fully CP or fully AP for everything it does — many systems make different consistency/availability tradeoffs for different kinds of operations.
Forgetting that CAP only matters during an actual network partition — most of the time, a well-designed system can offer strong guarantees on both fronts.
Practice exercises
- Easy:
Explain, in your own words, what a 'network partition' is in the context of CAP.
- Medium:
Describe a real feature where you'd want a system to prioritize availability over consistency during a partition, and why.
- Hard:
Explain why partition tolerance is usually treated as mandatory rather than optional for a real distributed system.
Interview questions
What does the CAP theorem state?
During a network partition, a distributed system must choose between consistency (every node agrees on the latest data) and availability (every node keeps responding), and can't fully guarantee both at once.
Why is partition tolerance usually considered mandatory?
Because network partitions are a real, unavoidable possibility in any system spread across multiple machines — a system that can't tolerate them at all isn't really distributed in practice.
Give an example of choosing availability over consistency.
A shopping cart service that keeps accepting additions during a partition, even if it means occasionally reconciling conflicting versions later, rather than blocking the user entirely.