Consistency Patterns (System Design #4)
You post a photo, refresh, and it isn’t there. Refresh again — still gone. Ten seconds later it appears. Nothing is broken; the read replica you hit simply had not caught up with the write yet. That experience has a name: eventual consistency. It is not a bug, it is a choice, and choosing it deliberately is what this chapter is about.
Consistency is not a boolean. It is a spectrum from “every read sees the latest write, everywhere, instantly” to “reads may return anything, eventually converging”. Between those extremes live several precise, useful patterns. This chapter defines them, shows what each costs, and maps them to real products.
Strong consistency (linearizability)
The strongest guarantee: the system behaves as if there is a single copy of the data. Every read that starts after a write completes sees that write.
\[ \text{write}(x, v)\ \text{returns} \implies \text{any later read of } x \text{ returns } v \]
- Cost: coordination. Writes and reads must go through a leader or a quorum, and reads may block until a value is agreed. Cross-region strong consistency adds round trips.
- Use when: correctness is non-negotiable — balances, inventory counts, unique usernames, locks, configuration, job leases.
- Examples: single-node Postgres, ZooKeeper, etcd, Google Spanner.
Strong consistency is the safe default for small data where you can afford the latency. The mistake is needing it everywhere; the second mistake is needing it nowhere.
Weak consistency
No guarantee that a read returns the latest write. After a write, a read may or may not see it. This sounds useless but is fine when the data is transient and soon overwritten.
- Use when: the value is a live approximation — real-time multiplayer positions, live video/voice state, presence indicators, volatile UI state.
- Example: a live cursor position; if a frame renders 50 ms stale, no one notices or cares.
Weak consistency is not a synonym for “broken”. It is a deliberate choice to skip coordination for data where coordination has no value.
Eventual consistency
The data will converge to the same value everywhere — if writes stop. The guarantee is about the end state, not any individual read. Replicas exchange updates in the background.
\[ \lim_{t \to \infty} \text{replicas} \to \text{identical} \quad (\text{order not guaranteed by default}) \]
- Cost: read-your-writes is not guaranteed; a user may see stale data. Conflicts may need resolution (e.g. last-write-wins, CRDTs).
- Use when: social feeds, likes, view counts, recommendations, search indexes, analytics, shopping carts.
- Examples: Cassandra, DynamoDB (default), Riak, DNS.
Two refinements make eventual consistency usable in practice, because raw “anything, eventually” is too loose:
- Read-your-writes — your reads reflect your writes. Achieved by routing a user’s reads to the same replica they wrote to, or by tracking a version and waiting. Users forgive staleness; they do not forgive seeing their own change vanish.
- Monotonic reads — once you have seen a newer version, you never see an older one on a subsequent read. Achieved by pinning a user to a replica for a session. Without it, a value can flicker forward and backward, which looks broken.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=4pt, minimum width=2.2cm, minimum height=0.75cm, align=center, font=\rmfamily\small},
a/.style={->, thick}]
\node[b, fill=blue!10] (w) at (-3.4,0) {write};
\node[b, fill=orange!16] (l) at (-0.9,0) {leader};
\node[b, fill=green!12] (r1) at (1.8,1.0) {replica};
\node[b, fill=green!12] (r2) at (1.8,-1.0) {replica};
\draw[a] (w) -- (l);
\draw[a, dashed] (l) -- (r1) node[midway, above, font=\scriptsize] {later};
\draw[a, dashed] (l) -- (r2) node[midway, below, font=\scriptsize] {later};
\node[font=\rmfamily\small, text=black!60, anchor=north] at (0,1.5) {reads from a replica may lag};
\end{tikzpicture}
Causal consistency
The middle ground. Operations that are causally related are seen in order by everyone; concurrent operations may be seen in different orders on different replicas.
- Use when: collaboration tools where “cause before effect” matters — comments and the posts they reply to, messages and edits, event sourcing.
- Examples: systems with vector clocks or version vectors (some CRDTs, MongoDB causal sessions).
Causal consistency gives you the ordering users intuitively expect without paying for global linearizability.
Pick a pattern per data type
A single product uses several. The skill is not choosing one; it is mapping each data flowing through the system to the weakest pattern that satisfies its users.
| Data | Pattern | Why |
|---|---|---|
| Account balance | Strong | wrong balance = real money lost |
| Inventory count | Strong | oversell is unacceptable |
| Username uniqueness | Strong | duplicates are permanent damage |
| User’s own new post | Read-your-writes | “where is my post?” is a support ticket |
| Follower feed | Eventual | a few seconds stale is invisible |
| Likes / view count | Eventual | approximate is fine |
| Chat message order | Causal | replies must not precede the message |
| Live cursor / presence | Weak | transient and overwritten |
# decide per data type, not per company
CONSISTENCY = {
"balance": "strong",
"inventory": "strong",
"my_own_post": "read_your_writes",
"feed": "eventual",
"like_count": "eventual",
"chat_thread": "causal",
"presence": "weak",
}
def route(data_type):
mode = CONSISTENCY[data_type]
if mode in ("strong", "read_your_writes", "causal"):
return "read from leader / quorum" # slower, correct
return "read from nearest replica" # faster, may be stale
This table is a design artifact. In a real review, writing it down forces the question “do we actually need strong here?” — and often the answer is no, which buys latency and availability back.
Conflict resolution, briefly
Eventually consistent systems must handle conflicting concurrent writes. Three common strategies:
- Last-write-wins (LWW) — keep the write with the highest timestamp. Simple, can silently drop data. Fine for profile fields, bad for counters.
- Merge functions (CRDTs) — data types designed to merge deterministically (counters, sets, sequences). Correct and automatic; requires the right type.
- Application resolution — surface both versions and let logic (or the user) decide. Used by some shopping-cart and document systems.
The rule: if losing a concurrent write is unacceptable, either use strong consistency or use a CRDT. LWW is a convenience, not a correctness guarantee.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
a/.style={->, thick}]
\draw[a] (0,0) -- (7.2,0) node[below, font=\small] {stronger consistency $\to$};
\foreach \x/\lab in {0.6/weak, 2.4/causal, 4.2/eventual, 6.2/strong} {
\fill[orange!60] (\x,0.05) circle (3pt); \draw (\x,0.05) circle (3pt);
\node[font=\small, anchor=north] at (\x,-0.1) {\lab};
}
\node[font=\rmfamily\small, text=black!60, anchor=north] at (3.6,-0.8) {more consistency = more coordination = more latency};
\end{tikzpicture}
Pitfalls
- Eventual consistency without read-your-writes. Users seeing their own write disappear generate the loudest bug reports. Add the refinement.
- Using LWW for things that must not be lost. Concurrent edits silently vanish. Use CRDTs or strong consistency.
- Strong consistency everywhere. It is easy to reason about and expensive to scale. Reserve it for data that needs it.
- Monotonicity violations. Flipping between replicas can show an older value after a newer one; pin sessions to avoid the time-travel effect.
- Forgetting that consistency is per-read. The same system can offer strong reads for one query and eventual reads for another. Choose per operation.
What exactly does eventual consistency guarantee, and what two refinements make it usable for user-facing data?
ASolution
That all replicas converge to the same value once writes stop — it says nothing about any single read being current. Read-your-writes (your reads see your writes) and monotonic reads (you never see older data after newer) make it tolerable, usually by routing a user to the replica that has their update.
Next: Availability Patterns and the Math of Nines (System Design #5) — keeping the system up while all this propagation happens.