When the network breaks
After this: Choose between a stale answer and an error while copies cannot talk, and state CAP carefully.
On this page · 4 sections · 3 checks
A company keeps its primary in one data centre and a replica in another, far away, so that users nearby read from a close copy. One day the network link between the two data centres breaks. Both buildings still run. A user asks the replica for a value. The replica cannot reach the primary to learn about new writes. What should it answer?
When copies cannot talk
A break in the network that leaves machines running but unable to reach each other is a , or a partition for short. While it lasts, the replica has two honest options.
- Answer with what it has. The user gets a reply, but it may be old. A system that always answers is .
- Refuse. The user gets an error, but never an old value. A system where every read sees the latest write is .
A worked example
Both copies hold x = 1, and every write is copied across at once while the link works.
- Step 1: a read at the replica returns 1.
- Step 2: nothing happens.
- Step 3: the link breaks.
- Step 4: x = 2 is written at the primary. The copy cannot cross.
- Step 5: a read at the replica. Staying available, it answers 1, which is stale. Staying consistent, it answers with an error, and that request fails.
- Partition · AvailablePress Run it and step through. The link between the two data centres breaks at step 3, and x = 2 is written at step 4. Read what the replica answers at step 5. Then switch the policy to consistent and run it again.
Press Run it, then step through and watch the requests move.
Neither answer is wrong. They are two different promises. For a count of likes, or a profile picture, an old value for a few minutes is fine, and an error would annoy people. For a bank balance before a payment, an old value could let someone spend the same money twice, so an error is better.
CAP in plain words
The is named after three letters: consistency, availability and partition tolerance. Stated carefully, it says this:
While a network partition lasts, a system that keeps copies of data must choose: answer every request, possibly with old data, or give only the latest data and refuse some requests.
Three things it does not say:
- It is not "pick any two of three". Partitions happen whether you choose them or not, because networks break. The real choice is between consistency and availability, and only during a partition.
- It says nothing about the time without a partition. While the network works, a system can be both consistent and available. Keeping the copies in step then costs time instead: a read or a write may wait for the copies to agree. (That second trade-off has its own name, PACELC; this tutorial does not cover it.)
- Its C is not the C in ACID. Database books use the letters ACID for promises about transactions, groups of changes that succeed or fail together. Their C means a change keeps the data's rules, such as a column that may not be empty. CAP's C means every read sees the latest write.
Check yourself
Both copies hold x = 1. The link between them breaks, then x = 2 is written at the primary. A user reads x at the replica. What does each policy answer?
The network between the copies is working normally. Must the system choose between giving every answer and giving only the latest value?
Which statement about CAP is right?
- Say what a network partition is: machines still running but unable to reach each other.
- Say what each choice answers during a partition: available gives what it has, possibly old; consistent refuses with an error.
- State CAP carefully: while a partition lasts, a system that keeps copies must choose between answering and giving only the latest data.
- Reject the overclaims: it is not pick two of three, it forces no choice without a partition, and its C is not the C in ACID.
- Choose the promise for the data: an old count of likes for a few minutes is fine, while a bank balance before a payment should give the latest value or an error.