This article explores the fundamental difficulties of maintaining accurate time across machines in a distributed system, highlighting how clock drift and network latency can lead to incorrect event ordering. It discusses the limitations of physical clocks and NTP, advocating for the use of logical and monotonic clocks to ensure causal consistency and accurate duration measurements in critical applications like distributed databases and payment systems.
Read original on Dev.to #architectureIn distributed systems, each server possesses its own independent clock. Even with initial synchronization, these clocks inevitably drift due to hardware oscillator variations and environmental factors. This divergence, known as clock skew and clock offset, means that two events occurring simultaneously on different machines might be recorded with different timestamps, leading to inconsistencies and logical errors, especially when application logic relies on these timestamps for event ordering.
Key Timing Concepts
- Clock offset: The difference between a clock's reading and a reference clock at a particular moment. - Clock skew: The difference between two clocks' readings or rates. - Clock drift: The change in a clock's timing error over time.
Synchronizing clocks across a network is further complicated by unpredictable network delays. While protocols like NTP (Network Time Protocol) significantly improve synchronization by estimating offsets and delays, they cannot eliminate all sources of uncertainty. This means a perfectly global, consistent clock is practically unattainable, and applications must account for residual timing errors rather than relying on perfect synchronization.
To correctly order events and measure durations in distributed environments, system designers often turn to alternatives to wall-clock time:
The challenges of time synchronization are particularly critical in distributed databases, where timestamp-based conflict resolution (e.g., last-write-wins) can lead to data inconsistencies if clocks are out of sync. Many databases employ more sophisticated mechanisms like version metadata, transaction ordering, or hybrid logical clocks to ensure strong consistency. Developers must also be wary of using wall-clock time for duration measurements in application code, as OS clock adjustments can lead to incorrect or even negative elapsed times.