16. Consistency and CAP
1
The picture to keep in your head2
16.1 What does "consistent" actually mean?3
16.2 What is strong consistency?4
16.3 What is eventual consistency?5
16.4 What are read-your-writes and monotonic reads?6
16.5 What is a network partition?7
16.6 What does the CAP theorem actually say?8
16.7 What do CP and AP systems look like in practice?9
16.8 What is PACELC?10
16.9 How do quorums give tunable consistency?11
16.10 How do you choose consistency for each feature?12
Memory card and where the analogy breaks13
Practice: five levels14
Interview questions15
Solutions16
Sources17
Code lab16. Consistency and CAP
16.7 What do CP and AP systems look like in practice?
3 min read+10 XP
At the bank: One bank's policy is "no withdrawals until the line is back" (safe). Another's is "pay out up to the last known balance, sort it out later" (customer-friendly, risky). Both are reasonable for different services.
In the system:
CP-style behaviour (consistency first):
- Coordination services like etcd and ZooKeeper: the minority side of a partition can't elect a leader or accept writes, so it returns errors. That's exactly what you want for leader election and configuration (Chapter 14, section 14.8).
- A single-leader database whose leader is cut off from the majority: well-designed failover stops the old leader from accepting writes (fencing), so that side becomes unavailable for writes.
- In the chapter's code, CP mode refuses both the write and the read on a replica during a partition.
AP-style behaviour (availability first):
- DNS: always answers from cache, even if the answer is old.
- Dynamo-style stores such as Cassandra and Riak at low consistency levels: any replica accepts writes and reconciles later. Amazon's Dynamo paper describes the shopping cart as the motivating example: never refuse an "add to cart", even if it occasionally means merging two versions of a cart.
- In the chapter's code, AP mode lets both sides accept profile edits; they diverge and are reconciled when the partition heals.
Many systems are tunable. Cassandra lets each query choose its consistency level; databases with replicas let you choose whether reads may go to followers. So "is X a CP or AP database?" often has the honest answer "depends how you configure and use it".
Memory code: "CP says sorry; AP says maybe."