Designbench

Lesson 2.2 · 15 min

CAP theorem

Cut the link between three replicas and watch a CP system return errors while an AP system serves stale data and loses writes on heal.

The idea in plain words

Imagine two bank branches that keep each other’s records in sync over a phone line. If the line goes dead, each branch must choose: stop serving customers until the line is back, or keep serving and risk the two sets of records disagreeing.

The problem it solves

Important data is copied to several machines so it survives failures. When the network between those copies breaks, the copies can no longer agree, and every request forces a choice.

How it works

  1. Normally all copies stay in sync, and the system is both consistent and available.
  2. Then a network partition splits them: some copies can’t reach the others.
  3. A copy that gets a request must pick one: refuse (or wait) because it can’t confirm it has the latest data, which keeps it consistent (CP); or answer with what it has, which might be out of date, which keeps it available (AP).
  4. When the network heals, an AP system must merge the changes made on both sides.

Example

Your bank balance is $100, stored in Vancouver and Toronto. The link between them goes down. You try to withdraw $80 in Vancouver while your partner withdraws $80 in Toronto. A CP bank refuses one of them until the link is back; an AP system allows both and finds −$60 later. Banks choose CP for balances; a social feed choosing AP just shows a slightly old post.

Key terms

C: Consistency
Every read sees the latest write. All copies of the data agree, as if there were only one.
A: Availability
Every request to a working node gets an answer, even if that answer might be out of date.
P: Partition tolerance
The system keeps running even when the network splits and some nodes can’t reach others.
Network partition
A break in the network that cuts the nodes into groups that can’t talk to each other.
Replica
One of several copies of the same data, kept on different machines.

In this lesson you'll learn

Sections

  1. Theory: split the network
  2. Theory: PACELC
  3. Design: pick a side
  4. Check yourself

Cheat sheet

During a partitionrefuse (C) or answer stale (A)
CAnot an option over a network
C in CAPlinearizable, not ACID C
Quorum overlapR + W > N
PACELCelse: latency vs consistency

The interactive simulations and the quiz load with JavaScript.