Two branches
A shop has two branches with a phone line between them. When the price list changes in one, it phones the other, so a shopper gets the same answer wherever they ask. While the line works, everything is easy: both branches are open, and both agree.
Replicas
Distributed databases keep replicas on several machines, for speed and to survive failures. Writes on one replica are copied to the others over the network. The CAP theorem talks about three properties:
- Consistency: every read sees the latest write, as if there were one copy.
- Availability: every request to a working node gets a (non-error) answer.
- Partition tolerance: the system keeps working when the network loses messages between nodes.
The line goes down
Then a storm cuts the phone line. Each branch can either close its doors to changes until the line is back, so nobody ever gets a wrong price, or stay open and accept that the two branches may disagree for a while. There's no third option: a branch can't know what it hasn't been told.
Pick one during a partition
Networks do partition, so P isn't optional. CAP says that during a partition a system must give up either C or A:
| Choice | Behaviour during a partition | Examples |
|---|---|---|
| CP | Refuse or block requests that can't be confirmed with a majority | etcd, ZooKeeper, HBase, Spanner |
| AP | Keep answering; replicas diverge and reconcile later | Cassandra, DynamoDB, Riak |
Without a partition, a system can be both consistent and available.
Staying open
A shop that stays open has to tidy up afterwards. When the line comes back, the branches compare notes and agree on the latest price. For a while, shoppers might have seen different prices, but eventually everyone sees the same one.
Eventual consistency
AP systems promise eventual consistency: if writes stop, all replicas converge. Conflicting writes are resolved by rules such as last-write-wins (by timestamp), version vectors, or data types that merge automatically (CRDTs).
Many systems let you tune this per request. Cassandra's QUORUM reads and writes overlap on at least one replica,
giving consistent results while a majority is reachable.
When the line comes back
Staying open has a cost. Say the north branch is told the cake is ยฃ6 and, a minute later, the south branch is told ยฃ7. Neither hears the other. When the line returns, the branches find two different prices for one cake. Someone must choose.
A simple rule is "the latest time on the note wins". It is quick, but the other change is thrown away, and the shopper who asked for it is never told. A careful shop keeps both notes and asks a person to decide.
The branch that closed has its own trouble. A shopper who walks in during the storm is sent away, even though the shelves are full. And even in good weather, waiting for a call to the other branch before every answer makes every shopper wait a little longer.
What can go wrong
- A CP system refuses writes on the minority side. A node that cannot reach a majority cannot confirm a write,
so it errors or blocks. You see timeouts and "no leader" or "unavailable" errors on that side only, while the
majority side works. Cassandra at
QUORUMbehaves this way: the coordinator throwsUnavailableExceptionwhen it knows too few replicas are alive, orWriteTimeoutExceptionif they don't answer in time; in etcd the minority side is unavailable and requests to it time out. Fix: retry against the majority side, set client timeouts, and use consensus groups of 3 or 5 so a single failure keeps a majority. - An AP system accepts both sides, then conflicts. Both replicas acknowledge writes to the same key. After the
partition heals, one must win. With last-write-wins (Cassandra, DynamoDB global tables in the default multi-Region eventual consistency mode), the later timestamp wins and the other
acknowledged write is silently lost. If clocks are skewed, the "later" write may be the older one. You see a
value that reverts, with no error anywhere. Fix: keep both as siblings (Riak with
allow_mult) and merge in the application, detect concurrency with vector clocks, or use CRDTs that merge by design. - Stale reads after the partition heals. Healing is not instant: replicas catch up through repair and
hinted handoff, so a read from a lagging replica returns old data. You see a client read its own write back as the old value
(read-your-writes broken). Fix: write and read at
QUORUM(soR + W > N), read from the primary, or pin a user's session to one replica. - Latency versus consistency with no partition (PACELC). Waiting for replicas costs time even on a healthy network; see the PACELC note below.
| Choice | You gain | You pay |
|---|---|---|
| CP (refuse on the minority side) | Every read sees the latest write; no conflicts | Errors and timeouts for clients cut off from the majority |
| AP (answer on both sides) | Always answers, even when partitioned | Conflicts, lost writes under LWW, stale reads |
Quorums with R + W > N | Read and write sets overlap, so a read sees the latest acknowledged write | Each request waits for more replicas; fewer failures tolerated |
| Strong versus eventual reads | Strong: latest value. Eventual: faster and cheaper | Strong: more latency and less availability. Eventual: stale data |
With N replicas, a read of R and a write of W overlap when R + W > N. For N = 3, R = 2 and W = 2 gives
overlap and survives one down replica. R = 1 and W = 1 is fast but can miss writes. Note that quorums reduce
staleness but do not by themselves make a system linearizable.
PACELC extends CAP: if there's a Partition, choose A or C; Else, choose between Latency and Consistency. Even with a healthy network, waiting for replicas to confirm costs time.