Sub topic 4
Sequential consistency: one order, no real-time guarantee
This chapter assumes you know what replication is and have read the chapter on linearizability. You should be able to read short Python and timelines of client operations.
The definition
Sequential consistency is a consistency model. It says a system's results must look as if every operation ran one at a time, in a single global order. That order must also respect each client's own program order. If client A does write(x, 1) and then write(x, 2), every client must see 1 before 2.
Linearizability adds one more rule: the global order must match real time. Sequential consistency drops that rule. An operation that finished earlier on the wall clock can appear later in the global order, as long as no client's own order is broken.
The history from the spine
Take this history again.
Client A: write(x, 1) starts t=0, completes t=2
Client B: read(x) starts t=3, returns 0
Initial value of x is 0.This history is not linearizable. B's read began after A's write completed, so it had to return 1.
It is sequentially consistent. The global order can be read(x) -> 0, then write(x, 1). Each client's own operations stay in their own order, and every result makes sense. Nothing in the model says the order must follow the clock.
What the model guarantees
- All clients agree on one order of operations.
- Each client's operations appear in the order that client issued them.
- Real time is ignored. A read may return stale data, as long as the stale value fits some valid order.
The next history shows what the model forbids.
Client A: write(x, 1); write(x, 2)
Client B: read(x) -> 2; read(x) -> 1No single order can explain this. A wrote 1 before 2, so B cannot see 2 and then 1. A system that allows this is not sequentially consistent.
Checking a history in code
The function below tests one narrow property. It checks that a client's reads never go back to an older write of a single register. The writes are numbered in the order they were applied.
def reads_go_backwards(read_positions):
"""read_positions: the write index each read observed, in client order."""
return any(b < a for a, b in zip(read_positions, read_positions[1:]))
print(reads_go_backwards([0, 2, 2])) # False
print(reads_go_backwards([0, 2, 1])) # TrueA full checker has to search for a legal global order. That search is expensive, which the testing chapter covers.
How it is implemented
A common design sends every write through one sequencer, such as a primary replica that numbers each write. Every replica applies writes in that number order. Reads can go to any replica, which may be behind. Each client keeps reading from the same replica, so its reads never go backward.
Replica 2 returns 0 even though A's write already completed. This is legal under sequential consistency and illegal under linearizability.
Common errors
- Treating "sequential" as "in real-time order". It is not. Staleness is allowed.
- Assuming that two clients see the same value at the same moment. Only the order is shared.
- Letting a client switch between replicas. Moving from a fresh replica to a stale one can break the client's own order.
Exercise
Client A runs write(x, 1) and it completes. Afterward, client B reads x and gets the initial value 0. Is this allowed under sequential consistency? Under linearizability?
Show answer
It is allowed under sequential consistency, because the global order can place B's read before A's write without breaking either client's own order. It is not allowed under linearizability, because B's read began after the write completed and must return 1.
Exercise
Client A writes x=1 and then x=2. Client B reads 2 and then reads 1. Can a sequentially consistent system produce this?
Show answer
No. All clients must see one order that keeps A's program order, so 1 comes before 2. B cannot see 2 and then 1.