- Category: A. Data-Intensive Design
- English term: Clock Skew and Monotonic Clocks
- Related: Lease (Leased Lock) · Fencing Token and Generation Number · Heartbeat and Failure Detection · Receipt and Definition of Done · Compare-and-Swap (CAS) and Optimistic Concurrency Control
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() - startcan yield negative values if an NTP adjustment occurs mid-flight; always usetime.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
- Lease (Leased Lock)
- Fencing Token and Generation Number
- Heartbeat and Failure Detection
- Receipt and Definition of Done
- Compare-and-Swap (CAS) and Optimistic Concurrency Control
References
- Book: Martin Kleppmann, Designing Data-Intensive Applications, Chapter 8 (The Trouble with Distributed Systems: Unreliable Clocks)
Verified: 2026-11-08