Clocks and ordering
Clocks on different machines disagree and can jump, so a timestamp does not prove which event came first. Order comes from a counter, a single owner or a bounded clock, and the choice decides what conflicts you can see.
What it is
Start with the two clocks a machine offers. The wall clock (CLOCK_REALTIME on Linux) reports seconds since 1970 and can be set: it jumps when an administrator changes it, and NTP nudges its rate to keep it near UTC. The monotonic clock (CLOCK_MONOTONIC) counts from an unspecified point, on Linux the moment of boot. Jumps in system time do not move it, two consecutive reads of it do not go backwards, and it stops while the machine is suspended. The first tells you what time it is; the second tells you how much time has passed. Two machines' wall clocks also differ from each other, and without a protocol that measures and bounds the offset a program has no figure for it; that is where the ordering problems below start.
Even well-run clocks disagree on purpose. Google has smeared leap seconds rather than stepping its clocks since 2008. Its current recommendation, which it plans to follow for future leap seconds, spreads the extra second linearly over the 24 hours centered on the leap, midday to midday in UTC: a rate change near 11.6 parts per million, and the same scheme Amazon uses, replacing earlier smears that ran over 20 hours. During a smear day a smeared clock reads differently from one that steps, so two correct machines on different time services disagree by a fraction of a second. Our advice: treat a wall-clock timestamp as a label for humans and a rough position in time, not as evidence of order.
Leslie Lamport's answer was to stop asking what time an event happened and ask what it could have influenced. Event a happened before b if you can walk from a to b by staying in one process and moving forward, or by following a message from its send to its receipt. Events with no such path are concurrent. A Lamport clock is a counter per process: increment it before each local event, attach it to each message you send, and on receipt set your counter to the larger of your own and the message's, then add one. If a happened before b, a's timestamp is smaller. The reverse does not hold: a smaller timestamp may mean earlier or unrelated. Breaking ties with a process id gives a total order, but that order is a convention, not a cause.
A vector clock replaces the single counter with one counter per process. Each process increments its own slot on a local event or a send, ships the whole vector with each message, and on receipt bumps its own slot and takes the larger value in each of the other slots. Compare two vectors: if each slot in the first is less than or equal to the matching slot in the second, and at least one is smaller, the first version came before the second and can be dropped. If each vector has a slot larger than the other's, the two versions were written concurrently, and a store can keep both until the application merges them. That detection is what a Lamport clock lacks. The cost is size: one slot for each process that writes, carried on each version.
A hybrid logical clock (HLC) gives each timestamp a physical part, kept close to the local wall clock, and a logical part that separates events sharing the same physical value. CockroachDB's design document describes each node keeping one HLC that is updated by each read and write and by the timestamp on each incoming request, so the node's clock stays at or above its wall time and ahead of the timestamps it has stored. Clock skew is handled by a configured maximum offset: a read at time t treats a value stamped between t and t plus that offset as uncertain and restarts at a later timestamp. The gain over a Lamport clock is a value that still reads as a time; the cost is that the maximum offset must hold, or the uncertainty check misses a write.
When to use it
One owner, one counter. When a single process owns a stream, let it assign the order: the server holding a conversation, the shard holding a partition, the leader holding a document. Each accepted event gets the next integer. Readers then have a cheap test for loss: a gap in the numbers. A client stores the highest number it has applied and, after a reconnect, asks for everything above it. Our advice: in a design round, reach for this first, and justify anything more elaborate by naming a write path that has more than one owner.
A logical clock fits when writes to the same data start in several places and no single process sees them all. A Lamport counter with a node-id tiebreak is enough when all nodes must agree on one order and nobody needs to know which operations were concurrent. A vector clock is the tool when concurrent writes must be detected and merged rather than overwritten, as with a shopping cart that should keep the items from both branches. A hybrid logical clock fits a multi-node database where timestamps must be comparable across nodes, stay readable as times in a log, and bound the damage of skew to a configured offset.
The monotonic clock is for durations measured inside one running process: a heartbeat deadline, a retry backoff, a request timeout. Computed from wall-clock timestamps, a step forward can fire those early and a step back can stall them; the monotonic clock sees neither. On Linux it stops while the machine is suspended, and it counts from boot, so a lease or a window that must outlive either needs a different basis, such as a stored wall-clock expiry or a check against the source of truth. Keep the wall clock for display and for the time stored with a record.
When not to
Last write wins is sometimes right. When losing one of two concurrent updates is acceptable, let one version win and skip vector clocks. The Dynamo paper lays out this choice: a store that resolves conflicts on its own is limited to simple policies such as last write wins, while an application that knows its data can merge, as a shopping cart service does, and its authors note that some developers prefer to push the decision down to the store anyway. A presence flag, a last-seen time or a profile field one user edits from one device are good candidates for the simple policy. Our advice: for a cart, a balance or a counter, decide on purpose whether an overwritten branch is acceptable, because that discarded write was real work.
Skip vector clocks when one owner can number the stream. They attach a vector to each stored version, oblige the reader to handle several versions at once, and grow with the number of writers. If the design already routes writes for a key or a conversation through one process, that process's counter is a total order with none of this cost. Likewise, skip a Lamport clock when you need to know what was concurrent: it gives concurrent events timestamps that look ordered.
Don't build a database's clock machinery into an application. Uncertainty windows and restarts, as in CockroachDB, pay off when many nodes commit transactions at timestamps that must be comparable. Our advice: for a chat or feed service, ask first whether the ordering question is per conversation or per user; if it is, a counter assigned by the owner answers it without any of that machinery. Timestamps from a user's device deserve even less trust; record them for display, but don't let them decide order or conflicts.
Compared
| Order source | What it gives you | What it costs | When it is the right choice |
|---|---|---|---|
Wall-clock timestamp | A readable time; last write wins on conflict | Clocks differ by an unknown offset and can jump; a concurrent update is dropped | Display, logs, and fields where losing a concurrent update is acceptable |
Monotonic clock | Durations that don't go backwards on one machine | Meaningless on another machine; may stop during suspend | Timeouts, heartbeats and backoff measured inside one running process |
Counter from one owner | A total order and gap detection inside one stream | The owner is a single writer; order across streams is undefined | A conversation, partition or document with one owner |
Lamport clock | Cause before effect across nodes, in one integer | Unrelated events look ordered; concurrency is invisible | Agreement on one order, with a node-id tiebreak, when conflicts don't need detecting |
Vector clock | Detects concurrent versions so they can be merged | One slot per writer; the application reconciles | Multi-leader writes to the same key where no update may be lost |
Hybrid logical clock | Comparable timestamps that still read as time, with bounded skew | Relies on a configured maximum offset; a read that meets a value stamped inside its uncertainty window restarts | Distributed databases ordering transactions across nodes |
In a picture
Step 1 of 4
A ticks
Process A does a local event and increments its counter to 1.