Systemsfor Aravinda
0 / 78 core
Week 4 · Distributed systems

Consistency models

3 hours · core path

Read first: CAP, PACELC, and what they actually say

In one sitting

Linearizability makes one register look like one copy in real time, and it costs a round trip across whatever distance separates the replicas. Weaker models drop that real-time point, then drop the total order, then only promise that quiet replicas will converge. Name the model per operation.

A consistency model is the set of histories you are willing to show a client. The same product uses more than one. A payment and a profile photo do not survive the same anomaly, and they should not pay the same round trip.

Core concepts

CAP told you that a partition forces a choice about linearizability. This page is the menu you are choosing from, including the models you use on a healthy day when you simply do not want to wait.

Draw the menu as a ladder. The top rung speaks about real time. Each step down drops a promise. Session guarantees sit near the bottom as promises to one client, not as a single mathematical rung, and eventual consistency is the weakest sentence in the set.

flowchart TB
  lin[Linearizability]
  seq[Sequential consistency]
  cau[Causal consistency]
  ses[Session guarantees]
  eve[Eventual consistency]
  lin --> seq
  seq --> cau
  cau --> ses
  ses --> eve

The arrows are a teaching order, so you can redraw it under pressure. They are not a claim that every paper’s definition implies the rung below in every edge case. When you are unsure which rung you implemented, describe the histories you allow.

Linearizability

A linearizable register looks like one copy. Each operation takes effect at a single point, and that point sits between the operation’s start and its finish. Real time is in the spec. If a write finishes, and only then a read starts, the read sees that write. There is no legal history in which the read still returns the old value.

The property is local and composable. Local means you can state it for one object. Composable means that if every object you touch is linearizable, a client that uses them together still sees a history that matches one real-time order. That is why a lock wants this rung. The lock and the record it protects have to make sense side by side, including to clients who never talked to each other.

It is expensive across distance. The implementation has to defend that single point before it returns. Defending it means a quorum or a leader has the value, which means a network round trip to whoever else is voting. The trip is cheap inside one building and costly across a continent. Nothing in the definition cares about your deadline. The definition only says the history is illegal if you return early and guess.

Sequential consistency

Sequential consistency gives everyone one total order, and that order respects each client’s program order. Client A writes x and then writes y, so in the total order x’s write is before y’s write. A read returns the value of the latest write in that order.

The order does not have to match the wall clock. A write that has already returned to one client can still be placed after a read, by another client, that has already returned. As long as no single client sees a contradiction with its own sequence, the history is legal. This is easier to implement when replicas can lag and you only have to avoid tearing one client’s own operations. It is the wrong promise for a lock held by two strangers, because those strangers have no program-order edge between them, and the wall clock is the only order that stops both of them from entering.

Causal consistency

Causal consistency tracks happened-before. A happened before B if the same client did A and then B, or if someone read A’s result and then did B, or if a chain of those links connects them. Nobody is allowed to observe B unless they also observe A.

A reply cannot appear without the comment it answers, if creating the reply read the comment. Two people who edit different fields at the same moment have no happened-before edge. Either order is legal. Causality is the strongest promise that still lets those concurrent writes proceed without coordinating.

The implementation tracks the dependencies a write must carry. A replica applies a write after the writes it depends on. Concurrent writes need a merge, or a leader to order them later. The cost shows up as metadata on the write, and as the client or the server having to notice concurrency. That machinery is the subject of clocks.

Session guarantees

Session guarantees are smaller promises about one client’s session. They are how you make a lagging replica less hostile without promising every client one real-time order. Three are worth naming.

Read-your-writes: after your write returns, your later reads in that session see it. Replication lag is why this needs the primary, a session token, or a version check. A read of a random replica does not provide it.

Monotonic reads: once you have seen a version, a later read in the session does not go backwards. The symptom when you lack it is a refresh that resurrects a value the user already watched disappear.

Consistent prefix: the session sees a prefix of the update stream, not a hole. You can be behind. You cannot see a later fact while missing an earlier one it depends on. A notification that points at a comment the timeline does not contain yet is the hole.

These three do not add up to linearizability. Other clients can still see your write late. Your own session has stopped gaslighting you.

Eventual consistency

