Clock Skew and Monotonic Clocks

An approach that avoids relying on volatile wall-clock time and instead measures elapsed durations using non-decreasing monotonic clocks. It prevents logic failures caused by time divergence across distributed nodes.

General Meaning

Computer systems maintain two distinct types of clocks: wall-clock time, which indicates the current calendar date and time of day, and monotonic clocks, which solely track elapsed time relative to an arbitrary starting point.

Wall clocks are subject to sudden backward leaps or forward jumps caused by NTP synchronization, manual time adjustments, or leap seconds. Furthermore, clock skew perpetually exists across physical machines in distributed environments. Relying on wall clocks for timeouts or event ordering triggers catastrophic miscalculations. In contrast, monotonic clocks guarantee strictly non-decreasing values, making them the standard foundation for tracking elapsed intervals.

Why It Matters

  • Preventing erratic timeout detection: A wall clock jumping backward causes timeout checks to hang indefinitely, while jumping forward causes premature false timeout triggers.
  • Preventing causality inversion across distributed nodes: If clocks drift between machines, a write on one host might appear to occur after a subsequent read on another, breaking causal event ordering.
  • Enforcing strict lease expiration boundaries: Mutual exclusion via timed leases requires predictable duration measurement so that clock drift does not cause split-brain concurrent execution.

How the Site Factory Uses It

In the Site Factory, three Mac nodes coordinate alongside Cloudflare Edge Workers under a defensive architecture that never places blind trust in machine wall clocks. When renewing service leases, execution scripts track elapsed duration using time.monotonic(), computing remaining validity by subtracting safety margins (SAFETY_SEC) from server timestamp deltas.

In addition, execution artifacts and receipts bearing future timestamps (violating started <= at <= now) are rejected immediately as malformed data. Job waiting logic in run_job.py also synchronizes against discrete task boundaries rather than wall-clock deadlines, ensuring cross-machine stability across ops/watchdog/service_lease.py, honbu/receipts.py, and dispatch/src/workflow.js.

Caveats & Operational Rules

  • Never measure elapsed duration using wall-clock deltas: Using datetime.now() - start can yield negative values if an NTP adjustment occurs mid-flight; always use time.monotonic() for intervals.
  • Never share absolute monotonic timestamps across machines: Monotonic values represent uptime ticks specific to a single operating system instance, making cross-machine comparisons invalid.
  • Do not extend leases based on response arrival time: When network latency delays a lease response, calculating expiration from receipt time risks overrunning the server's actual lease deadline; anchor deadlines to request dispatch time.

Related Terms

References

  • Book: Martin Kleppmann, Designing Data-Intensive Applications, Chapter 8 (The Trouble with Distributed Systems: Unreliable Clocks)

Verified: 2026-11-08