Hybrid Logical Clocks and TrueTime

Topics Covered

Wanting Both Kinds of Time

What physical time gives you

What logical time gives you

The design space

Requirements in a typical database

Timestamp oracles as a baseline

Why not just synchronize clocks better

Multiversion storage depends on timestamps

A concrete failure story

The vocabulary of guarantees

Where this lesson goes next

The last three lessons presented two families of clocks. Physical clocks track real time but drift, jump and disagree, so they cannot order events safely. Logical clocks order events consistently with causality but bear no relation to real time, so they cannot answer questions like what did this table look like at noon, or has this lease expired. Many systems need both. A database that offers snapshot reads at a timestamp, time travel queries, time based retention and globally consistent transactions wants timestamps that are close to wall time and that never contradict causality. This lesson covers the two main answers: hybrid logical clocks, which combine a physical and a logical component in one value, and TrueTime, which bounds physical clock uncertainty so tightly that physical timestamps alone become safe, at the cost of waiting.

What physical time gives you

Physical timestamps are meaningful to people and to other systems. An operator investigating an incident can ask for every write between 14.02 and 14.05. A compliance rule can require deleting data older than thirty days. A backup can be restored to a point in time. A query can read a snapshot as of a timestamp and get a result that corresponds to a moment people recognize. Physical time also lets machines that never communicate agree roughly on ordering, because their clocks are all approximately synchronized to the same source. Logical clocks offer none of this: a Lamport timestamp of 48,211 means nothing outside the system.

What logical time gives you

Logical timestamps never contradict causality. If a write was made after reading another write, its timestamp is larger, no matter how clocks drift. Physical timestamps cannot promise this: if the second machine clock is behind, the later write gets a smaller timestamp, and a snapshot read at some time could include the second write but not the first, showing an effect without its cause. For databases, that kind of anomaly breaks invariants. A transfer could appear to deposit money that was never withdrawn, or a foreign key could point at a row that does not yet exist in the snapshot.

When a clock runs behind, a causally later write can get an earlier physical timestamp, so a snapshot can include the effect but not its cause.

The design space

There are three broad ways to get both properties. The first is to keep physical timestamps and accept rare anomalies, as many systems with last write wins do. The second is to combine physical and logical components so that timestamps track wall time closely but are bumped forward whenever causality requires, which is what hybrid logical clocks do. The third is to bound clock uncertainty explicitly and wait out the uncertainty before making results visible, so that physical timestamps become safe, which is what Spanner does with TrueTime. Each makes a different trade between precision, latency and infrastructure cost.

Requirements in a typical database

Consider what a distributed SQL database needs from its timestamps. Each transaction needs a commit timestamp. Snapshot reads need to see exactly the transactions with commit timestamps at or below the read timestamp. If transaction T2 starts after T1 commits, in real time, many applications expect T2 to see T1, a property called external consistency or strict serializability. Timestamps should be close enough to wall time for time travel queries and garbage collection of old versions. And timestamp generation must not require contacting a central service on every transaction, or it becomes a bottleneck and a single point of failure. Hybrid logical clocks satisfy most of these; TrueTime satisfies all of them, at a price.

Timestamp oracles as a baseline

The simplest way to get monotonic, causally consistent timestamps is a timestamp oracle: one service that hands out increasing timestamps. Google Percolator used one, and TiDB uses a placement driver that serves timestamps in batches. An oracle gives external consistency trivially, since every transaction gets its timestamp from the same counter, and it can embed physical time by using the wall clock as the high bits. The costs are a network round trip to the oracle for each transaction and the need to make the oracle highly available, typically by replicating it with consensus. For a single region, the round trip is small and the oracle works well. Across continents, the round trip dominates latency, which is why globally distributed databases avoid it.

Why not just synchronize clocks better

Better synchronization shrinks the problem but does not remove it. Even with precise time protocols, there is always some residual error, and a system that assumes zero error will eventually misorder events when a clock misbehaves. The interesting systems either make timestamps robust to error, as hybrid logical clocks do, or measure the error and design around it, as TrueTime does. Both approaches treat clock error as a quantity to engineer with, not a bug to ignore.

Multiversion storage depends on timestamps

The reason timestamps matter so much to databases is multiversion concurrency control. Most modern databases keep several versions of each row, each tagged with the timestamp of the transaction that wrote it. A reader at timestamp t sees, for each row, the newest version with a timestamp at or below t, which gives it a consistent snapshot without blocking writers. Old versions are garbage collected once no reader could still need them, typically after a time horizon such as a few hours. Everything about this scheme depends on timestamps: they decide which versions a snapshot includes, which writes conflict and when versions can be deleted. If timestamps contradict causality, snapshots become inconsistent. If timestamps drift far from wall time, garbage collection horizons and time travel queries stop meaning what operators expect.

A concrete failure story

Consider a ledger service that records transfers between accounts on two nodes. A transfer debits account A on node one, then credits account B on node two, with the second step triggered by a message from the first. If node two clock runs 50 milliseconds behind, the credit can get an earlier timestamp than the debit. An auditor running a snapshot query at a moment between the two timestamps would see money arriving in B without having left A, and the sum of balances would be too large. Nothing crashed, no write was lost and every individual timestamp was taken from a correctly running clock. The bug is purely in using unadjusted physical timestamps for causally related writes. Hybrid logical clocks fix exactly this case, because the message from node one carries its timestamp and node two moves past it.

The vocabulary of guarantees

Several terms in this lesson describe related but different guarantees. Causal consistency means causally related operations are ordered correctly everywhere. Linearizability means each operation on a single object appears to take effect at one instant between its start and end, so real time order is respected for that object. Serializability means transactions appear to run in some serial order, not necessarily matching real time. Strict serializability, also called external consistency, combines the last two: transactions appear to run in a serial order that respects real time. Spanner provides strict serializability. Many HLC based databases provide serializability plus per key linearizability. Keeping these straight makes vendor documentation much easier to read.

Where this lesson goes next

The rest of the lesson takes the approaches in order of how much infrastructure they need. Hybrid logical clocks need nothing beyond ordinary synchronized clocks and a few lines of code. TrueTime needs dedicated time hardware but buys the strongest guarantee. Uncertainty intervals on commodity clocks sit between them. The final section brings the ideas down to everyday engineering tasks such as generating identifiers.

Throughout, keep one question in mind: what does each approach assume about clocks, and what happens when the assumption fails?

Real time for people, causal time for correctness

Physical timestamps are meaningful and comparable without communication but can reverse causality when clocks skew; logical timestamps preserve causality but mean nothing outside the system. Databases need both, and the options are accept anomalies, combine the two in a hybrid clock or bound uncertainty and wait it out.