Eventual consistency says that if updates stop, replicas converge. Werner Vogels’ point, and the useful one, is that this is a choice you make on purpose. The sentence says nothing about when convergence happens. It says nothing about what a read returns while updates are still flowing, which is the normal case. “Eventual” is not a duration. If you need a bound, you have a different model, or you have an operational lag target, and you should say which.

A history is the right way to test any of these sentences. Kyle Kingsbury’s Jepsen work is the reminder: consistency is whatever remains true when the network fails, and you check it by writing down operations and their return values. Do not cite a specific database failure from memory. Build the history yourself.

A worked example

A history that is eventual and not linearizable

One register, x, two replicas, A and B. Asynchronous replication from A to B. x starts at 0. Two clients, a writer and a reader, talk to different replicas. The times below are assumptions so the order is easy to see. The argument only needs the order.

x = 0 on A and on B

t = 0 ms    writer starts write x = 1 on A
t = 5 ms    A acks. The write has finished. B still holds 0.
t = 10 ms   reader starts a read of x on B
t = 15 ms   B returns 0. The read has finished.
t = 80 ms   replication delivers 1 to B. Assumed lag, not a measured SLA.
later       no more writes. A and B both hold 1.

Run the definitions against that timeline.

Eventual consistency allows it. Updates stopped. Both replicas hold 1. The model is satisfied and the reader still observed 0.

Linearizability forbids it. The write finished at 5 ms. The read started at 10 ms, after the write’s finish. A single copy would already hold 1 at every moment of that read, so the read would return 1. Returning 0 means there is no legal point to place the write between its start and its finish. The history is not linearizable.

Sequential consistency can still accept it. The two clients each performed one operation, so program order is trivial. A total order that places the read before the write returns 0 and then sets 1. Nothing inside one client contradicts that order. The wall clock does, and sequential consistency does not consult the wall clock. Same history, different rung.

Causal consistency also allows it. The reader did not read the writer’s value, and they are not the same client, so there is no happened-before edge from the write to the read. Causality does not require a stranger’s later read to see your write.

Read-your-writes says nothing here either, because the reader is not the writer. Change the cast and the history breaks a session guarantee: if the writer reads x from B at 10 ms and gets 0, their own write has vanished. That is a different bug, and it is the common one.

What the top rung costs across distance

Place A and B 4,000 km apart and insist on the linearizable history. The write cannot return until a majority, or the leader, has the value. The fiber math is the bill.

fiber speed = 200 km per ms
4,000 / 200 = 20 ms one way
round-trip floor = 40 ms before routers
assumed planning RTT = 60 ms

Dean's same-datacenter RTT = 0.5 ms
cross-zone inside one region, planning band, often about 1 ms

60 ms / 0.5 ms = 120
one linearizable cross-country hop is about 120 in-building round trips

A read of the local replica that is allowed to return 0, the history above, costs about 0.5 ms. A read that is required to return 1 once the remote write has finished costs the cross-country trip, about 60 ms under the assumption, with a floor near 40 ms. The gap is the product decision. Latency is the whole argument: you spend the 120-fold jump only on the operations whose illegal history you refuse to show.

Spanner’s external consistency is the transactional version of the top rung across regions. Commit order agrees with real time. The mechanism is an interval clock and a commit wait, and the paper treats the uncertainty as milliseconds. You are paying those milliseconds on top of the distance. The Spanner page is where that design belongs. A new-grad sketch that says “we will use Spanner” has named a bill, not a model.

The same user, a weaker promise

The history that actually pages an on-call is usually a session bug, not a linearizability proof.

A user changes their display name and gets a 200. The next request reads a replica that has not applied the write. The page shows the old name. They submit again, or they file a ticket that says the save failed. You did not need a global total order to prevent this. You needed read-your-writes: pin the session to the primary, or send a token that means “I have seen version 7,” and reject replicas that are behind it.

A second refresh that hits an even older replica and brings the old name back breaks monotonic reads. The page flaps. Sticky routing or a minimum version on the session stops the flap. A notification for a comment the timeline cannot show yet breaks consistent prefix. You are behind, and you also have a hole.

None of those fixes makes the register linearizable for a stranger. They make one session monotonic. Say that out loud when you choose them, or you will “upgrade” a feed to a cross-country quorum and spend 60 ms on a photo.

Tradeoffs

