การเคลื่อนตัวของนาฬิกาและการซิงโครไนซ์: ทำไมนาฬิกาถึงไม่ตรงกัน

What Is Clock Drift?

Clock drift is the tendency of a computer clock to gradually run faster or slower than true time. Every computer contains a quartz crystal oscillator that generates the timing signal. But quartz crystals are imperfect — they oscillate at slightly different rates depending on temperature, aging, manufacturing variations, and power supply fluctuations. Even a drift of just 1 ppm (part per million) results in an error of 86 milliseconds per day.

Typical Drift Rates

  • PC quartz oscillator: 10–100 ppm — loses/gains 1–9 seconds per day without correction
  • Server-grade TCXO: 0.1–1 ppm — much better, but still drifts without NTP
  • GPS-disciplined oscillator: <0.001 ppm — accurate to nanoseconds
  • Cesium atomic clock: <0.00001 ppm — accurate to nanoseconds over years

How NTP Corrects Drift

NTP doesn't just set the clock — it continuously measures the drift rate and adjusts the clock's frequency to correct it:

  • Step (jump): If the offset is more than 125ms (or 1000 seconds in panic mode), NTP makes an immediate correction.
  • Slew (gradual): For offsets less than 125ms, NTP adjusts the clock rate by up to 500 ppm, gradually steering the clock toward the correct time without a jump. A 100ms correction takes about 200 seconds.
  • Frequency discipline: NTP learns the local oscillator's drift rate over time and continuously compensates for it.

Monitoring Clock Drift

# chrony (modern Linux NTP client)
chronyc tracking
# Reference ID    : 8.8.4.4 (time.google.com)
# Stratum         : 2
# System time     : 0.000012340 seconds fast of NTP time
# Last offset     : +0.000002123 seconds
# RMS offset      : 0.000008456 seconds
# Frequency       : 23.451 ppm slow
# Residual freq   : +0.001 ppm
# Skew            : 0.123 ppm

# ntpq (traditional)
ntpq -p
# * = current sync source, offset in milliseconds

Virtual Machine Drift

VMs have a particularly severe drift problem. When the host is under load, the hypervisor may not give the VM CPU time when expected, causing the guest clock to lag badly. Modern hypervisors provide para-virtualized clock sources (kvm-clock) that sync guest time to host time directly. In VMs, always run NTP or systemd-timesyncd:

# Check if kvm-clock is active (Linux guest on KVM)
cat /sys/devices/system/clocksource/clocksource0/current_clocksource
# kvm-clock  ← good

# Enable timesyncd if disabled
sudo systemctl enable --now systemd-timesyncd

Container and Cloud Time

Docker containers share the host kernel and clock — they cannot have a different time than the host. Kubernetes pods are the same. Ensure the host OS is synchronized with NTP, and containers inherit accurate time automatically.