CAP THEOREM
Pick two (but you must pick P)
A network partition cuts one replica off from the other two. A client on the cut-off (minority) side sends a write to that replica. Which of these can the system offer?
Three replicas hold one value. The network splits them into a majority side and a minority side, and a client on each side sends a write. The minority replica has two options. It can accept and acknowledge the write it cannot replicate: the client is happy, and the two sides now hold different acknowledged values. Or it can refuse because it cannot reach a quorum: the data stays consistent, and the client gets an error. There is no third option while the partition lasts, and that is the whole theorem.
During a partition, a replica that cannot reach a majority must pick:
AP answer anyway
write: accept locally, replicate later -> two acknowledged values
read: return the local value -> may be stale
later: reconcile. Last-writer-wins drops one; version vectors keep both
and hand the conflict to the application.
CP answer only with a quorum
write: refuse unless a majority acknowledges -> errors on the minority side
read: refuse unless a majority confirms -> errors on the minority side
a majority exists on at most one side, so two sides can never both accept.
No partition: both modes accept every request and agree. The trade-off is
per request, for the duration of the partition. See PACELC for the cost you
pay when the network is fine. Run AP and CP back to back; the schedule is identical. One invariant turns red in each, never both in the same run, and NO PARTITION keeps both green. The number to read in AP is the write that last-writer-wins throws away; in CP it is the error count.
Same clients, same writes, AP mode. Insert the partition where it makes two replicas acknowledge values that conflict. The slider sets how long it lasts, and where it can go depends on that length.
GOAL: two replicas acknowledge conflicting values (diverged)
Implement the write path for one replica. In CP mode refuse when no quorum is reachable. In AP mode always accept, and return a version vector that makes two concurrent writes detectable instead of silently overwriting one. The grader runs both modes with and without a partition.
Multi-region databases with a writable replica in each region, caches that keep serving after they lose their primary, etcd or ZooKeeper members on the minority side of a cut, which return errors or time out until they rejoin, Cassandra and DynamoDB global tables that need a conflict policy. The AP failure is quiet data loss discovered weeks later; the CP failure is a page at 3am because one region is returning 503.
“Is your system CP or AP?” is a trap: the honest answer is per operation and per partition. Say which side of a partition can hold a quorum and what each side does without one. Then name the reconciliation policy for anything AP (last-writer-wins drops data; version vectors keep conflicts for the application) and the user-visible symptom for anything CP (errors, not stale data).
USED IN
Chapters that build on this lesson.
- Consistency models in practiceDatabases