In distributed systems, physical clocks on independent nodes deviate due to thermal variations and crystal imperfections. Clock synchronization minimizes skew (instantaneous difference) by correcting for drift (deviation rate).

Clock Offset and Delay Estimation (NTP)

The Network Time Protocol (NTP) estimates the offset between a client (C) and a server (S) using four timestamps: -T_1: Client sends request. -T_2: Server receives request. -T_3: Server sends response. -T_4: Client receives response.

The Math

Round-trip delay (\delta):

\delta = (T_4 - T_1) - (T_3 - T_2)

Clock Offset (\theta):

\theta = \frac{(T_2 - T_1) + (T_3 - T_4)}{2}

This calculation assumes network symmetry (\text{delay}_{C \to S} \approx \text{delay}_{S \to C}). Asymmetry is the primary source of error in NTP.## NTP Stratum Hierarchy

Trust is organized into strata:

Correction Mechanisms

  1. Stepping (The Jump): For large offsets (>125\text{ms}), the clock is immediately set. This breaks monotonicity.
  2. Slewing (The Drift): For small offsets, the kernel subtly speeds up or slows down the clock (typically at$500\text$) to converge without jumps.

Precision Time Protocol (PTP / IEEE 1588)

PTP is required for sub-microsecond precision (e.g., HFT, 5G base stations). It differs from NTP by using hardware timestamping at the MAC/PHY layer, bypassing OS kernel jitter.

FeatureNTPPTP
Precision$1\text-\50\text|< 1\mu\text$
ImplementationSoftware (UDP)Hardware (NIC/MAC)
NetworkVariable (Internet)Low-jitter (LAN/PTP-aware switches)

Logical vs. Physical Time

Because physical clocks cannot be perfectly synced, distributed databases use logical time for ordering:

Operational Failure Modes

  1. Leap Second Smearing: Abruptly adding a second can crash applications. Modern providers (Google, AWS) "smear" the extra second over 24 hours.
  2. Virtualization Skew: Hypervisor context switching can cause massive, unpredictable clock jumps in VMs. Use the hypervisor's specific clock source (e.g., kvm-clock).
  3. Partitioned NTP: If a cluster loses access to external Stratum 1 sources, nodes will drift relative to each other. Use a local Stratum 1 source (GPS receiver) for isolated environments.

Implementation Checklist