Skip to content

The CAP Theorem

One-Liner

A distributed system can only provide two of three guarantees: Consistency, Availability, and Partition Tolerance.

What It Is

A fundamental theorem in distributed systems that states it is impossible for a distributed data store to simultaneously provide more than two of the following three guarantees: Consistency, Availability, and Partition Tolerance.

Why It Exists

To force system designers to consciously choose which guarantees to prioritize when facing network partitions, which are inevitable in distributed systems.

How It Works

  • Consistency (C): Every read receives the most recent write or an error.
  • Availability (A): Every request receives a (non-error) response, without the guarantee that it contains the most recent write.
  • Partition Tolerance (P): The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes.

In a distributed system, you must choose P, which means you are forced to trade off between C and A.

Tradeoffs

Pros

  • CP (Consistency/Partition Tolerance): The system remains consistent even during a partition.
  • AP (Availability/Partition Tolerance): The system remains available during a partition.

Cons

  • CP (Consistency/Partition Tolerance): May become unavailable to some clients.
  • AP (Availability/Partition Tolerance): Some clients may see stale data.

Failure Modes

  • Split-brain in AP systems: Nodes in different partitions can accept conflicting writes.
  • Cascading unavailability in CP systems: A partition can cause large parts of the system to become read-only or completely unavailable.

Interview Traps

  • Forgetting that in practice, the choice is always between C and A, because P (partitions) are a fact of life in distributed systems.
  • Treating the CAP theorem as a binary choice, rather than a spectrum of tradeoffs.

Anti-Patterns

  • Claiming a system is “CAP” (all three).
  • Ignoring the theorem and not having a clear strategy for handling partitions.
  • Consistency Models
  • Distributed Consensus