0%
Distributed Systems: Consensus, Clocks, and Replication
Foundations
Replication
Consistency Models
Consensus
Transactions and Operations
Vector Clocks and Causality
The previous lesson ended with a limit. Lamport timestamps guarantee that causes get smaller numbers than their effects, but they cannot tell whether a smaller number belongs to a cause or to an unrelated concurrent event. For many problems that limit is fine. For one large class of problems, it is fatal: any system that must notice when two updates were made independently, so that it can merge them or surface a conflict, needs to detect concurrency exactly. Vector clocks, introduced independently by Colin Fidge and Friedemann Mattern in 1988, solve this by keeping one counter per process instead of one counter overall. This lesson explains how they work, why they capture happens before exactly and how version vectors bring the same idea to replicated databases.
The problem in concrete terms
Picture a shopping cart replicated across two data centers. A customer on a phone adds headphones while the phone is talking to the east region. A moment later, on a laptop connected to the west region, the same customer adds a charger. Neither region has heard from the other yet. When the regions exchange updates, each must decide what to do with the incoming version of the cart. If the east version happened before the west version, the west version already includes everything the east version knows, and the east version can be discarded. If the two were made concurrently, neither includes the other, and discarding either loses an item the customer added.
With Lamport timestamps, the east update might carry 17 and the west update 18. The system sees 17 less than 18 and keeps the west version, losing the headphones. Nothing in the numbers says the two updates were concurrent. The same failure appears with physical timestamps, as the physical clocks lesson showed with last write wins. The system needs a representation of history rich enough to answer the question: does this version include everything that version knew?
What information is missing
A Lamport timestamp summarizes the entire causal past of an event in one number, the length of the longest chain leading to it, roughly speaking. Many different pasts produce the same number, so the number cannot be used to compare pasts. To decide whether one event happened before another, a process needs to know, for each other process, how much of that process history the event has seen. If event b has seen everything event a has seen and more, a happened before b. If each has seen something the other has not, they are concurrent. That per process accounting is exactly what a vector clock stores.
A vector of counters
A vector clock in a system of n processes is an array of n integers. Entry i counts how many events of process i are in the causal past of the current event, including the event itself if it is on process i. When process P2 holds the vector [3, 5, 1], it means: my current state reflects the first three events of P1, the first five events of P2 and the first event of P3. The vector is a compact summary of the causal past, and unlike a Lamport timestamp it is exact. Two events have the same vector only if they have the same causal past, and comparing vectors entry by entry tells you how the pasts relate.
Where conflicts come from
Concurrent updates are common in practice, not exotic. They arise whenever clients can write to more than one replica at once, as in multi leader and leaderless databases, when devices work offline and synchronize later, as in mobile apps and collaborative editors, and when a network partition separates replicas that both keep accepting writes. Systems built for availability across regions accept writes locally and replicate asynchronously precisely to avoid waiting on distant replicas, and the price is that concurrent updates will happen. The system can either detect them and handle them deliberately, or let an arbitrary rule discard some of them. Vector clocks make the first option possible.
What detecting concurrency buys you
Once a system can tell concurrent updates from ordered ones, it has choices. It can keep both versions as siblings and let the application or user merge them, as Amazon Dynamo did for shopping carts and Riak does by default. It can merge automatically with a data type designed for it, such as the conflict free replicated data types covered later in the course. It can apply a deterministic rule but only for genuinely concurrent updates, leaving ordered updates untouched. Or it can alert someone. All of these depend on knowing that a conflict happened, and none of them is possible with a single counter.
Detecting versus preventing
It helps to separate two strategies for concurrent writes. Prevention means making sure concurrent writes to the same item cannot happen, typically by routing all writes for an item through one leader or by taking a lock. Detection means allowing concurrent writes and noticing them afterward. Prevention gives simpler semantics but costs latency and availability, because every write must reach the single place that orders it. Detection keeps writes local and fast but pushes complexity into merging. Single leader databases choose prevention. Dynamo style stores, offline first apps and collaborative editors choose detection, and vector clocks or their relatives are the tool that makes detection reliable.
A brief history
Lamport described scalar logical clocks in 1978. A decade later, Fidge and Mattern independently published vector time, showing that vectors characterize happens before exactly. Around the same period, version vectors were developed for replicated file systems, notably in the Locus system and later in Coda, to detect conflicting file updates made by disconnected clients. Amazon popularized the idea for databases with its 2007 Dynamo paper, which used vector clocks to detect conflicting shopping cart versions. Riak, Voldemort and other Dynamo inspired databases followed. More recent work, such as dotted version vectors and interval tree clocks, refines the idea to fix practical problems that the original formulations had in large or dynamic systems.
Why physical timestamps make it worse
Teams sometimes reach for physical timestamps instead of a logical counter, reasoning that a real clock at least reflects real time. For conflict detection it is the worst of both worlds. Physical timestamps still map every update onto one number, so they still hide concurrency, and they add clock skew on top, so a causally later update can even get a smaller timestamp and lose to the update it was meant to replace. The physical clocks lesson showed this failure with last write wins. A logical counter at least never reverses a causal order. A vector of counters goes further and exposes concurrency itself, which is the property conflict handling needs.
Thinking in terms of knowledge
A useful way to read a vector is as a statement of knowledge. The vector [3, 5, 1] says: I know about three things P1 did, five things P2 did and one thing P3 did. Comparing two vectors then asks whether one state knows everything the other knows. If it does, the more knowledgeable state can safely replace the other, because nothing is lost. If each knows something the other does not, replacing either would throw away knowledge, so the states must be combined. This framing carries straight over to replicated data, where knowledge means the writes a version reflects.
The rest of this lesson builds that representation step by step, starting with the rules for a single vector, then applying them to stored data and finally to message delivery.
Keep the cart example in mind throughout. Every mechanism that follows is ultimately a way to make sure both the headphones and the charger end up in the merged cart, and that the system knows it had to merge.
A single counter maps a partial order onto integers, so it must invent an order for concurrent updates and silently discards one of them. Systems that accept writes in several places need a representation of history, one counter per process, that can tell an update that supersedes another from one made independently.