Sub topic 4
Sequential consistency: one order, no real-time guarantee
This chapter assumes you know what replication is and why replicas can disagree. It also assumes you have read the previous chapter on linearizability, and that you can read short Python programs. Here you will learn what sequential consistency promises, what it drops compared with linearizability, and how to check a history against it.
The definition
Sequential consistency was defined by Leslie Lamport in 1979 for multiprocessor memory. A system is sequentially consistent if the result of any execution is the same as if:
- all operations from all clients ran in one single sequence, and
- the operations of each client appear in that sequence in the order that client issued them.
The first condition says there is one global order that every client agrees on. The second condition is called program order: a client's own operations are never reordered relative to each other.
Linearizability has both conditions and adds a third. If operation X finished before operation Y started, in wall-clock time, then X must come before Y in the global order. Sequential consistency leaves this third condition out. That is the whole difference, and the rest of the chapter works through its consequences.
What "no real-time guarantee" means
Take the history from the linearizability chapter. 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 0This history is not linearizable, because B's read began after A's write completed but still returned the old value. It is sequentially consistent. The two clients do not constrain each other's program order, so the system may place B's read before A's write in the global order. A legal order is: read(x) -> 0, then write(x, 1).
The system is allowed to act as though B's read happened "earlier" than the wall clock says. The order only has to be consistent with each client's own sequence of operations.
What sequential consistency still forbids
The model is weaker than linearizability, but it is not permissive. It rules out several anomalies.
- Reordering within a client. If A writes
x = 1and theny = 1, no client may seey = 1whilexis still 0. This breaks A's program order. - Going backwards. A client that reads the new value and then the old value has violated its own order, assuming no other write restored the old value.
- Disagreement on write order. Two clients must not see two concurrent writes to the same register in opposite orders. A single global order means everyone sees the same one.
Checking a history in code
You can test small histories with a brute-force search. The function below tries to build one global order, one operation at a time, while keeping each client's operations in order. A read is only allowed when it returns the current value of the key. If the search reaches the end, a valid order exists and the history is sequentially consistent.
def is_sequentially_consistent(clients, initial=0):
"""clients: dict of client name -> list of (kind, key, value) operations.
kind is "w" for a write or "r" for a read that returned value.
Returns True if some interleaving that keeps each client's order
makes every read return the latest written value."""
names = list(clients)
def search(positions, state):
if all(positions[n] == len(clients[n]) for n in names):
return True
for n in names:
i = positions[n]
if i == len(clients[n]):
continue
kind, key, val = clients[n][i]
if kind == "w":
new_state = {**state, key: val}
elif state.get(key, initial) == val:
new_state = state
else:
continue
if search({**positions, n: i + 1}, new_state):
return True
return False
return search({n: 0 for n in names}, {})
# 1. The history above: B's read can be ordered before A's write.
print(is_sequentially_consistent({
"A": [("w", "x", 1)],
"B": [("r", "x", 0)],
})) # True
# 2. B sees the new value, then the old one: B's own order is violated.
print(is_sequentially_consistent({
"A": [("w", "x", 1)],
"B": [("r", "x", 1), ("r", "x", 0)],
})) # False
# 3. Two readers disagree on the order of two writes.
print(is_sequentially_consistent({
"A": [("w", "x", 1)],
"B": [("w", "x", 2)],
"C": [("r", "x", 1), ("r", "x", 2)],
"D": [("r", "x", 2), ("r", "x", 1)],
})) # False
# 4. A's writes to x and y are seen out of order by B.
print(is_sequentially_consistent({
"A": [("w", "x", 1), ("w", "y", 1)],
"B": [("r", "y", 1), ("r", "x", 0)],
})) # FalseThe search is exponential in the number of operations. In general, checking sequential consistency of a history is NP-complete. Use this only to understand the model, not to verify real systems. The testing chapter covers practical tools.
Comparing it with linearizability
| Property | Linearizability | Sequential consistency |
|---|---|---|
| Single global order | Yes | Yes |
| Each client's order preserved | Yes | Yes |
| Order respects real time across clients | Yes | No |
| Reads may be stale in wall-clock terms | No | Yes |
| Composable across objects | Yes | No |
The last row needs explanation. Linearizability is local: if every object in a system is linearizable on its own, the whole system is linearizable. Sequential consistency is not. Two registers can each be sequentially consistent, yet no single order may exist that satisfies program order across both. Per-object guarantees do not add up to a system-wide guarantee, so you must reason about the whole system.
The out-of-band problem
The missing real-time condition has a practical cost. The system only knows about operations that pass through it. If client A writes a value and then tells client B by phone, chat, or another service, the system has no record of that dependency. Under sequential consistency B may still read the old value, and the model has not been violated.
Linearizability closes this gap because its order follows real time, and the phone call happens in real time. Sequential consistency does not. A system built on it must either avoid hidden channels between clients or accept that they can observe stale data.
How systems provide it
A common design sends all writes through a single ordering point, such as a leader or a replicated log. Every replica applies writes in log order, so all replicas pass through the same sequence of states. Reads can then be served by any replica, which is cheap and fast. A replica may lag behind the leader, which produces stale reads, but the log order means no replica ever shows writes out of order.
The one thing a client must not do is move to a replica that is further behind. If a client reads at log position 10 on one replica and then at position 7 on another, it has gone backwards, and its own program order is broken. Systems prevent this by having clients track the log position they have seen and refuse to read from a replica that is behind it. Apache ZooKeeper works this way. It orders all updates through a leader, applies a client's requests in the order sent, and lets followers serve reads that may be stale. The sync operation exists for clients that need a read at least as fresh as the current leader state.
Multicore processors face the same issue in a different setting. Many CPUs use weaker memory models for speed, and programmers restore sequentially consistent behavior with memory barriers or atomic operations.
When it is a good fit
Sequential consistency suits workloads where:
- all clients share one logical order of events, such as a log or a configuration store,
- reads may be a little stale as long as they never go backwards,
- cheap local reads matter more than reading the latest committed value.
It is a poor fit when users compare results across channels or when a read must reflect a write that has already been acknowledged. For example, a payment service that confirms a charge must not let another service read a pre-charge balance. That workload needs linearizability.
Exercises
Exercise
Client A writes x = 1 and then x = 2. Client B reads x twice and gets 2 and then 1. Is this history sequentially consistent? Assume the initial value is 0 and that no other client writes.
Show answer
No. The global order must keep A's writes in order, so x = 1 comes before x = 2. B read 2 first, which puts the write of 2 before B's second read, and B then read 1. After the write of 2 the register never returns to 1, so no legal order exists.
Exercise
A history is sequentially consistent but not linearizable. Name the one property of the history that must be responsible, in terms of time.
Show answer
Some operation finished, in wall-clock time, before another one started, yet the only valid global order puts the later operation first. Sequential consistency ignores real-time order across clients, and linearizability requires it.
Exercise
Replicas apply writes in the same log order and serve reads locally. A client reads from replica R1 at log position 10, then its next request goes to replica R2 at position 7. Which guarantee breaks, and what should the client do to prevent this?
Show answer
The client's own program order breaks, because it sees state go backwards. The client should remember the highest log position it has seen and only accept reads from replicas that have applied at least that position, or wait or retry elsewhere until one has.