Home › Categories › Concepts & Theory › CAP Theorem

CAP Theorem

From WikiTech, the encyclopedia of technology

Concept · Concepts & Theory · 5 min read · 1,062 words · Updated

CAP Theorem
Concept
CAP Theorem — article photograph or illustration
Image placeholderPlace the file shown below in the assets/articles folder
File: cap-theorem.webp
Proposed2000 (Eric Brewer)
Proved2002 (Gilbert and Lynch)
FieldDistributed systems

The CAP theorem states that a distributed data store cannot simultaneously provide linearizable consistency and availability for every request while also tolerating network partitions.[1] When a partition occurs, the system has to give up one of the first two properties. The result is one of the best-known principles in the design of replicated databases and distributed services.

The name comes from the three properties involved: Consistency, Availability and Partition tolerance. The theorem gave engineers a precise vocabulary for a trade-off that practitioners had already felt, and it shaped the design of many large-scale storage systems built since the early 2000s.

History

Eric Brewer, a professor at the University of California, Berkeley and co-founder of Inktomi, presented the idea as a conjecture in a keynote at the ACM Symposium on Principles of Distributed Computing in 2000. He argued that web services built from many machines could choose between strong consistency and high availability but not both at once.

Seth Gilbert and Nancy Lynch of MIT published a formal proof in 2002, using an asynchronous network model.[1] Their paper turned the conjecture into a theorem and fixed the definitions precisely. Brewer revisited the topic in 2012, stressing that the popular summary was misleading and that the real design space is more nuanced.[2]

The three properties

  • Consistency in CAP means linearizability: every read returns the value of the most recent completed write, as if there were a single copy of the data. This is narrower than the word "consistency" in database transactions, where it refers to preserving integrity constraints.
  • Availability means that every request sent to a non-failing node eventually receives a non-error response. It says nothing about how fast the response arrives.
  • Partition tolerance means that the system keeps working when the network drops or delays an arbitrary number of messages between nodes.

Why the trade-off exists

Suppose two replicas of a value are separated by a network partition. A client writes to the first replica. A second client then reads from the other replica. The second replica cannot know about the write. It has two options: refuse to answer, or answer with possibly stale data. Refusing preserves consistency but sacrifices availability. Answering preserves availability but sacrifices consistency. No third option exists as long as the partition lasts.

Because real networks do fail, partitions cannot be ruled out. Partition tolerance is therefore not an optional feature that a designer can decline, and the practical choice is between consistency and availability during a failure. When the network is healthy, a system can offer both.

CP and AP systems

Systems are often labelled by the property they keep during a partition.

StyleBehaviour during a partitionTypical examples
CPRejects or delays requests that cannot be answered consistentlyZooKeeper, etcd, HBase
APKeeps answering and reconciles differences laterDynamo-style stores, Cassandra with low consistency levels

Coordination services such as ZooKeeper and etcd use consensus protocols, which require a majority of nodes to agree. A minority side of a partition stops serving writes. Dynamo-style stores, described by Amazon in 2007, instead accept writes on any reachable replica and use techniques such as vector clocks, hinted handoff and read repair to converge later, an approach known as eventual consistency.[3]

Many modern databases are tunable. Cassandra and similar systems let the client choose how many replicas must acknowledge each read or write, moving the same system along the spectrum between availability and consistency. A single-primary PostgreSQL deployment is not a distributed AP system, but its replication setups face the same choice when a standby cannot reach the primary.

PACELC and other refinements

CAP only describes behaviour during a partition, which is rare. In 2010 Daniel Abadi proposed PACELC to describe the rest of the time. It reads: if there is a partition (P), choose between availability (A) and consistency (C); else (E), choose between latency (L) and consistency (C). Even without failures, waiting for several replicas to agree takes time, so a strongly consistent system is slower than one that answers from the nearest copy.

Other formulations relax the strict definitions. Consistency models form a ladder that includes linearizability, sequential consistency, causal consistency and eventual consistency, and each rung has different availability guarantees. Research on conflict-free replicated data types (CRDTs) shows that some data structures can merge concurrent updates automatically without coordination.

Criticism and misconceptions

CAP is often summarised as "pick two of three". Brewer himself called this framing misleading, because partition tolerance is not optional in a network that can fail, and because the properties are not all-or-nothing.[2] Systems can offer partial consistency and partial availability, and the choice can be made per operation instead of per system.

The theorem's definition of availability is also very demanding, since every non-failing node must answer. Real systems are judged by looser measures such as percentage uptime. Some commercial systems advertise being both consistent and highly available; they do not break the theorem but make partitions extremely rare through private networks and redundant links, which shifts the engineering problem from logic to operations.

Practical guidance

Designers usually start from the requirements of the data. Account balances, inventory counts and uniqueness constraints often need strong consistency, even at the cost of rejecting requests during failures. Shopping carts, social feeds and view counters often tolerate temporary inconsistency in exchange for staying up. Documenting which operations fall into which group, and what the user sees during a partition, is part of the design rather than an afterthought. The theorem does not say which choice is correct; it says only that a choice must be made.

Editorial connections and engineering implications

What a network partition changes

The CAP theorem concerns a network partition that prevents some nodes from communicating. During that condition, a distributed system cannot guarantee both a single strongly consistent view of operations and a successful response from every reachable side. A design may reject or delay some operations to preserve consistency, or accept operations locally and reconcile them later to improve availability. CAP does not mean every system permanently chooses only two properties under all conditions.

The consistency in CAP is close to linearizability, not every meaning of consistency used in database discussions. Eventual consistency, causal consistency and read-your-writes guarantees are different models. Real deployments must also consider quorum sizes, replication lag, leader election, durability and recovery. Engineers should identify which user actions must remain available during a partition and which can be delayed. A shopping cart and a financial transfer may reasonably use different conflict policies. CAP is a starting point; retry safety and concurrent updates also involve idempotency and race conditions.

See also

  • PostgreSQL – An open-source object-relational database system with a strong emphasis on standards compliance and extensibility.
  • Race Condition – A defect in which the outcome of a program depends on the unpredictable timing of concurrent operations.
  • Idempotency – The property of an operation that produces the same result whether it is applied once or many times.
  • Microservices – An architectural style that structures an application as independently deployable services communicating through explicit interfaces.
  • Big-O Notation – A notation describing how the resource use of an algorithm grows as the input size grows.
  • Event Loop – A programming construct that waits for and dispatches events or messages in a single thread of control.
  • Garbage Collection – Automatic memory management in which a runtime reclaims memory that a program can no longer reach.
  • Domain Name System (DNS) – The distributed naming system that translates human-readable domain names into records used to locate internet services.

References

  1. ↑ Gilbert and Lynch (2002), Brewer's conjecture and the feasibility of consistent, available, partition-tolerant web services (doi.org)
  2. ↑ Brewer (2012), CAP twelve years later - how the rules have changed (doi.org)
  3. ↑ DeCandia et al. (2007), Dynamo - Amazon's highly available key-value store (allthingsdistributed.com)