What is the CAP theorem?

The CAP theorem says that when a network partition splits a distributed data store, it can't be both consistent and available. Each node must either refuse requests or risk returning stale data.

Also called Brewer's theorem, CAP principleUpdated

The CAP theorem says a distributed data store can't guarantee all three of Consistency, Availability and Partition tolerance at once. CAP is the full form. Eric Brewer of the University of California, Berkeley, presented it as a conjecture in his keynote at the 2000 ACM Symposium on Principles of Distributed Computing (PODC). In 2002, Seth Gilbert and Nancy Lynch of MIT published a formal proof, which made it a theorem.

The three properties, precisely

  • Consistency: every read returns the most recent write, or an error. All clients see the same data at the same time. This is not the C in ACID.
  • Availability: every request to a node that hasn't failed gets a non-error response, though it may not hold the latest data.
  • Partition tolerance: the system keeps working when messages between nodes are lost or delayed.

Why it's really a choice between C and A

Networks do fail, so any system spread over several machines has to tolerate partitions. The real question is what a node does when it can't reach the others:

  • CP: refuse the request or time out, rather than risk returning stale or conflicting data.
  • AP: answer with what it has, and reconcile later.

When there is no partition, a system can be both consistent and available. Brewer said as much in his 2012 article "CAP Twelve Years Later", where he called the popular "pick two of three" framing misleading.

A booking system runs replicas in Mumbai and Hyderabad. The link between them drops for 90 seconds. A customer tries to book the last seat on a flight through the Mumbai replica.

  • CP choice. Mumbai can't confirm with Hyderabad that the seat is still free, so it returns "please try again". There's no double booking, but some customers can't book for 90 seconds.
  • AP choice. Mumbai accepts the booking. Meanwhile Hyderabad sells the same seat to someone else. When the link returns, the system detects the conflict and one passenger is moved or compensated.

For seat inventory, payments and ledgers, CP is usually right. For a shopping cart, likes or a product catalogue, AP with eventual consistency is usually fine. Werner Vogels describes Amazon's shopping cart this way: it keeps accepting items on both sides of a partition and merges the carts once the partition heals.

CAP in real databases

Most modern databases let you tune the choice per operation rather than fixing it. MongoDB, with majority writes and reads from the primary, gives up availability in a partition. Cassandra, reading and writing at consistency level ONE, stays available and may return stale data. Raising both to QUORUM makes reads overlap the latest write. Labels such as "MongoDB is CP" therefore oversimplify, and researchers such as Martin Kleppmann have argued against using them at all.

PACELC

Daniel Abadi proposed PACELC in 2010 to cover what CAP leaves out. If there is a Partition, choose Availability or Consistency. Else, in normal operation, choose Latency or Consistency. Keeping replicas in different regions in step costs latency on every write, not only during failures. In Abadi's terms, Dynamo-style stores such as Cassandra are PA/EL, and fully ACID systems are PC/EC.

For choosing a database in practice, see DynamoDB vs PostgreSQL and PostgreSQL vs MongoDB for fintech.

Any idea in mind? Let's make it real.

Tell us what you're building. You'll hear back from an engineer, not a sales team, usually within one working day.

Book a 30-min call