Skip to content

CAP in practice

Working knowledgeConcept

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.

CP: majority or nothing
Step 1 of 7
Three replicas of the cart
A
B
C
Cart on each replica
cart
A[pen]
B[pen]
C[pen]
Replies to clients
Empty
  • 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.

2 of 3 majority

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.

AP: answer now, reconcile later
Step 1 of 8
Three replicas of the cart
A
B
C
Cart on each replica
cart
A[pen]
B[pen]
C[pen]
Replies to clients
Empty
  • 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

CP during a splitErrors on the small sideNever wrong, sometimes unavailable. Good for balances, locks, unique usernames.
AP during a splitAnswers everywhereAlways answers, sometimes stale or conflicting. Good for carts, likes, timelines.
What some well-known systems do when a partition cuts off a minority
SystemOn the minority side
etcd, ZooKeeperWrites fail; they need a majority (Raft, ZAB). CP.
MongoDB, majority write concernA primary that loses the majority steps down, so writes fail there. CP for those writes.
Cassandra, consistency ONEAny reachable replica answers. AP.
Cassandra, consistency QUORUMRequests 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

A slow link looks like a broken oneA replica cannot tell a partition from a peer that is slow or paused. It only sees a timeout, so a long GC pause can trigger the same behaviour as a cut cable.
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.
Calling a single-leader setup "CA"If followers keep serving reads after losing the leader, you have chosen A. If they stop, you have chosen C. There is always a choice; it is just made by defaults.
One answer for the whole appA cart and an account balance need different answers. Pick per feature, or per operation where the store lets you.

08 Check yourself

Question

Five replicas split into a group of three and a group of two. In a CP system using majorities, which side can still accept writes?

Question

A vendor says their database is "CA: consistent and available". What does that tell you about a partition?

Question

Two sides of a split both set seat 7A = owner to different people. Can the replicas merge this like the cart?

09 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?