Skip to content

Consistency models: what a read may return

Once data lives on more than one node, a read can be answered by a copy that is behind. The earlier lesson on stale reads showed the symptoms: your own write missing, a value going back in time. A consistency model is the name for what a system promises to prevent. Jepsen, which tests databases for exactly these promises, describes a model as a safety property: it defines the set of histories that a system may legally produce. A history is the list of reads and writes with the values they returned. If a history is in the set, the system behaved as promised, whatever the implementation did to get there.

That makes models comparable. A stronger model allows fewer histories, so it is easier to program against, and it is harder to provide. The five below are the ones the rest of the course uses.

Linearizability. Jepsen’s definition: every operation appears to take place atomically, in some order, consistent with the real-time ordering of those operations. If one operation completes before another begins, the second takes effect after the first. A read that starts after a write has been acknowledged must return that write or a later one. It is the model of a single copy that answers one request at a time, and it is the “C” that the next lesson uses in the CAP theorem. Jepsen notes that a linearizable system cannot be totally or sticky available: when the network is cut, some or all nodes cannot make progress.

Sequential consistency. The operations appear to take place in one total order, and that order respects the order of the operations of each process. There is no real-time constraint. Jepsen points out that a process may be far ahead of or behind the others, and may read arbitrarily stale state, but once it has seen an operation by another process it can never see a state from before it. Like linearizability, it cannot be totally or sticky available.

Causal consistency. Causally related operations appear in the same order to everyone, but processes may disagree about the order of operations that are causally independent. Jepsen’s example is a chat: Attiya asks “shall we have lunch?”, and Barbarella and Cyrus answer “yes” and “no”. Attiya may see “yes” before “no” while Barbarella sees “no” before “yes”, but nobody sees an answer before the question. Causal consistency is sticky available: a client that keeps talking to the same non-faulty server can make progress during a partition.

Read-your-writes. If a process performs a write and then a read, the read observes the write. It says nothing about other processes. Jepsen lists it as sticky available.

Eventual consistency. Werner Vogels’ definition: if no new updates are made to the object, eventually all accesses return the last updated value. The definition puts no bound on how long that takes, and says nothing about what is returned in the meantime.

Two things to notice. First, the models answer different questions: linearizability and sequential consistency are about one global order, causal consistency is about order only where there is a cause, read-your-writes is about one client’s own history. Second, Jepsen states a cost that all of this adds up to: if you need total availability, you give up causal consistency and read-your-writes, and keep weaker guarantees such as monotonic reads.

Mental model. A group chat. Eventual consistency says everyone will end up with the same messages. Read-your-writes says you always see what you just sent. Causal consistency says nobody sees a reply before the question. Sequential consistency says everyone sees the same single order of messages. Linearizability adds that if you phone a friend after sending, their screen already shows it.

The right question is not “which is best” but “which is the weakest model that keeps this feature correct”, because every step up costs waiting and availability. Sort each symptom under the weakest model that rules it out.

Challenge

Which consistency model does it need?

A feature is correct only if the data store rules out one particular symptom. Put each symptom under the weakest of these four models that rules it out. Sequential consistency is left out of this exercise.

Sorting needs JavaScript.

Categories

Linearizability
A read that starts after a write completes returns that write or a later one, for every client.
Causal consistency
An effect is never seen before its cause.
Read-your-writes
A client always sees its own earlier writes.
Eventual consistency
The copies agree once writes stop; a short disagreement is acceptable.
  1. Line 1 of 8A cashier confirms a payment on screen. The customer's bank, called on the phone a moment later, must already show the payment.
  2. Line 2 of 8Two people try to register the same username at almost the same time. Exactly one must succeed, and the second must be told it is taken.
  3. Line 3 of 8In a chat, a reader sees the answer "yes" on screen before the question it answers.
  4. Line 4 of 8A user deletes an account, and then a comment on it from the same thread shows up as if the account still existed.
  5. Line 5 of 8A user saves a new display name and reloads the page, and sees the old name.
  6. Line 6 of 8A shopper adds an item to the cart, and the cart page does not list it.
  7. Line 7 of 8The view counter of a video shows slightly different totals on two replicas for a few seconds, then they agree.
  8. Line 8 of 8Two visitors see a different number of likes on the same post for a moment.

Check your answers at least once before looking at the answers.

What happens if a read is allowed to be old?

Section titled “What happens if a read is allowed to be old?”

Take two clients and one value. Alice writes, and the system acknowledges. Then, after the acknowledgement, Bob starts a read, and a lagging copy answers with the old value. Under eventual consistency, that is allowed: the copies will converge, and the model says nothing about when. Under sequential consistency it is also allowed, because the order of operations can put Bob’s read before Alice’s write, as long as it respects what each of them did in turn. Under linearizability it is forbidden, because Bob’s read began after Alice’s write completed, and so it has to return that write or a later one.

That difference is invisible to a person looking at one screen, and it matters the moment two screens are connected by something outside the system, such as a phone call or a message. Whether this applies to your feature is a requirement to ask for, not a property you get for free.

  • A consistency model is a promise about which histories of reads and writes a system may produce. A stronger model allows fewer.
  • Linearizability keeps the real-time order of operations. Sequential consistency keeps one total order and each process’s own order. Causal consistency keeps only the order of cause and effect.
  • Read-your-writes protects one client’s own writes. Eventual consistency promises only that the copies agree if writes stop.
  • According to Jepsen, linearizable and sequential systems cannot be totally or sticky available, while causal consistency and read-your-writes are sticky available.
  • Choose the weakest model that keeps the feature correct.

Next: CAP stated precisely, and PACELC.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.