CAP Theorem: Consistency vs Availability Trade-offs
See what CAP guarantees during a network partition, define consistency and availability, and distinguish the theorem from quorum and recovery choices.
CAP describes the trade-off a distributed data service faces during a network partition: it cannot guarantee both linearizable operations and a successful response to every request. This guide defines the theorem's terms, separates CAP from quorum tuning and recovery, and shows how to reason about the guarantees a particular operation needs.
Understanding the CAP Theorem
CAP is about a distributed data service during a network partition. While the partition lasts, the service cannot guarantee both linearizable operations and a successful response to every request. It must reject or delay some operations, or answer with results that may not reflect the latest completed write. CAP is not a permanent “pick two” label for an entire database.
Introduction
Eric Brewer introduced the conjecture in 2000; Seth Gilbert and Nancy Lynch published a formal proof in 2002. In their model, a partition prevents nodes from exchanging messages reliably. A system that continues to tolerate that partition cannot also provide both atomic consistency and availability for every request. See Gilbert and Lynch’s formal proof.
The familiar “two of three” shorthand hides the condition that matters: the trade-off is exposed when there is a partition. If the network is healthy, CAP alone does not decide what consistency or availability a system should offer.
graph TB
A["CAP Theorem"]
A --> B["Consistency (C)"]
A --> C["Availability (A)"]
A --> D["Partition Tolerance (P)"]
B --- E["Choose C or A when partition occurs"]
C --- E
D --- E
Core Concepts
Consistency (C)
Atomic consistency (linearizability): each completed operation appears to take effect at one instant between its start and completion, in an order that respects real-time ordering.
For a read that starts after a write has completed, a linearizable service returns that write or a later one. It does not require every node to hold identical bytes at every instant; the guarantee is about the results clients observe. A service can preserve it by waiting for the necessary coordination or by refusing a request when it cannot establish the required order.
// Example: Consistent read
// After writing x = 5 to node A, any subsequent read from any node must return 5
await write("x", 5); // Write to node A
const result = await read("x"); // Must return 5 from any node
Availability (A)
Every request to a non-failing node eventually receives a non-error response.
Availability in the CAP model is a progress guarantee, not simply “return stale data.” A response may be stale, but an error or an indefinitely delayed response does not satisfy this definition.
// Example: Available read
// Even if some nodes are down, the system returns a response
try {
const result = await read("x"); // Returns cached/stale data if needed
return result;
} catch (error) {
// Must NOT happen in an available system
}
Partition Tolerance (P)
The system continues to operate despite messages being lost or delayed between nodes.
The theorem asks what the service guarantees when parts of the system cannot communicate. A design that tolerates a partition may still choose to stop or delay some operations to preserve linearizability.
Applying CAP to one operation
A useful CAP question is concrete: during a partition, what should this operation promise? For an inventory reservation, an application might require a linearizable check-and-decrement so two isolated replicas cannot both promise the last item. If the service cannot coordinate, it can reject or wait for the request. A different operation, such as recording a view count, may accept a successful local write and reconcile later. These are application requirements; the database’s exact behavior depends on its product, topology, and configuration.
This example does not make a whole database permanently “CP” or “AP.” CAP describes guarantees under partition in a formal model. Quorum settings and product consistency options are implementation mechanisms with their own documented semantics. Do not infer a CAP guarantee from a setting name alone.
What CAP does not decide
CAP does not tell you how replicas reconcile after connectivity returns, which consistency model fits each read, or how much storage and compute to provision. Those need separate designs and measurements:
- For linearizability, serializability, and weaker read guarantees, see Consistency Models.
- For recovery objectives and restoring service after failures, see Disaster Recovery.
- For measured storage, memory, and concurrency estimates, see Database Capacity Planning.
For the theorem’s formal result, see Gilbert and Lynch’s proof. The exact statement depends on the model’s assumptions; the practical takeaway is to state the guarantee your operation needs while a partition is active, then verify the chosen product and configuration provide it.
Category
Related Posts
Distributed Systems Primer: Key Concepts for Modern Architecture
A practical introduction to distributed systems fundamentals. Learn about failure modes, replication strategies, consensus algorithms, and the core challenges of building distributed software.
The Eight Fallacies of Distributed Computing
Explore the classic assumptions developers make about networked systems that lead to failures. Learn how to avoid these pitfalls in distributed architecture.
Microservices vs Monolith: Choosing the Right Architecture
Understand the fundamental differences between monolithic and microservices architectures, their trade-offs, and how to decide which approach fits your project.