CAP in practice
Your data lives on three machines, and the network between them breaks. A request arrives at the machine that is cut off. It can answer with what it has, or refuse to risk being wrong. That is the whole of CAP, and here is what each choice looks like.
01 A cable breaks between your replicas
You keep a shopping cart on three replicas, A, B and C, so that losing one machine loses nothing. Most days they talk to each other freely and stay in step.
Then a switch fails. A and B can still reach each other, but neither can reach C. C is not down: it is running fine and clients near it can still connect. This is a network partition. Nobody chose it, and the next request will not wait for it to heal.
A client now asks C for the cart. C has a copy, but it cannot check whether A or B changed it a second ago. It has two honest options: answer with what it has, or refuse.
02 What CAP actually says
The CAP theorem names three properties and proves you cannot have all of them while a partition lasts.
- Consistency (C): every read sees the latest acknowledged write, as if there were one copy. (Formally, linearizability.)
- Availability (A): every request to a replica that is running gets a real answer, not an error.
- Partition tolerance (P): the system keeps working when messages between replicas are lost.
On a real network, partitions happen whether you plan for them or not, so P is not optional. CAP is really one question: when a partition happens, does a cut-off replica answer, or does it stay correct?
"Pick two of three" is the common misreading. You cannot drop P. A system that claims to be "CA" simply has not said what it does when the network splits.
03 Choose consistency: the small side refuses
A CP system only accepts a write once a majority of replicas have it, and only serves a read it can confirm with a majority. With three replicas, a majority is two. Watch what happens to client Y, who is stuck on C’s side.
- replica acting now
- just written
- broken link
- message sent
- all agree
Three replicas hold the same cart, [pen], and can all talk to each other. A write counts once two of the three have it.
Nothing wrong was ever returned. The price was paid by Y, who got errors until the network healed. Clients on the majority side carried on normally.
04 Choose availability: both sides answer
An AP system lets any running replica accept reads and writes, and sorts out the differences later. Same cluster, same split.
- replica acting now
- just written
- broken link
- compared
- all agree
The same three replicas, all holding [pen]. This time every replica answers on its own and copies writes to the others later.
Every client got an answer. The price was a stale read and a conflict. A cart merges cleanly because adding items is easy to combine. Many values do not merge: if two sides set a seat’s owner to different people, one of them has to lose.
05 The two choices side by side
| System | On the minority side |
|---|---|
| etcd, ZooKeeper | Writes fail; they need a majority (Raft, ZAB). CP. |
| MongoDB, majority write concern | A primary that loses the majority steps down, so writes fail there. CP for those writes. |
| Cassandra, consistency ONE | Any reachable replica answers. AP. |
| Cassandra, consistency QUORUM | Requests that cannot reach a quorum fail. CP-like for that request. |
Notice the last two rows: the choice is often per request, not per product. The same database can be CP for a payment and AP for a "last seen" timestamp.
06 When nothing is broken: latency
Partitions are rare. The trade you make every second is a softer version of the same one. To give a consistent answer, a replica must check with others, and that takes a network round trip. To answer fast, it uses what it has.
Daniel Abadi’s PACELC puts both halves together: if there is a Partition, choose A or C; Else, choose Latency or Consistency. A store that reads from the nearest replica is fast and may be stale even on a calm day.
07 Where CAP thinking goes wrong
last-write-winsAP with no merge planKeeping the write with the latest timestamp silently throws the other one away, and clocks on different machines disagree. Decide how each kind of value merges before you choose AP.