This article explores the fundamental challenges of time and event ordering in distributed systems where a shared global clock is absent. It delves into the limitations of physical clocks and the necessity of logical and vector clocks to establish causality and ensure correctness across independent machines. Understanding these concepts is critical for designing robust distributed systems that handle data replication, auditing, and debugging effectively.
Read original on ByteByteGoIn a single computer system, a central clock provides a clear, unambiguous order for all events. However, in a distributed system, this simplicity vanishes. Each machine has its own hardware clock, which drifts independently, making global synchronization extremely difficult, even with protocols like NTP. This clock skew can lead to significant problems when the correctness of operations depends on the precise order of events, such as data updates or log tracing.
Danger of Physical Clocks
Relying solely on physical timestamps for ordering events across different machines in a distributed system can lead to incorrect state, data loss, and difficult-to-debug issues. An event that *actually* happened later might receive an earlier timestamp due to clock skew, causing logical inconsistencies.
To address the limitations of physical clocks, distributed systems often employ logical clocks. The core idea is to establish a "happened-before" relationship, defining causality between events. If event A can causally affect event B, then A "happened before" B, regardless of physical clock timestamps. This relationship is crucial for maintaining data consistency and understanding the true sequence of operations.
Hybrid Logical Clocks aim to combine the best aspects of physical and logical clocks. They provide a timestamp that is monotonically increasing and closely tracks physical time, while also preserving the happened-before relationship like logical clocks. HLCs are particularly useful in globally distributed databases like Google Spanner, which aim to provide external consistency through synchronized physical clocks and commit protocols.