CAP Theorem
BeginnerThe CAP theorem states that a distributed system can guarantee at most two of three properties: Consistency, Availability, and Partition Tolerance. Since network partitions are inevitable, the real choice is between consistency and availability during a partition.
Overview
Proposed by Eric Brewer in 2000, the CAP theorem applies to any distributed data store. Consistency (C) means every read returns the most recent write. Availability (A) means every request receives a non-error response. Partition Tolerance (P) means the system continues operating despite network splits between nodes. In practice, network partitions will happen, so you must choose CP (sacrifice availability during partitions — e.g. HBase, MongoDB with majority reads) or AP (sacrifice consistency during partitions — e.g. Cassandra, DynamoDB). After the partition heals, AP systems converge via conflict resolution (last-write-wins, vector clocks, CRDTs). Most modern systems offer tunable consistency — you choose per-query where on the CP-AP spectrum you sit.
The Three Guarantees
Consistency means all nodes see the same data simultaneously. Availability means the system responds to every request. Partition Tolerance means the system works even when messages between nodes are lost or delayed.
// CAP visualisation
//
// Consistency (C)
// / \
// / \
// CP systems CA systems
// (HBase, (single-node
// MongoDB, RDBMS — not
// etcd) distributed)
// \ /
// \ /
// Partition Tolerance (P)
// / \
// / \
// AP systems
// (Cassandra, DynamoDB,
// CouchDB, Riak)
// \
// Availability (A)
// Network partitions are INEVITABLE in distributed systems
// → the real choice is C vs A during a partition.CP vs AP in Practice
CP systems reject requests (become unavailable) rather than return stale data during a partition. AP systems continue serving requests but may return stale data, resolving conflicts after the partition heals.
// CP example — MongoDB with majority read concern
db.orders.find({ _id: "order-123" })
.readConcern("majority") // waits for majority of replicas to confirm
.maxTimeMS(5000); // times out (unavailable) if partition prevents majority
// AP example — Cassandra with ONE consistency level
SELECT * FROM orders WHERE id = 'order-123';
-- CONSISTENCY ONE: returns data from nearest replica
-- Fast + available, but might be stale during partition
-- After partition heals, read-repair + anti-entropy sync data
// DynamoDB — tunable per-request
// Eventually consistent read (AP): lower latency, might be stale
// Strongly consistent read (CP): higher latency, always fresh
const params = {
TableName: 'Orders',
Key: { id: { S: 'order-123' } },
ConsistentRead: true // CP mode
};Beyond CAP — PACELC
PACELC extends CAP: if there is a Partition, choose A or C; Else (normal operation), choose Latency or Consistency. This captures the trade-off systems make even when the network is healthy.
// PACELC examples:
//
// System | Partition → | Else →
// ──────────────────────────────────────
// DynamoDB | PA | EL (fast reads, eventual consistency)
// Cassandra | PA | EL (tunable, defaults to eventual)
// MongoDB | PC | EC (majority writes, consistent reads)
// HBase/ZooKeeper | PC | EC (strong consistency, higher latency)
// CockroachDB | PC | EC (serializable by default)
// Cosmos DB | Tunable | Tunable (5 consistency levels)Key Points to Remember
- 1CAP: pick two of Consistency, Availability, Partition Tolerance — but P is mandatory in distributed systems.
- 2CP systems sacrifice availability during partitions (e.g. MongoDB majority reads, HBase, etcd).
- 3AP systems sacrifice consistency during partitions (e.g. Cassandra, DynamoDB eventually consistent reads).
- 4PACELC extends CAP to cover latency vs consistency trade-offs during normal operation.
- 5Many modern databases offer tunable consistency — choose per query or per table.
Interview Questions
Sign in to ask AriaExplain the CAP theorem with real-world examples.
Is it possible to have all three guarantees? Why or why not?
Classify DynamoDB, MongoDB, and Cassandra under CAP.
What is the PACELC extension and why does it matter?
How would you design a shopping cart service — CP or AP?
Ask Aria about CAP Theorem
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.