Home / System Design / The CAP Theorem

๐Ÿ”บ The CAP Theorem

Fundamentals โฑ 6 min read

The CAP theorem is the classic rule for reasoning about distributed databases. It says that during a network partition, a distributed system can guarantee at most two of three properties. Understanding it helps you justify database choices.

The three properties

  • Consistency (C) โ€” every read sees the most recent write (all nodes agree).
  • Availability (A) โ€” every request gets a (non-error) response, even if some nodes are down.
  • Partition tolerance (P) โ€” the system keeps working when the network between nodes drops messages.

Why you must choose during a partition

In any real distributed system, network partitions will happen, so P is not optional. When a partition occurs you must choose:

  • CP โ€” stay consistent by rejecting requests that can't be confirmed (sacrifice availability). Example: many SQL/consensus systems.
  • AP โ€” stay available by answering with possibly stale data (sacrifice strong consistency). Example: Cassandra, DynamoDB with eventual consistency.

Eventual consistency

AP systems are usually eventually consistent โ€” after writes stop, all replicas converge to the same value given enough time. That's perfectly fine for things like social feeds or like-counts, but not for a bank balance.

PACELC โ€” the fuller picture

CAP only talks about behaviour during a partition. PACELC extends it: if there is a Partition, choose between Availability and Consistency; Else (normal operation) choose between Latency and Consistency. It captures the everyday trade-off that even without partitions, stronger consistency costs latency.

Frequently Asked Questions

Can a system be CA (consistent and available)?
Only if there are no network partitions, which is unrealistic for a distributed system. Since partitions will happen, real distributed systems must be partition tolerant and choose between consistency and availability during a partition.
What is eventual consistency?
It means replicas may temporarily disagree after a write but will converge to the same value once updates stop propagating. It is common in highly available (AP) systems.