Sub topic 16
Choosing a consistency model for your workload
This chapter assumes you know what replication is, how linearizability, causal and eventual consistency differ, how quorums work (R + W > N), and the basic CAP and PACELC trade-offs. It gives a way to pick a model for a real workload.
Start from the anomaly you cannot tolerate
Do not start from "which model is best". Start from the question: what wrong answer would hurt a user or the business? Strong models cost latency and availability, so buy only what you need.
Typical anomalies and the cheapest model that prevents them:
| Anomaly you cannot accept | Cheapest sufficient model |
|---|---|
| Two clients both claim a unique username or the last seat | Linearizability (a single agreed order) |
| A user saves a profile and then sees the old one | Read-your-writes (session guarantee) |
| A reply appears before the post it answers | Causal consistency |
| A feed goes backwards in time on refresh | Monotonic reads |
| A view counter is slightly stale | Eventual consistency |
| A transfer debits one account but not the other | Serializable transactions |
A decision procedure
- List the operations and mark each as a read, a blind write, or a read-modify-write.
- For each, ask whether a stale or reordered result causes harm that cannot be fixed later.
- If an operation needs a global invariant (uniqueness, balance never below zero), it needs coordination. Use consensus-backed linearizable writes or serializable transactions.
- If the harm is only confusing ordering, try causal consistency or session guarantees.
- If concurrent writes can be merged, use eventual consistency with a CRDT or a clear conflict rule.
- Check the latency budget. A write that must reach a majority across regions pays at least one cross-region round trip.
Mix models in one system
Most systems use different models for different data. A shop might use:
- Inventory reservation and payment: linearizable or serializable, through a consensus-backed store.
- Shopping cart: eventual consistency with a mergeable structure.
- Product reviews and page views: eventual consistency.
- User settings: read-your-writes, often by routing a user's reads to the replica that took their write.
Common mistakes
- Treating "R + W > N" as linearizability. Quorum overlap alone does not give it, as an earlier chapter showed.
- Choosing last-write-wins for data where losing an update matters. Clock skew can make an older write win.
- Reading "CP" or "AP" on a product page as a full description. Check the actual default and the per-request options.
- Requiring strong consistency everywhere "to be safe". This raises tail latency and reduces availability during partitions.
- Ignoring failure behavior. Decide what the system returns when a majority is unreachable: an error or possibly stale data.
Checking a choice in code
A small helper can make the quorum part of your reasoning explicit. This checks only overlap, not full linearizability.
def describe(n, r, w):
overlap = r + w > n
tolerates_write_failures = n - w
tolerates_read_failures = n - r
return {
"overlap": overlap,
"write_failures_tolerated": tolerates_write_failures,
"read_failures_tolerated": tolerates_read_failures,
}
print(describe(3, 2, 2)) # overlap True, tolerates 1 failure each
print(describe(5, 1, 5)) # overlap True, but one down node blocks writes
print(describe(3, 1, 1)) # overlap False, fast but can read stale dataThe second line shows the trade-off: reads are cheap, but writes become unavailable when any one replica is down.
Exercise
A ticketing service sells a fixed number of seats. It also shows a "people viewing this event" counter. Which consistency model would you choose for each, and why?
Show answer
Seat reservation has a global invariant (never sell more seats than exist, never sell one seat twice), so use linearizable or serializable writes through a consensus-backed store. The viewer counter tolerates stale or approximate values, so use eventual consistency, ideally with a CRDT counter, which keeps it fast and available during partitions.
Exercise
A team sets N=3, R=1, W=3 and claims reads are always fresh and the system is highly available. What is wrong with the claim?
Show answer
R + W = 4 > 3, so reads overlap with the last successful write. But W=3 needs every replica, so one failed replica blocks all writes. Availability for writes is low. Overlap also does not by itself guarantee linearizability.