AlgoViz

Elapsed time, clocks and causal order

hard 7 min

Wall clocks place events on a civil timeline and may be corrected.

Video lesson · Chapter 1 of 4

A request with a negative duration

0:00 / 2:30

Theory

Read or listen first. About 2 min.

What it is

Wall clocks place events on a civil timeline and may be corrected. A monotonic clock is suitable for measuring elapsed time within its documented scope. Values from independent machines do not share a monotonic origin. Logical clocks encode ordering constraints between events and messages, while physical timestamps carry uncertainty.

Why it exists

A monitoring release subtracts wall timestamps before and after each request. A clock correction produces a negative value and damages percentile metrics. Record elapsed durations on one host’s monotonic clock and keep separate wall timestamps for correlation. Across services, use trace relationships and explicit uncertainty before inferring which event happened first.

If two hosts contributed the start and end values, a negative duration can be cross-host skew even when neither clock stepped locally.

A request with a negative duration

Service timer → Wall clock (record civil time). Service timer → Monotonic clock (measure duration)
Service timer → Wall clock (record civil time). Service timer → Monotonic clock (measure duration)

Inside this chapter

Physical Clocks vs. Logical Clocks
Physical clocks estimate real time within error. Logical clocks track ordering constraints imposed by local actions and messages. Neither alone reveals every causal relationship between independent events.
Wall-clock time vs. Monotonic time
Wall clocks can step or slew as they are corrected. Use a monotonic source for elapsed time within its scope, and keep wall time separately for human timelines.
Network Time Protocol (NTP) skew
NTP estimates clock offset and delay. Network asymmetry and clock discipline limit accuracy. Record synchronization quality before assigning cross-host order to closely spaced timestamps.
Leap Seconds handling
Time services may step, repeat or smear around a leap second depending on policy. Mixed policies can disagree temporarily, so duration and deadline code needs defined clock semantics.
Google Spanner's TrueTime concept
TrueTime returns an interval reflecting clock uncertainty. Spanner uses that bound with a commit-wait protocol to support externally consistent transactions; ordinary clock synchronization alone is insufficient.
Lamport Timestamps
Lamport clocks ensure a causally earlier event has a lower timestamp. A lower timestamp does not establish causality in the reverse direction.
Vector Clocks
Vector clocks encode per-participant progress. Incomparable vectors can represent concurrent versions. Metadata may grow with the number of participants represented.
Hybrid Logical Clocks (HLC)
A hybrid logical clock combines physical time and a logical counter to preserve causal ordering properties while staying near wall time. Physical uncertainty does not disappear.

How it works

Step through the idea, or press Play.

Step 1 of 4

Record local order. Within a service, sequence numbers or monotonic durations preserve useful local information.

Understand it

Worked incident

A request with a negative duration

ObservationReported request duration (ms)Interpretation
Baseline80Wall and monotonic durations agree for a short request.
Incident-420The wall clock steps backward during a request.
After intervention82Record elapsed time with a monotonic source. Keep civil time for human investigation.
Illustrative measurements from the same workload at three checkpoints.

Choose the next investigation

If two hosts contributed the start and end values, a negative duration can be cross-host skew even when neither clock stepped locally.

Summary

The whole topic in one minute.

Independent events can have either wall order. Clock error and the absence of a causal message both limit that inference.

If two hosts contributed the start and end values, a negative duration can be cross-host skew even when neither clock stepped locally.

Sources