CAP Theorem

Difficulty: Advanced

Question

Explain the CAP theorem. Why do people say you must choose between CP and AP, and what does that mean for real databases?

Answer

The CAP theorem says that when a distributed system experiences a network partition, it must choose between Consistency (every read returns the most recent write) and Availability (every request to a non-failed node gets a non-error response). You cannot have both during the partition. Because partitions happen in any real network, the practical choice is between CP (refuse some requests to stay correct) and AP (keep answering, possibly with stale data).

What the three letters precisely mean

  • Consistency in CAP means linearizability: the system behaves as if there were a single copy of the data. This is not the "C" in ACID.
  • Availability means every request received by a non-failing node eventually gets a successful response. A timeout or "try later" counts as unavailable.
  • Partition tolerance means the system keeps operating even when messages between nodes are dropped or delayed.

The "pick two of three" phrasing is misleading. You cannot opt out of partitions, so "CA" only describes a single-node system. Eric Brewer himself clarified in 2012 that the choice only arises during a partition; the rest of the time you can have both.

Walking through a partition

Two replicas of a Swiggy restaurant-status store, one in Mumbai and one in Chennai, lose connectivity. A write arrives in Mumbai marking a restaurant closed. A read hits Chennai. Chennai has two options:

  1. CP: refuse the read (or the earlier write) because it cannot confirm it has the latest value. Correct, but some users see errors.
  2. AP: return the last value it knows (open). Users always get an answer, but it may be stale until the partition heals and replicas reconcile.

CP vs AP in real systems

ChoiceBehaviour during partitionExamples
CPMinority side rejects writes or readsetcd, ZooKeeper, HBase, Spanner, MongoDB with majority write/read concerns
APAll sides accept requests, reconcile laterCassandra, DynamoDB (default eventually consistent reads), Riak, CouchDB

Many databases are tunable: Cassandra with QUORUM reads and writes (R + W > N) behaves much closer to CP for a given key, while ONE favours availability. So label configurations, not products.

PACELC: the more useful framing

CAP only talks about partitions. PACELC adds: if Partition, choose A or C; Else, choose Latency or Consistency. Even with a healthy network, synchronous replication to guarantee consistency adds latency. DynamoDB and Cassandra are PA/EL; Spanner and traditional synchronous setups are PC/EC. This is the trade-off engineers actually face daily.

How to pick

Use CP where wrong answers cost money or safety: payment ledgers, inventory decrements, leader election, distributed locks. Use AP where a briefly stale answer is fine and downtime is not: product catalogues, feeds, likes counters, shopping carts that can merge.

A read during a network partition. The isolated replica must either refuse the read (CP) or serve possibly stale data (AP).
  1. Client A → Replica Mumbai: Write status = closed
  2. Replica Mumbai → Replica Chennai: Replicate (dropped by partition)
  3. Client B → Replica Chennai: Read status
  4. Replica Chennai → Client B: CP: error, cannot confirm latest
  5. Replica Chennai → Client B: AP: open (stale value)

Code examples

Tuning consistency per query in Cassandra

-- Replication factor 3 per data centre
CREATE KEYSPACE shop WITH replication =
  {'class': 'NetworkTopologyStrategy', 'dc_mumbai': 3};

-- In cqlsh:
CONSISTENCY QUORUM;   -- 2 of 3 replicas must respond
INSERT INTO shop.stock (sku, qty) VALUES ('SKU-1', 10);
SELECT qty FROM shop.stock WHERE sku = 'SKU-1';
-- R(2) + W(2) > N(3): reads see the latest acknowledged write

CONSISTENCY ONE;      -- any single replica; more available, may be stale
SELECT qty FROM shop.stock WHERE sku = 'SKU-1';

The same database can lean CP or AP per request. Quorum overlap gives strong reads for a key, at the cost of failing when a majority of replicas is unreachable.

Key points

  • CAP consistency means linearizability, not ACID consistency.
  • Partitions are unavoidable, so the real choice is CP or AP during a partition.
  • "CA" only describes single-node systems.
  • Many databases are tunable per query (quorums), so classify configurations, not products.
  • PACELC adds the latency vs consistency trade-off when there is no partition.

Common follow-up questions

  • What is PACELC and why is it more practical than CAP?
  • How do quorum reads and writes (R + W > N) give strong consistency?
  • Is MongoDB CP or AP?
  • What is eventual consistency and how do replicas converge?

Interview tips

  • Correct the "pick any two" myth politely; it shows depth.
  • Give a concrete partition scenario and explain both possible behaviours.
  • Mention PACELC and tunable consistency to stand out from textbook answers.

Concepts covered

Consistency, Availability, Partition Tolerance, PACELC, Linearizability, Quorum

More Database Design questions