Skip to content

Lesson 3 of 9 · 27m

Time, Ordering, and Vector Clocks

Understand why wall clocks cannot order distributed events, how vector clocks capture causality, and when version vectors surface conflicts in Riak-style systems.

The most dangerous sentence in distributed systems is "we can sort by timestamp." Machines disagree about time. NTP corrects clocks, sometimes backward. VMs pause. Containers migrate. Leap seconds happen. If correctness depends on one universal ordering of events, wall-clock time will eventually betray you.

Wall-clock time versus causality

A timestamp answers "what did this machine's clock say?" It does not answer "what did the writer know when it wrote?" Those are different questions:

11:00:00.100  node A: cart = {book}
11:00:00.090  node B: cart = {book, pen}

The second timestamp is earlier only because B's clock is behind. If B's write causally happened after reading A's write, replacing it because of a smaller timestamp destroys information. Ordering by physical time is attractive because it is simple, and dangerous for the same reason.

Lamport clocks: ordering with gaps

A Lamport clock is a counter carried with messages. Each process increments its counter before an event and takes the maximum when receiving a message:

A1 -> A2 -> send m(counter=2)
                                     B receives: B clock = max(B clock, 2) + 1

If event X causally happens before event Y, then Lamport(X) < Lamport(Y). The reverse is not true. Two events can have different counters and still be concurrent. Lamport clocks give a partial causal ordering, not conflict detection.

Vector clocks make concurrency visible

A vector clock tracks one logical counter per writer:

A writes after seeing A:1              => {A:1}
B writes independently                 => {B:1}
B reads {A:1}, then writes             => {A:1, B:2}

Compare vectors component by component:

{A:1}          <  {A:1,B:2}    causal history
{A:2}          ?? {B:1}        concurrent conflict
{A:2,B:1}      >  {A:1}        strictly newer

This is the important mental model: concurrency is not an error. It is a fact that must be represented and resolved.

Riak made this operational

Riak historically exposed vector clocks as opaque metadata. Clients fetched the object and supplied the vector clock when writing so the database could know what history the write replaced. A simplified interaction looks like:

GET /buckets/cart/keys/user-42
X-Riak-Vclock: a85hYGBgzGDKBVI8+xkzyo...

If siblings appeared, the application received multiple versions and had to merge them. For a cart, set union may be reasonable. For a bank balance, it is not. The correct merge logic belongs to the domain, not the database.

Last-write-wins is a business decision pretending to be a database feature

Many systems collapse conflicts using timestamps because it is easy. Sometimes that is acceptable: overwriting a display name, updating a cache entry, or recording approximate location. Sometimes it is catastrophic: inventory counts, document collaboration, permission grants, or medical data. CRDTs exist for structures where mathematically safe merge behavior is possible, but they do not magically solve domain-level invariants.

Field notes

  • Never use updated_at DESC as a correctness primitive across independently writable nodes.
  • NTP synchronization reduces clock error; it does not create a total causal order.
  • If two writes cannot be ordered causally, the system must choose conflict resolution, not pretend one vanished.
  • Vector clocks and version vectors are related but not identical operational objects; know which one your database exposes.
  • Monotonic clocks are excellent for measuring elapsed time on one machine and useless for ordering events across machines.

Was this lesson helpful?

Sign in to rate