When data is copied to several machines, an uncomfortable question appears: if you read from a different copy than the one just written, do you see the new value? Consistency is the answer a system promises. Strong consistency says "always the latest". Eventual consistency says "the latest, eventually".
The CAP theorem explains why you cannot always have both strong consistency and availability when the network breaks, and quorums are the practical dial that lets you tune this per request. This lesson defines each term, does the quorum arithmetic (W + R > N), and shows how to talk about it in an interview.
Detailed Notes
What is it?
Consistency (in the distributed sense) describes what a reader may see after a write when several copies exist.
Strong consistency (also called linearizability). After a write completes, every later read, from any client and any replica, returns that write or something newer. The system behaves as if there were one copy.
Eventual consistency. If writes stop, all copies will eventually hold the same value. In the meantime, different readers may see different values (stale reads).
Between the two are weaker-than-strong guarantees you met in the replication lesson, such as read-your-writes and monotonic reads.
Do not confuse this with the "C" in ACID. ACID consistency means "rules are not broken". Distributed consistency means "all copies agree".
Why do we need it?
Copies are the reason systems survive failures and serve global users, and copies can disagree. Two forces pull against each other:
Making copies agree takes communication. A strongly consistent write must reach enough copies before it is acknowledged. That costs latency (a round trip across the ocean can take 80 ms or more) and fails when copies cannot be reached.
Users want fast and always-on. A "like" button that waits 200 ms or errors during a network problem feels broken.
So engineers choose, per operation, how much agreement to pay for. A bank balance needs strong consistency. A view counter does not.
How does it work?
The CAP theorem
In a distributed data store, three properties matter:
C: Consistency. Every read gets the latest write (strong consistency).
A: Availability. Every request to a working node gets a response (not an error), even if it may not be the latest data.
P: Partition tolerance. The system keeps operating when the network splits nodes into groups that cannot talk to each other (a network partition).
The theorem says: during a network partition, you must choose between consistency and availability. You cannot keep both.
Why? Suppose two data centres A and B hold copies, and the link between them is cut. A client writes to A.
If B keeps answering reads (available), it returns the old value, so the system is not consistent.
If B refuses to answer until it can sync with A (consistent), it returns errors, so it is not available.
Network partitions will happen in any multi-machine system, so P is not optional. The real choice is:
CP: on a partition, reject or delay some requests to stay correct. Examples: systems used for locks, leader election, bank ledgers.
AP: on a partition, keep answering, possibly with stale data, and reconcile later. Examples: shopping carts, feeds, view counters.
During a Network Partition: CP or AP?Comparison
CP: Consistent
refuse rather than be wrong
1
Write lands on side A
link to B is down
2
Side B cannot confirm the latest
no majority reachable
3
B returns an error or waits
correctness kept
Never serves stale or conflicting data
Simple for the application
Some requests fail during the partition
Higher latency to confirm
Payments, inventory, locks, leader election
AP: Available
answer now, reconcile later
1
Write lands on side A
link to B is down
2
B keeps serving its copy
may be stale
3
Merge when the link heals
read repair, version vectors
Always responds, low latency
Survives regional outages
Stale reads
Conflicts the application must resolve
Carts, feeds, counters, presence
Key point. Pick CP where wrong answers cost money. Pick AP where unavailable costs more than stale.
The link between two data centres is cut and a client writes to one side.
Two refinements interviewers like:
CAP only describes what happens during a partition. When the network is healthy you can have both, but you still trade latency against consistency: waiting for more replicas is slower. (This extension is called PACELC: if Partition choose A or C, Else choose Latency or Consistency.)
"Pick two of three" is a slogan, not a design. Real systems let you choose per operation.
Quorums: tuning agreement with numbers
A quorum system keeps N copies of each item and uses three numbers:
N: the number of replicas holding the item (say 3).
W: how many replicas must acknowledge a write before it counts as successful.
R: how many replicas must answer a read before the read returns. The reader takes the answer with the newest version.
The rule: if W + R > N, every read overlaps every write in at least one replica, so a read is guaranteed to see the latest acknowledged write.
Why: with N = 3 and W = 2, the write sits on 2 of the 3 replicas. A read with R = 2 asks 2 of 3. Any two groups of 2 drawn from 3 must share at least one member (pigeonhole), and that shared replica has the new value.
text
N = 3
W = 2, R = 2 : 2 + 2 = 4 > 3 -> reads see latest (strong-style)
W = 1, R = 1 : 1 + 1 = 2 <= 3 -> fast, but stale reads possible (eventual)
W = 3, R = 1 : 3 + 1 = 4 > 3 -> latest reads, but a write fails if any one replica is down
W = 1, R = 3 : 1 + 3 = 4 > 3 -> fast writes, slow reads, a read fails if any replica is down
N = 5, W = 3, R = 3 : 6 > 5 -> survives 2 failed replicas for both reads and writes
Fault tolerance: with W = 2 and R = 2 out of N = 3, you can lose one replica and keep both reads and writes working. With N = 5, W = 3, R = 3 you can lose two.
Quorum Write and Read with N = 3, W = 2, R = 2Request flow
Key point. Any 2 nodes written and any 2 nodes read must share a node, since 2 + 2 > 3.
Node 3 misses the write, yet the read still returns the newest value.
How stale replicas heal
Replicas that missed a write need to catch up. Two common mechanisms:
Read repair. When a read asks R replicas and notices one returned an old version, the coordinator writes the newest value back to that stale replica.
Anti-entropy. A background process compares replicas (often with hash trees, which summarise ranges of data so differences are found cheaply) and copies missing data.
Versions are needed to know which value is newer. Systems use timestamps (simple, but clocks drift) or version vectors (per-replica counters that can detect that two writes happened concurrently, so the application can merge them).
How Many Copies Must Agree?Architecture
N = 3Clientper-request W / RCoordinatorwaits for W or RReplica 1v2Replica 2v2Replica 3v1 (lagging)
Key point. Raise W or R for safety, lower them for speed. W + R > N is the line between latest and maybe stale.
A coordinator sends each request to all N replicas and waits for W or R answers.
Quorum caveats
W + R > N is a strong tool but not a magic guarantee. Concurrent writes can still conflict; "last write wins" by timestamp can drop an update; and a sloppy quorum (accepting writes on other nodes temporarily during failures) weakens the overlap guarantee. For true linearizability, systems use consensus protocols (introduced in the distributed-coordination lesson).
Interview tip. Do not say "we'll use CAP to choose two out of three". Say which operation needs which guarantee, and what happens during a partition: "the order total must be correct, so I'll fail the purchase rather than risk double-selling; the product view counter can be stale."
Real-world example
A shopping site stores a customer's cart in an AP store with N = 3, W = 2, R = 1. During a network partition a customer adds a book on one side and a pen on the other. Neither write is rejected. When the partition heals, the two versions are merged by taking the union of the items, so both the book and the pen remain. The same site processes payment against a CP relational ledger: during a partition, payments may fail with an error, but money is never double-charged.
System design example
Design a "likes" counter and an "inventory" count for an e-commerce page.
Likes: nobody cares whether it shows 10,203 or 10,207. Use eventual consistency, writes with W = 1, buffered and aggregated asynchronously. Availability and speed win. An error saying "can't like right now" is worse than a stale count.
Inventory at checkout: overselling is expensive. Use a strongly consistent single-leader store, or a quorum with W + R > N, and do the decrement with a conditional update. If the system cannot reach enough replicas, fail the order with a clear message.
Product page stock display ("only 3 left"): eventual is fine for display. Re-check strongly at checkout.
One system, three consistency levels, each chosen from the cost of being wrong.
Interview discussion. Say: "I'd decide consistency per operation. Checkout needs strong consistency, so it uses a leader or quorum reads and writes, and during a partition it fails rather than oversells. Browsing can be eventually consistent, so it reads from nearby replicas and is always available."
Trade-off thinking. Stronger consistency costs latency and availability. Weaker consistency is faster and more available but exposes stale data and conflicts to your application. The question is never "which is better" but "what is the cost of a stale or conflicting answer here".
Key interview takeaway
Explain CAP as a choice between consistency and availability during a partition, then show the dial: quorum math W + R > N with concrete values. Choose per operation by the cost of being wrong, and mention how stale copies heal (read repair, anti-entropy).
Real World Example
A shopping site keeps carts in an available (AP) store and merges conflicting carts by union after a partition, while payments run on a consistent (CP) ledger that fails rather than double-charges.