Lesson 03

Consistency Models That Come Up

Building block

A consistency model is a promise about what a read may return after a write. Interviews use four of them, and each is a product behaviour with a name: a gift code redeemed once, a reply never shown before its message, your own edit visible on your next page load, a like count that is right in the end.

You choose a model per operation, not per system. Every step up the list costs latency or availability, and you say which when you choose it.

A Model Is a Promise

The four, from strongest to weakest, are linearizable, causal, read-your-writes and eventual. A design holds several at once. Redeeming a gift code and counting how many codes were redeemed need different promises, and paying for the stronger one on the count buys nothing a user would notice.

Stronger models need replicas to coordinate before they answer, and what that coordination costs is the next lesson. Transaction isolation is a different axis: it is about several records in one transaction, not one record across its copies. PostgreSQL Deep Dive covers isolation levels inside one engine, and Chapter 5 covers transactions that span partitions.

The weakest model that prevents each behaviour
A gift code can be redeemed only once→Linearizable
A reply never shows before the message it answers→Causal
Your own edit shows on your next page load→Read-your-writes
A like count is right once the writes settle→Eventual

Linearizable

A linearizable system behaves as if there were one copy of the data. Each operation takes effect at a single instant between its request and its answer, and once a write has answered, every later read sees it.

The behaviours that need it are the ones where two people acting at once must get one answer. A single-use gift code is redeemed once. A feature switched off across the fleet is off for every request that starts after the switch has returned. A lock, which is Chapter 5's subject, has one holder.

A design gets it from one leader that serves those reads and writes, kept through a failover by a synchronous follower and fencing, or from consensus, also Chapter 5. And the operation is one conditional write: mark the code used only if it is still unused. A read followed by a write lets two people both see it unused. Quorums alone do not give it, for the reasons the last lesson listed. The cost is that each such operation goes to one place. For a user in western Europe with the leader in US east, that is about 85 milliseconds on every one of them.

Causal

Causal consistency promises that an effect is never seen before its cause; writes that do not depend on each other may appear in any order. It is the happens-before order that Computer Science Deep Dive teaches inside one machine, applied across replicas.

In a chat, a reply never appears before the message it answers, and the chat problem in Chapter 18 leans on that. In a shared folder, you remove a colleague's access and then upload the salary sheet. The upload depends on the removal, so the colleague must never see the sheet while still holding the old access.

Without causal order, the upload overtakes the removal
Access removedwrite 1
→
Sheet uploadedwrite 2, after write 1
→
Upload arrives firston the colleague's replica
→
Colleague sees the sheetwith the old access

The cost is bookkeeping. Each write carries what it depends on, and a replica holds a write back until its causes have arrived. What causal does not cost is availability: replicas keep serving through a partition, as long as each client keeps talking to the same replica.

Read-Your-Writes and Its Neighbours

Two promises cover one user's own view. Read-your-writes says you see what you wrote. Monotonic reads says you never see time go backwards: once you have seen a value, a later read does not show you an older one.

They answer the everyday complaint, "I saved it and it is gone", and they cost far less than linearizable, because only the writer needs the guarantee. Everyone else keeps reading from the nearest replica. How routing a user's reads provides them is Chapter 2's lesson on read replicas; this lesson only places them on the map.

Eventual

Eventual consistency promises that if writes stop, the replicas end up the same. It promises nothing about when, or about the order in which a reader sees the writes along the way.

It fits like counts and view counts, a follower count, a search index. Eventual alone is not a design, though. It needs a rule that makes the replicas converge, which is the last lesson of this chapter, repair, which was the quorum lesson, and a staleness the product accepts, stated as an assumption. You might say: "Assume a like count may be a few seconds behind, so the post page reads it from the nearest replica."

Linearizable vs Read-Your-Writes

Linearizable makes every reader see a write once it has answered. Use it when two people acting at once must get one answer, such as a code redeemed or a seat taken.

Read-your-writes makes only the writer see it at once, while others may lag. Use it when the complaint is a user missing their own change, which covers most profile, settings and post edits, and keep the cheaper reads for everyone else.

How Interviewers Probe This

Each answer here starts from one operation and the behaviour a user would notice if its promise were weaker. Then it names the weakest model that prevents that behaviour, and what the model costs.

The first probe is "What consistency does this design need?" Split the operations, name a model for each with the behaviour it protects, and refuse one label for the whole system.

The second is "Why not make everything strongly consistent?" Price it: the round trip to one leader on every read and write, about 85 milliseconds for a user across the Atlantic, and the requests refused whenever that leader is out of reach.

The third is "A user edits their profile and sees the old one. Is that a bug?" Name read-your-writes as the missing promise, say that only that user needs it, and point to routing their reads rather than to a stronger store. You might say: "That's read-your-writes missing. I'd send this user's reads to the leader for a short while after an edit and leave everyone else on the replicas."

Misconceptions
  • "Strong consistency is one thing." Products use the phrase for different promises. Say the model by name and the behaviour it guarantees.
  • "Eventual consistency means stale by a little." It promises convergence, not a bound. The bound is a separate number the design states and monitors.
  • "Serializable means linearizable." Serializable is about transactions over several records; linearizable is about one record in real time. A system can have either without the other.
  • "The whole system needs the strongest model." One or two operations usually do, and paying for it on every read buys latency for behaviours nobody would notice.
  • "Read-your-writes needs linearizable storage." Routing one user's reads after their write is enough, and it costs a few reads on the leader.
Why It Matters
  • Name the model per operation, with the user-visible behaviour that a weaker model would break.
  • Choose the weakest model that prevents that behaviour, and say what the stronger one would have cost.
  • Give every eventually consistent read a stated staleness, and say what the product shows meanwhile.
  • Send linearizable operations to one leader or a consensus group, and keep them few.
RelatedRead Replicas and Lag the routing that restores read-your-writesCAP and PACELC, Precisely what the stronger models costPostgreSQL Deep Dive isolation levels in one engineComputer Science Deep Dive happens-before in one machine

Knowledge Check

A concert seat must never go to two buyers who click at the same moment. Which is the weakest model that prevents a double sale?

  • Causal, for the operation that marks the seat as sold to a buyer
  • Linearizable, for the operation that marks the seat as sold
  • Read-your-writes, for the operation that marks the seat as sold
  • Eventual, with a later check that refunds the second buyer

A leaderless store uses R plus W greater than N. Why is that still not linearizable?

  • A read during a write can see the new value, and the next read the old one
  • The overlap holds only when every replica sits in the same zone as the rest
  • R plus W above N makes reads skip the replicas holding the latest write
  • Linearizable needs every read to ask all N replicas, not just R of them

In a group chat, replies sometimes appear before the messages they answer. Which promise is missing?

  • Eventual, so that every replica ends up holding the same messages
  • Causal, so that no write is shown before a write it depends on
  • Read-your-writes, so that each sender sees their own messages
  • Monotonic reads, so that a reader never sees an older state again

A user saves a new bio and the reload shows the old one. Other users may see the change a few seconds late. What is the cheapest fix?

  • Make the profile store linearizable, so every reader sees each edit
  • Route that user's reads to an up-to-date copy for a while after the edit
  • Cut replication lag until every replica applies writes within a second
  • Move every profile read to the leader, for every user, all the time

How do serializable and linearizable differ?

  • Serializable orders transactions over many records; linearizable, one record in real time
  • They are two names for one promise, one used by databases and the other by replicated stores
  • Linearizable is the stronger form of serializable, which adds a real-time order on top of it
  • Serializable applies across replicas; linearizable applies within one machine

You got correct