0%
Distributed Systems: Consensus, Clocks, and Replication
Foundations
Time and Order
Replication
Consensus
Transactions and Operations
Linearizability
Earlier lessons kept saying that some technique does or does not give strong consistency. This lesson makes that phrase precise. The strongest common consistency model for single objects is linearizability, defined by Maurice Herlihy and Jeannette Wing in 1990. It is the guarantee that most people intuitively expect from a database and the one that many distributed systems quietly fail to provide. Understanding exactly what it promises, how to check it, how to build it and what it costs is the foundation for the rest of the consistency and consensus material in this course.
The single copy illusion
Informally, linearizability means that a replicated object behaves as if there were only one copy of it and every operation took effect instantly at some moment between when it was invoked and when it returned. Clients cannot tell that the object is replicated. If one client writes a value and the write returns, every client that starts a read afterward sees that value or a newer one. If one client reads a new value, every client that starts a read afterward also sees it or something newer. Values never appear to go backward, and no client can observe a state that a single copy could not have produced.
Operations as intervals
To reason precisely, think of each operation as an interval in real time, from its invocation, when the client sends the request, to its response, when the client receives the answer. Two operations are concurrent if their intervals overlap; otherwise one precedes the other in real time. Linearizability says that each operation appears to take effect at a single point, its linearization point, somewhere inside its own interval, and the results of all operations are consistent with applying them in the order of those points to a single copy of the object.
This definition is subtle in a useful way. For concurrent operations, the system is free to order them either way, because their intervals overlap and either point placement is legal. For operations that do not overlap, the order is forced: if a write returned before a read started, the write point is earlier than the read point, so the read must see the write. The real time constraint is what distinguishes linearizability from weaker models.
A simple violation
Suppose a register holds 1. Client A writes 2, and the write returns. Then client B, starting after A finished, reads and gets 1. No placement of points can explain this: the write point is inside its interval, which ended before the read began, so the read point comes after the write point, and a single copy would return 2. This history is not linearizable. It is exactly what an asynchronous follower produces when it serves reads, and exactly what a leaderless quorum system can produce during the windows described in the quorums lesson.
A slightly harder case involves two readers. A write of 2 is in progress. Reader B reads and gets 2. Then reader C starts after B finished and reads 1. Each read individually overlaps the write, so each alone is fine, but together they are not: B saw 2, so the write point is before B read point, which is before C read point, so C must also see 2. This new then old pattern is the most common linearizability violation in replicated systems, and it is invisible to anyone checking reads one at a time.
What linearizability covers
Linearizability is defined per object. A key value store is linearizable if each key individually behaves like a single register. That says nothing about operations that touch several keys together; those require transactions and a model such as serializability, covered in the next section. Linearizability also applies to richer objects than registers: a linearizable queue behaves like a single queue, a linearizable lock like a single lock, and a linearizable compare and set like a single atomic variable. The definition is the same: each operation takes effect at an instant inside its interval, in an order consistent with the object sequential specification.
Why it is useful
Linearizability makes distributed systems behave like the single machine programs people already know how to reason about. A lock built on a linearizable store cannot be held by two clients at once. A uniqueness check for a username can be done with a linearizable compare and set and is guaranteed correct. A leader election that writes the winner into a linearizable register is unambiguous. A client that writes something and then tells another client through a side channel, such as a message queue or a phone call, can be sure the other client will see the write when it reads. That last property, consistency across side channels, is often overlooked and is where weaker models cause surprising bugs.
The side channel problem
Consider an image upload service. A web server stores an uploaded image in a replicated file store and then puts a message on a queue telling a resize worker to process it. The worker reads the image from the store. If the store is not linearizable, the worker may read from a replica that has not yet received the image and fail or process an old version, even though the upload finished before the message was sent. The message queue is a side channel the store knows nothing about. Only linearizability, or explicitly passing a version and waiting for it, guarantees the worker sees the image. Martin Kleppmann uses this exact example in Designing Data Intensive Applications to show why the real time guarantee matters.
Composability
Linearizability has an important property called locality or composability: a system made of independently linearizable objects is linearizable as a whole. If each key in a store is linearizable, the store as a collection of keys is linearizable for single key operations, without any extra coordination across keys. This lets systems shard data across many independent consensus groups, as Spanner, CockroachDB and TiKV do, while still offering linearizable single key operations. Serializability, by contrast, does not compose this way, which is one reason multi key transactions are much harder than single key operations.
Linearizability and atomic objects
The definition comes from concurrent programming on a single machine, where it describes atomic objects such as a concurrent queue or an atomic integer shared by threads. Herlihy and Wing wanted a correctness condition that matched how programmers think about such objects: each method call seems to happen all at once, at some moment while it runs. Distributed systems borrowed the definition unchanged, which is convenient: the same reasoning that justifies a lock free queue in one process justifies a replicated register across many machines, and the same testing tools apply. The distributed setting only makes it harder to achieve, because the copies are separated by an unreliable network rather than a shared memory bus.
A mental test
A practical test for whether a system behaves linearizably is to imagine a single client that can observe every operation instantly and ask whether any sequence of instantaneous updates to one copy could have produced what clients saw, given when each operation started and ended. If the answer requires an operation to take effect before it was invoked or after it returned, or requires values to go backward, the system is not linearizable. This test is easy to apply to small examples in design reviews and catches most violations before any code is written.
Linearizability means each operation takes effect at one instant within its interval, so once a write returns or any reader sees a value, every later operation sees it too. That real time guarantee is what makes locks, uniqueness checks and side channel coordination safe.