CAP Theorem

Distributed Systems
03 / 21

In a distributed system, during a network partition, you must choose between Consistency and Availability - you cannot have both.

CAPConsistencyAvailabilityPartition ToleranceFAIL
SD blueprint: CAP Theorem Partition
The Analogy (Read This First)

You run two bank branches. The network connecting them breaks (Partition). A customer deposits $1,000 at Branch A. Now Branch B is contacted for the balance. Do you: (A) show the correct $1,000 balance but pause service until sync completes (Consistency), or (B) immediately show the old balance but keep serving customers (Availability)? You just lived CAP Theorem.

Deep Dive Analysis

CAP stands for Consistency, Availability, and Partition Tolerance. Proved by Eric Brewer in 2000, CAP Theorem states that a distributed data store can only guarantee two of these three properties simultaneously.

Consistency (C): Every read receives the most recent write or an error. All nodes see the same data at the same time. Reads are never stale.

Availability (A): Every request receives a (non-error) response, but it might not contain the most recent data. The system is always responsive.

Partition Tolerance (P): The system continues to operate despite arbitrary network partitions (messages being dropped or delayed between nodes).

The critical insight: in any real distributed system, network partitions are inevitable. Networks fail. Packets drop. Cables get cut. So P is not optional - you must tolerate partitions. This means your real choice is between C and A when a partition occurs.

CP Systems (Consistency + Partition Tolerance): During a partition, these systems will refuse to answer (return an error) rather than return stale data. Examples: HBase, Zookeeper, etcd, MongoDB (in strict mode). Banking systems often choose CP - better to be unavailable for 5 minutes than to show wrong balances.

AP Systems (Availability + Partition Tolerance): During a partition, these systems keep responding but may return stale/inconsistent data. They eventually become consistent when the partition heals. Examples: Cassandra, DynamoDB, CouchDB, DNS. Social media "like counts" are fine being slightly stale.

Key Bullets
  • P (Partition Tolerance) is mandatory for real distributed systems.
  • The real choice is CP vs AP during failures.
  • CP: Bank transactions, inventory management, leader elections.
  • AP: Social feeds, shopping carts, DNS, caching systems.
  • PACELC is a more nuanced extension: even without partitions, there is a Latency-Consistency tradeoff.
Trade-offs
CP: Strong consistency at the cost of availability during partitionsAP: Always available but potentially stale dataNo free lunch - pick what your use case can tolerate
Real-World Examples
CP: ZooKeeper (Kafka's leader election)AP: Cassandra (eventually consistent writes)AP: DynamoDB (default AP, consistent reads available at higher latency/cost)

Curated Curation & Deep Insights

ScaleDojo Certified
Video Tutorial Pending

Our system architects are vetting high-quality, authorized video guides for CAP Theorem with zero third-party platform links.

Verification in Progress
Reading Material Pending

We are preparing premium, zero-competitor deep-dives for CAP Theorem. Authorized reading references will appear automatically.

Verification in Progress
ScaleDojo Logo
Initializing ScaleDojo