Sub topic 3
Linearizability: behaving like a single copy
This chapter assumes you know what replication is, that a consistency model is a contract on histories, and that you can read short Python. The previous chapters covered those ideas. Here we define the strongest common contract for single objects, show how to check a history against it, and note where it is easy to get wrong.
The idea
A system is linearizable if every operation appears to take effect atomically at one instant, somewhere between the moment the client sent the request and the moment it received the reply. All clients must agree on the resulting order, and that order must respect real time.
Put simply, the system behaves as if there were one copy of the data, even though there are many replicas. A client cannot tell from the results that replication exists.
Linearizability is defined per object, such as one register, one key, or one queue. It says nothing about operations that touch several objects. We return to that in a later chapter on serializability.
Operations have duration
A history records operations with a start time (invocation) and an end time (response). Two operations are concurrent if their intervals overlap. They are ordered in real time if one finishes before the other starts.
The model has two rules:
- Single order. There must be one total order of all operations that makes every read return the value of the latest preceding write in that order.
- Real-time constraint. If operation A finishes before operation B starts, A must come before B in that order.
Concurrent operations may be placed in either order. That freedom is where all the flexibility comes from.
A history that fails
The example below uses an initial value of x = 0.
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.A's write finished at t=2. B's read started at t=3, so it began after the write was complete. Real-time order forces the write before the read, so the read must return 1. It returned 0, so the history is not linearizable.
The same history could still be sequentially consistent, because that model allows the read to be ordered before the write as long as each client's own order is kept. It could also be eventually consistent, since the new value may simply not have reached B's replica yet. Linearizability is the model that rules this out.
A history that passes
Change B's timing so that it overlaps A's write.
Client A: write(x, 1) starts t=0, completes t=4
Client B: read(x) starts t=2, returns 0
Client C: read(x) starts t=3, returns 1Is there a valid order? The write is concurrent with both reads. One order that works is: B's read (returns 0), then A's write, then C's read (returns 1). B's read finishes before C's read starts only if B's end time is before t=3, so check the constraint: if B finishes at t=2.5, the real-time order B before C holds and the history is linearizable. If B instead finished at t=5 and C had to be placed before B, C would return 1 and B would later return 0. That would break the single order, because the value would go backwards.
The rule to remember is that once any client has seen a new value, no later read may return the old one.
The linearization point
For each operation you can pick a linearization point, an instant inside its interval where it appears to happen. A history is linearizable if you can choose such points so that the operations, sorted by those points, form a valid sequential history.
This view helps when you design a system. For a single-leader store, the leader applying the write to its log is often the linearization point. For a read, the point might be the moment the leader confirms it is still the leader and reads its state.
Checking a history by brute force
For small histories you can test linearizability directly. The code below tries every ordering of the operations. It keeps only the orderings that respect real time, then replays them against a register.
from itertools import permutations
# Each operation: (name, kind, value, start, end)
# For a write, value is what is written. For a read, value is what was returned.
def is_linearizable(history, initial=0):
for order in permutations(history):
if not respects_real_time(order):
continue
if replays_correctly(order, initial):
return True
return False
def respects_real_time(order):
# If b finished before a started, then b must not come after a.
for i, a in enumerate(order):
for b in order[i + 1:]:
if b[4] < a[3]:
return False
return True
def replays_correctly(order, initial):
current = initial
for _, kind, value, _, _ in order:
if kind == "write":
current = value
elif value != current:
return False
return True
failing = [
("A", "write", 1, 0, 2),
("B", "read", 0, 3, 3.5),
]
passing = [
("A", "write", 1, 0, 4),
("B", "read", 0, 2, 2.5),
("C", "read", 1, 3, 3.5),
]
print(is_linearizable(failing)) # False
print(is_linearizable(passing)) # TrueThis takes factorial time, so it only suits tiny examples. Real checkers use smarter search, which a later chapter covers.
What linearizability gives you
- Recency. A read returns the latest completed write, or a write that is still in progress.
- Compare-and-set works. Locks, leader election, unique-ID allocation, and uniqueness constraints rely on an atomic check-and-update. These need a single agreed order of updates to one object.
- Simple reasoning. You can think about the system as one variable and ignore replication when writing the application.
What it costs
Linearizability requires coordination. A write cannot be acknowledged until enough replicas agree on its place in the order, and reads must not return stale data. This has two consequences:
- Latency. Each operation pays at least one round trip to a quorum or leader. Across regions that is tens to hundreds of milliseconds.
- Availability under partition. If a client can reach only a minority of replicas, a linearizable system must refuse or delay requests. The CAP theorem chapter makes this precise.
Gotchas
Stale reads from replicas or caches
Reading from a follower, or from a cache in front of the database, breaks linearizability even when the leader is linearizable. The follower may lag. Many databases offer a "read from replica" or "stale read" option that trades the guarantee for speed.
Quorums are not enough by themselves
With N replicas, choosing R + W > N makes read and write sets overlap. That alone does not make a store linearizable. Concurrent writes, sloppy quorums, clock-based timestamps, and a read that does not repair stale replicas can all still produce stale or reordered results.
Two leaders
If a failed leader recovers and believes it is still in charge, it may serve reads that miss newer writes made by the new leader. Linearizable systems need a way to stop an old leader, such as leases tied to a term number or a quorum check on each read.
Clock reliance
Some systems use synchronized clocks to bound how long a leader's lease lasts. If a clock drifts past the assumed bound, two nodes may both think they hold the lease. The guarantee is only as strong as the clock assumption.
Failed and timed-out operations
A client that times out does not know whether its write happened. For checking purposes, such an operation is treated as one that might take effect at any later time. A linearizable store can therefore still show a write the client thought had failed. Idempotent retries or request IDs handle this.
Not the same as serializability
Linearizability is about real-time order on a single object. Serializability is about multi-object transactions behaving as if run one at a time, with no real-time requirement. A database can offer one without the other. Having both is called strict serializability.
Where it sits among models
| Model | Single global order | Respects real time | Typical cost |
|---|---|---|---|
| Linearizability | Yes | Yes | Coordination on every operation |
| Sequential consistency | Yes | No, only per-client order | Lower than linearizable, still coordinated |
| Causal consistency | Only for related operations | No | Can stay available during partitions |
| Eventual consistency | No | No | Lowest latency |
Every linearizable history is also sequentially consistent, causally consistent, and eventually consistent. The reverse does not hold.
Exercise
Client A completes write(x, 7) at t=10. Client B starts read(x) at t=12 and returns the previous value 3. Client C starts read(x) at t=9, before A's write completes, and returns 3. Which of these reads violates linearizability, and why?
Show answer
B's read violates it. B started after A's write completed, so real-time order puts the write first, and B must see 7 (or a later value). C's read overlaps the write, since C started at t=9 and the write was still in progress until t=10. C may be ordered before the write and return 3, so C is allowed.
Exercise
A store lets clients read from any follower replica, while all writes go through a single leader that is linearizable. Is reading from followers linearizable? Give one concrete failure.
Show answer
No. A follower can lag behind the leader. A client writes x=1 through the leader and gets an acknowledgment, then reads from a follower that has not yet applied the write and gets the old value. This breaks the real-time rule. Reading from the leader after confirming it is still the leader would restore the guarantee.