Part 2 of 6 · 2 min read
The CAP idea in plain words
Three letters, three definitions, one real choice.
Think of two bank branches that keep their own copy of your balance, joined by a phone line. The line goes dead. A customer asks branch B for the balance. Branch B can refuse to answer until the line is back, or it can answer with what it last knew. It cannot do both "answer now" and "answer with the very latest balance".
The interview book (volume 1) defines the three letters.
- Consistency: all clients see the same data at the same time, whichever node they connect to.
- Availability: any client that asks for data gets a response, even if some nodes are down.
- Partition tolerance: the system keeps operating despite a network partition, which is a communication break between nodes.
CAP theorem
The CAP theorem says that when a network partition happens, a replicated system must choose between consistency (every read sees the latest write) and availability (every request gets an answer).
Example: three replicas n1, n2 and n3 hold a balance. n3 loses contact with the others. A write to n1 cannot reach n3. Either n3 refuses requests (consistent, not available) or n3 answers with an old balance (available, not consistent).
The interview book's three-node walk-through:
- In the ideal world with no partition, a write to n1 is copied to n2 and n3, and the system is both consistent and available.
- When n3 is cut off, the system must choose. If it chooses consistency (a CP system), it blocks writes to n1 and n2 so that no copy gets ahead of another. The system is unavailable until the link is back. The book notes banks usually want this, since showing a wrong balance is serious.
- If it chooses availability (an AP system), n1 and n2 keep accepting writes, reads may be stale, and the data is synced to n3 when the partition heals.
The book also states why the "CA" system does not exist in the real world: network failure is unavoidable, so a distributed system must tolerate partitions.
CP and AP
A CP system keeps consistency and gives up availability during a partition. An AP system keeps availability and gives up consistency during a partition.
Example: a bank ledger that returns an error while replicas disagree is CP. A shopping-list app that lets both sides edit and merges later is AP.
A worked example with a counter. The counter reads 10 on all three nodes. The link to n3 breaks. A client adds 1 at n1 (now 11 at n1 and n2) and a different client reads at n3.
| Choice | n3 answers | After the link returns |
|---|---|---|
| CP | An error: "cannot reach the majority" | n3 catches up to 11, no mismatch ever shown |
| AP | 10 (stale) | n3 receives the update and shows 11 |
Same event, different promise. Neither choice is a bug. Each fits different features.
Check yourself:
- When does the CAP choice arise, and when does it not?
- Which would you choose for a bank balance and which for a "like" counter?