CAP Theorem

A distributed system can't fully guarantee both consistency and availability during a network failure.

What is it?

When data is spread across several machines (for reliability or scale), what happens if the network between them breaks? That break is a partition.

The CAP theorem says that during a partition, a distributed system must choose between:

  • Consistency: every node (each machine holding a copy of the data) gives the same, most up-to-date answer.
  • Availability: every node keeps responding to requests.

It can't fully guarantee both at once.

Partition tolerance (surviving a network split instead of the whole system failing) is treated as mandatory for any real distributed system. So in practice, CAP is a choice between consistency and availability when a partition happens.

Explain like I'm 10

Picture two bank branches that can no longer talk to each other. A customer wants to withdraw money at one. The bank can refuse until the branches can confirm with each other (consistency over availability), or allow it using local information and risk the branches disagreeing later (availability over consistency). While they're cut off, it can't give both an agreed answer and an instant one.

Examples

Two systems, two different choices during a partition

// CP system: refuses to answer rather than risk an inconsistent one
if (!canReachOtherNodes()) {
  throw new Error("Cannot guarantee consistency — refusing request");
}

// AP system: answers anyway, using whatever local data it has
if (!canReachOtherNodes()) {
  return localData; // might be stale, but still responds
}

How it works

With no partition, a well-designed distributed system can offer both strong consistency and full availability. CAP only bites when a partition happens.

Then a node that can't confirm with the rest must choose: refuse to answer (protecting consistency) or answer from its local data (protecting availability).

Why does it exist?

It makes an unavoidable trade-off explicit. Systems spread across machines will eventually hit network partitions, so designers must decide in advance whether consistency or availability matters more for their use case when that happens.

When to use it

Use CAP thinking when designing or choosing a distributed database or multi-node system. It gives you the question to ask ("what should happen here during a partition?"), not the answer, which depends on the application.

When not to use it

Don't treat CAP as a permanent label ("this database is CP" or "AP") for every situation. Many systems make different trade-offs for different operations, and the theorem is about behavior during partitions, not all the time.

Common mistakes

  • Treating CAP as 'pick 2 of 3 permanently'. Partition tolerance isn't optional for a real distributed system, so it's a choice between C and A during a partition.

  • Assuming a system must be fully CP or fully AP for everything. Many systems make different trade-offs for different kinds of operations.

  • Forgetting CAP only matters during an actual partition. Most of the time, a well-designed system can offer strong guarantees on both.

Practice exercises

  1. Easy:

    Explain in your own words what a 'network partition' is in the context of CAP.

  2. Medium:

    Describe a real feature where a system should favor availability over consistency during a partition, and why.

  3. Hard:

    Explain why partition tolerance is usually treated as mandatory rather than optional for a real distributed system.

Interview questions

What does the CAP theorem state?

During a network partition, a distributed system must choose between consistency (every node agrees on the latest data) and availability (every node keeps responding). It can't fully guarantee both at once.

Why is partition tolerance usually considered mandatory?

Partitions are a real, unavoidable risk for any system spread across machines. A system that can't tolerate them isn't really distributed in practice.

Give an example of choosing availability over consistency.

A shopping cart service that keeps accepting additions during a partition, reconciling conflicting versions later if needed, instead of blocking the user.