System Design Fundamentals

0% completed

Introduction to CAP Theorem

The Three Things You Want

The Claim

Where the Choice Actually Happens

Why You Do Not Get to Skip Partitions

What This Looks Like in Products You Use

Every distributed system runs on more than one machine, and those machines talk to each other over a network. Networks fail. Cables get cut, switches die, a data center loses its uplink for ninety seconds.

The CAP theorem is the rule that tells you what your system is allowed to do when that happens. It was introduced by the computer scientist Eric Brewer.

The Three Things You Want

A distributed system that stores data usually wants three things at once.

  1. Consistency (C). Every read returns the most recent write, no matter which machine answers it.
  2. Availability (A). Every request gets an answer. The system never refuses to respond.
  3. Partition tolerance (P). The system keeps working even when the network splits and some machines cannot reach the others.

All three sound reasonable. All three sound like things you would put in a design document without arguing.

The Claim

The CAP theorem says you cannot have all three at the same time. A distributed system can guarantee at most two of the three at any given moment.

The CAP theorem: a distributed system can hold at most two of the three properties, which gives the three familiar pairs CP, AP and CA
The CAP theorem: a distributed system can hold at most two of the three properties, which gives the three familiar pairs CP, AP and CA

That is the whole theorem. The reason it matters is not the arithmetic. It is what the arithmetic forces you to decide.

Where the Choice Actually Happens

While the network is healthy, there is no problem. Every machine can reach every other machine, so the system can be consistent and available at the same time. Nothing is being given up.

The choice arrives the moment the network splits. A split like this, where some machines can no longer reach the others, is called a network partition.

Now picture a system with machines on both sides of that split, and a write arriving on one side.

  • If the system answers the write, the two sides now hold different data. It stayed available and gave up consistency.
  • If the system refuses the write, the data stays in agreement, but somebody was told no. It stayed consistent and gave up availability.
When a partition splits the network, a write arriving on one side forces a choice: answer it and let the two sides disagree, or refuse it and stay in agreement
When a partition splits the network, a write arriving on one side forces a choice: answer it and let the two sides disagree, or refuse it and stay in agreement

There is no third option. You cannot accept the write and keep both sides identical, because the two sides cannot talk to each other. That is what a partition means.

Why You Do Not Get to Skip Partitions

Reading the theorem, it is tempting to pick consistency and availability and simply not tolerate partitions.

You cannot. Partition tolerance is not a feature you switch on. It is a description of what happens to your system when the network breaks, and the network will break. Choosing "no partition tolerance" means choosing to fall over when it does.

So in practice, P is a given. The real question the CAP theorem asks you is much narrower:

When the network splits, would you rather be wrong or be silent?

What This Looks Like in Products You Use

You have already seen both answers in the wild.

A banking app that will not show your balance during an outage chose to be silent. A wrong balance is worse than no balance, so the system refuses rather than guesses.

A social feed that shows you a slightly old set of posts during an outage chose to be wrong, in a small and harmless way. A stale post is better than an error page, so the system answers with whatever it has.

Neither one is the correct answer in general. They are correct answers for different products.

💡 Interviewers rarely ask you to recite the CAP theorem. They describe a system and listen for whether you notice that a choice exists. "Since this is distributed, a partition will happen eventually, and for a payments ledger I would rather reject the write than accept two conflicting ones" is the answer they are waiting for.

Key takeaway: The CAP theorem says a distributed system can guarantee at most two of consistency, availability and partition tolerance. Since partitions are unavoidable, the choice you actually make is between consistency and availability during a partition.

In the next lesson, Components of CAP Theorem, we will look at each of the three properties closely, because each one means something more specific than its everyday name suggests.

Mark as Completed
Sid D

Sid D

· a year ago

Does it mean that an RDBMS isn’t partition tolerant? Not functional if network goes down between nodes?

how does it provide C & A at the same time? Can you give an example

Show 1 reply
S

Sunderamurthy

· 2 years ago

The CAP theorem is often misrepresented as choosing two properties out of three. In reality, it was created to raise awareness among developers about the impossibility of achieving both consistency and availability in the event of a network partition.

On This Page

The Three Things You Want

The Claim

Where the Choice Actually Happens

Why You Do Not Get to Skip Partitions

What This Looks Like in Products You Use