DEV Community

Cover image for The CAP Theorem Explained Simply: What Happens When the Network Splits
Abdeldjalil Chougui
Abdeldjalil Chougui

Posted on Originally published at abdeldjalilchougui.biz

The CAP Theorem Explained Simply: What Happens When the Network Splits

"Consistency, availability, partition tolerance: pick two." That is how the CAP theorem is usually summarised, and the summary is misleading enough that many developers come away with the wrong idea. CAP is not a menu of three features. It is a statement about one specific situation: what a distributed system can do when its nodes cannot talk to each other.

The three letters, precisely

CAP was proposed by Eric Brewer around 2000 and proved formally by Seth Gilbert and Nancy Lynch in 2002. In the proof, the terms have strict meanings:

  • C, Consistency: every read sees the most recent completed write, as if there were only one copy of the data. This is a strong guarantee called linearizability. It is not the "C" in ACID, which is about constraints and valid states.
  • A, Availability: every request sent to a node that is still running gets a non-error response. Not "most requests", not "the system is up somewhere": every working node answers.
  • P, Partition tolerance: the system keeps operating even when the network drops or delays messages between nodes, splitting them into groups that cannot reach each other.

Why "pick two" is the wrong picture

In a distributed system, network partitions are not optional. Cables get cut, switches fail, cloud zones lose connectivity, a long garbage-collection pause makes a node look dead. You cannot choose to "not tolerate" partitions; they happen to you.

So the real statement is:

When a network partition happens, a distributed system must choose between consistency and availability.

When there is no partition, a well-designed system can be both consistent and available. CAP says nothing about that case. (Its follow-up, PACELC, does.)

A concrete example

Take a simple key-value store with two nodes, one in Algiers and one in Paris. Both hold a copy of the key stock:812, currently 5. Normally, every write goes to both nodes.

Now the link between them breaks. A client in Algiers writes stock:812 = 4. The Algiers node cannot tell Paris. A moment later, a client in Paris reads stock:812.

The Paris node has two options:

  1. Answer with what it has: 5. The client gets a response, so the system is available, but the value is stale, so it is not consistent. This is AP behaviour.
  2. Refuse to answer (or wait until it can confirm with Algiers): return an error or time out. The client never sees a stale value, so the system stays consistent, but this node is not available. This is CP behaviour.

There is no third option that is both. Paris cannot know about a write it never received. That is the whole theorem, in one picture.

What CP systems look like

CP systems usually work with a majority (quorum). A write counts only when most replicas acknowledge it. During a partition, the side with the majority keeps working; the minority side refuses writes, and often linearizable reads too.

Examples: coordination services such as ZooKeeper and etcd, which use consensus algorithms (Zab and Raft) and are used for configuration, leader election and locks, where a wrong answer is worse than no answer. A traditional database with synchronous replication that refuses commits when the replica is unreachable also chooses consistency.

What AP systems look like

AP systems let every node accept reads and writes during a partition, and reconcile the differences once the network heals. Replicas may disagree for a while; they converge later. This is eventual consistency.

Reconciliation needs a rule: last write wins (by timestamp), keeping both versions for the application to merge, or data types designed to merge automatically (CRDTs).

Examples: Amazon's Dynamo design and databases inspired by it, such as Cassandra and Riak, and document stores built for offline sync such as CouchDB. A shopping cart is the classic use case: accepting an "add to cart" during a partition and merging later is better than refusing the customer.

Many of these systems are tunable. In Cassandra, you choose a consistency level per query: ONE favours availability, QUORUM on both reads and writes gives much stronger guarantees and loses availability when a majority is unreachable.

Where single-node databases fit

A PostgreSQL or MySQL server on one machine is not a distributed system, so CAP does not really apply: there is no partition between replicas. CAP becomes relevant the moment you add replicas and must decide what happens when they cannot talk. Do reads go to an asynchronous replica that may be behind? Then those reads are not linearizable. Does the primary wait for a synchronous replica? Then a lost replica can block writes.

Common misunderstandings

  • "We chose CA." For a system spread over a network, this means "we hope partitions never happen". When one does, the system will lose C or A anyway, possibly in an unplanned way.
  • "Our system is AP, so it is never consistent." AP systems are usually consistent most of the time. The choice applies during partitions.
  • "Availability means 99.99% uptime." In CAP, availability is a precise property of each response, not an SLA figure.
  • "Consistency in CAP is the C in ACID." They are different ideas with the same word.
  • Applying one label to a whole product. Many databases offer different guarantees per operation or per configuration. Ask what happens to your specific reads and writes during a partition.

How to use CAP in practice

When you design or pick a distributed data store, ask for each kind of data: if the nodes cannot talk, is a stale or conflicting answer acceptable, or is an error better?

  • Bank balances, inventory reservations, unique usernames, locks: a wrong answer is costly. Lean towards CP.
  • Shopping carts, likes, view counters, user feeds, caches: a slightly stale answer is fine. Lean towards AP.

Different parts of the same application can make different choices.

In short

  • CAP is about what happens during a network partition, not a pick-two menu.
  • During a partition, a node must either answer with possibly stale data (AP) or refuse until it can be sure (CP).
  • CP systems use majorities and sacrifice the minority side; AP systems accept writes everywhere and reconcile later.
  • Without partitions, you can have both; the follow-up question of latency versus consistency is what PACELC covers.

Originally published at abdeldjalilchougui.biz.

Top comments (0)