Sub topic 2
A consistency model is a contract on histories
Prerequisites and scope
This chapter assumes you know what a replicated key-value store is and can read short Python. The previous chapter explained why replication creates consistency questions. Here we define what a consistency model is. Later chapters cover each model in detail, so this one only sets up the vocabulary and the way of checking a claim.
What a model promises
A consistency model is a contract between a data store and the programs that use it. The contract says which results reads and writes are allowed to return when data is copied across machines. It does not describe how the store is built. Two stores with different internals can offer the same model, and one store can offer several models through different settings.
The contract is stated in terms of histories. A history is a record of what clients did and what they saw: which operations were invoked, when each began, when each finished, and what each returned. A store satisfies a model if every history it can produce is allowed by that model.
This framing has a useful consequence. You can reason about a store without opening its source code. You collect a history, then ask whether the model permits it.
Anatomy of a history
An operation has an invocation (the client sends the request) and a response (the client receives the answer). The span between them is the operation's interval. Two operations are concurrent if their intervals overlap. They are ordered in real time if one finishes before the other begins.
Here is a small history. The initial value of x is 0.
Client A: write(x, 1) starts t=0, completes t=2
Client B: read(x) starts t=3, returns 0B's read began after A's write completed, yet it returned the old value. Whether that is acceptable depends on the model:
- Under linearizability it is not allowed. Once a write completes, every later read must see it or a newer value.
- Under sequential consistency it is allowed. The store may order B's read before A's write, as long as every client sees one agreed order that respects each client's own program order. Real time is not part of that contract.
- Under eventual consistency it is allowed. The replica B read from may not have received the write yet, and the contract only says replicas converge if writes stop.
The same history is legal under one contract and illegal under another. That is the point of the framing: a model is a set of permitted histories, and stronger models permit fewer of them.
Models form a hierarchy of sets
Because each model is a set of allowed histories, you can compare models by comparing sets. If every history allowed by model S is also allowed by model W, then S is stronger than W. Any program that is correct under W is also correct under S, since S can only produce histories W already permits.
The diagram below shows the relationship for the models this tutorial covers.
Read each arrow as "every history allowed here is also allowed below". Moving down the chain gives the store more freedom, which usually buys lower latency and better availability. Moving up gives programmers more guarantees, which usually costs coordination.
Not every pair of models is comparable. Serializability, covered later, constrains multi-operation transactions rather than single-object reads and writes, so it does not sit on this chain.
Three questions that pin down a model
When you read a vendor's claim such as "strongly consistent" or "tunable consistency", the word alone tells you little. Ask three questions instead.
- What is the unit of operation? A single read or write on one key, or a transaction over many keys? Linearizability is about single objects. Serializability is about transactions.
- What ordering is promised? Real-time order, a single global order with no real-time link, only causally related order, or none until writes stop?
- Which clients must agree? All clients, only one client's own session, or only replicas that eventually exchange messages?
The answers place a claim in the hierarchy. "Strongly consistent" with no answers to these questions is marketing, not a contract.
Checking a history by hand
You can check small histories mechanically. The sketch below models a single register with one writer and tests the real-time rule that linearizability imposes on reads. It is a teaching tool, not a full checker. It assumes each write has a unique value and that writes are not concurrent with the read being tested.
from dataclasses import dataclass
@dataclass
class Op:
kind: str # "write" or "read"
value: int # value written, or value returned
start: int
end: int
def stale_reads(history, initial=0):
"""Return reads that began after a write completed but returned
an older value. Assumes unique write values and sequential writes."""
writes = sorted((o for o in history if o.kind == "write"),
key=lambda o: o.end)
order = [initial] + [w.value for w in writes] # newest is last
stale = []
for r in (o for o in history if o.kind == "read"):
done = [w for w in writes if w.end < r.start]
if not done:
continue
newest_done = done[-1].value
if order.index(r.value) < order.index(newest_done):
stale.append(r)
return stale
history = [
Op("write", 1, 0, 2),
Op("read", 0, 3, 3),
]
print(stale_reads(history))Running it prints the read that returned 0 after the write of 1 had finished. That read is the violation of linearizability. The same history passes a check for eventual consistency, because that model has no real-time rule to break.
Real checkers must handle concurrent operations, where a read overlapping a write may legally return either value, and where many orderings must be searched. That search is why the problem is hard, and a later chapter on testing returns to it.
What a model does not tell you
A consistency model describes what clients may observe. It leaves several other properties open, and mixing them up is a common source of errors.
- Durability. A model says nothing about whether acknowledged writes survive a crash.
- Availability and latency. Stronger models tend to cost more of both, but the model itself sets no numbers. Later chapters on CAP and PACELC cover this trade-off.
- Isolation. The word "consistency" in ACID's "C" means that a transaction preserves application invariants. That is a different concept from the consistency models in this tutorial.
- Correctness of your application. A strong model does not stop you from writing a read-modify-write sequence that loses updates if the two steps are separate operations.
The last point catches many people. Even on a linearizable store, this code has a race:
def increment(store, key):
current = store.get(key) # operation 1
store.put(key, current + 1) # operation 2Each operation is individually linearizable. Two clients can both read 5 and both write 6, so one increment is lost. The contract covers single operations. To make the pair atomic you need a compare-and-set, a transaction, or a lock.
Common errors when reading a contract
- Assuming "consistent" means "latest". Only models with a real-time rule guarantee a read sees the most recent completed write.
- Judging a store from one run. A history that looks fine proves nothing. A model is violated by one bad history and satisfied only if none can occur.
- Ignoring the failure case. Many stores meet a strong model while the network is healthy and weaken during partitions or failovers. State the conditions under which the contract holds.
- Treating replicas as the unit. The contract is about what clients see, not about whether replicas hold identical bytes at every instant.
Summary of the idea
A consistency model is a set of allowed histories. Stronger models allow fewer, so they give you more guarantees and ask the store for more coordination. To evaluate any claim, identify the unit of operation, the ordering promised, and which clients must agree, then check concrete histories against that rule. The next chapter takes the strongest single-object contract, linearizability, and applies this method to it.
Exercise
Client A runs write(x, 1) from t=0 to t=2. Client B runs read(x) from t=3 to t=4 and returns 0. Client C runs read(x) from t=1 to t=5 and returns 0. The initial value is 0. Is this history allowed under linearizability? Explain for each read.
Show answer
B's read began after A's write completed, so it must return 1 or a newer value. Returning 0 is a stale read, and the history is not linearizable. C's read overlaps the write (its interval 1 to 5 overlaps 0 to 2), so it may legally return either 0 or 1, and C alone would not be a violation. B's read is enough to make the whole history illegal under linearizability.
Exercise
A store claims to be "strongly consistent". Name the three questions you would ask to turn that claim into a checkable contract.
Show answer
What is the unit of operation (single key or multi-key transaction)? What ordering is promised (real-time order, one global order, causal order, or none)? Which clients must agree (all clients, a single session, or only replicas eventually)?