Notes
All notes

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 \]

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.

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}) \]

Two refinements make eventual consistency usable in practice, because raw “anything, eventually” is too loose:

\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.

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:

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

QQuestionSelf-check

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.