Promise History you forbid What you spend
Linearizability A read that starts after a write finishes and returns the old value A round trip to a quorum or a leader, tens of milliseconds across a continent
Sequential consistency A client seeing its own operations out of order Coordination that can ignore the wall clock, and surprises between clients
Causal consistency Seeing an effect without its cause Dependency metadata, and a merge for concurrent writes
Read-your-writes Your own write missing from your next read Primary reads, or a session token checked against replica version
Monotonic reads A later read in the session going backwards Stickiness or a minimum version
Eventual consistency Nothing, until updates stop and replicas still disagree Lag, conflict, and a convergence mechanism

A cache in front of a linearizable database can drop you down the ladder. If the write has finished and the cache still serves the old bytes, the history the user sees is the illegal one above. Invalidation is what keeps the rung. A TTL only bounds how long you sit on the wrong rung.

Pitfalls

  • Saying “eventual” when you mean “stale for a moment, and the writer can see their own write.” Those are different promises. Only one of them has a bound, and only if you measured the lag.
  • Hearing “sequential” and promising that the order matches the clock. The missing real-time constraint is the entire gap between sequential consistency and linearizability.
  • Mixing this ladder with isolation levels. Read uncommitted, read committed, repeatable read, snapshot isolation, and serializable describe anomalies inside a database: dirty reads, non-repeatable reads, phantoms, write skew. Snapshot isolation stops the first three and still allows write skew. That menu does not say what a lagging replica may return. Keep transactions and this page in separate sentences.
  • Claiming the product is linearizable because one table is. A stale cache, an asynchronous replica read, or a search index that lags writes is a second object in the history. Composability does not survive a path that skipped the spec.
  • Treating a quorum overlap as a proof of linearizability. Overlap helps in the happy case. Lost replies and sloppy quorums take it away. That argument is the quorums page.
  • Offering causal consistency and then breaking it with a notification path that reads a different, faster replica than the timeline.

In the interview

In the first five minutes, draw the ladder and point at two operations. “Stock reservation is linearizable, so a write waits for a quorum. I am assuming a 60 ms cross-region round trip, against a 40 ms fiber floor. The feed is eventual, with read-your-writes for the person who just posted, using a session token.” That is a path, a number, a bottleneck, and a tradeoff.

Skip a formal proof. Skip Jepsen issue numbers. Early-career interviewers are not grading a composability proof. They are grading whether your history matches the product.

The question that usually follows is the history. Be ready to say: the write finished, the other replica returned the old value, the replicas later matched, and that is eventual and not linearizable. Then say what the user of that read was promised.

Check yourself

A write finishes, a later read on another replica returns the old value, and then the replicas match. Which models allow this?

Eventual consistency allows it, because updates stopped and the replicas converged. Linearizability forbids it, because the read started after the write finished and a single copy would have returned the new value. Sequential consistency can still allow it, since the total order does not have to follow the wall clock when the two operations belong to different clients. Name the rung you meant.

What does sequential consistency refuse to promise that linearizability requires?

A match with real time. Sequential consistency gives one total order that respects each client’s own program order. It does not require that a write which has already finished be visible to a read which starts later on another client. That real-time point is the extra demand, and it is why the stronger model needs coordination before it returns.

A user sees their new display name, refreshes, and sees the old name. Which session guarantee failed?

Monotonic reads. The session went backwards. Read-your-writes would cover the case where they never saw the new name at all after saving it. The fix is stickiness or a version token that later reads are not allowed to fall behind. Neither fix makes the name linearizable for every other user.

What is the round-trip floor for a linearizable write across a 4,000 km fiber path?

Fiber is about 200 km per millisecond, so 4,000 km is 20 ms one way and about 40 ms round trip before routers. A planning number in the tens of milliseconds, 60 ms if you assume ordinary routing, is what the write waits if the other voter is on that path. Dean’s same-datacenter round trip is 0.5 ms, so the cross-country hop is about 120 of those. Pay it only for the operation that needs the top rung.

Why can a correct linearizable database still present an illegal history to the user?

Linearizability is per object, and the user sees every object on the path. A cache that keeps serving the old value after the write has finished, or a replica read that ignores the version, is a second history in front of the database. The database object met the spec. The product did not. Invalidation, or a version check, is what keeps the composed history legal.

Based on: Gilbert and Lynch; Werner Vogels on eventual consistency; Kyle Kingsbury, Jepsen; Spanner, OSDI 2012, Corbett and others; Designing Data-Intensive Applications, Kleppmann and Riccomini.