Physical Clocks and Clock Synchronization
Clock Synchronization in Distributed Systems
In distributed systems, multiple processes on different computers need to coordinate their actions, requiring a common understanding of time. However, each computer has its own physical clock—a hardware device that counts crystal oscillations. These clocks drift over time, leading to discrepancies called clock skew.
Cristian's Algorithm
Cristian's algorithm is a simple synchronization method where a client synchronizes with a time server. The client sends a request, receives the server's time (T_server), and measures the round-trip time (T_round). Assuming symmetric network delay, the client sets its clock to T_server + T_round / 2.
Network Time Protocol (NTP)
NTP is a sophisticated protocol using a hierarchical system of time servers. It employs complex algorithms to estimate clock offset and round-trip delay, plus statistical methods to filter noise and select reliable sources, achieving accuracy within milliseconds. NTP typically uses UDP port 123.
NTP Hierarchical Structure (Strata):
- Stratum 0: High-precision reference clocks (atomic clocks, GPS receivers)
- Stratum 1: Primary servers directly connected to Stratum 0 devices
- Stratum 2 and lower: Secondary servers synchronized with higher strata
Each stratum level represents an additional hop from the reference clock, with slight increases in potential inaccuracy. This hierarchy identifies source reliability and prevents time loops.
Reference Clock Types:
- Atomic Clocks: Use atomic resonant frequency (e.g., cesium clocks count 9,192,631,770 cycles/second) for extreme accuracy
- GPS Clocks: Satellite-based atomic clocks providing globally accessible time signals
- Radio Clocks: Receive signals from dedicated radio time stations
NTP Synchronization Mechanism
NTP uses four timestamps in each client-server exchange to calculate clock offset and round-trip delay:
- : Client sends request
- : Server receives request
- : Server sends response
- : Client receives response
Clock Offset (θ): Difference between client and server clocks
Round-Trip Delay (δ): Total time for request-response cycle
Advanced Features:
- Intersection Algorithm: Selects optimal time sources by discarding outliers and identifying the largest subset of servers with consistent readings
- Multiple Measurements: Uses statistical analysis over time rather than single measurements for improved accuracy
- Dynamic Intervals: Adjusts synchronization frequency based on network stability
Applications of NTP
- Distributed Systems: Coordinates actions, ensures data consistency, and prevents conflicts
- Logging and Monitoring: Provides accurate timestamps for event sequencing and issue identification
- Financial Transactions: Ensures correct transaction ordering and regulatory compliance
- Telecommunications: Manages data flow and prevents collisions
- Scientific Research: Enables precise time-sensitive measurements
Implementation Challenges
- Asymmetric Network Paths: Different routes for request/response can cause inaccurate delay calculations
- Security Concerns: Vulnerable to spoofing and man-in-the-middle attacks; mitigated by secure implementations like NTPsec using encryption and authentication
Alternative Synchronization Methods
- Radio Clocks: Use radio signals for distributed clock synchronization
- Global Positioning System (GPS): Leverages satellite system for accurate real-time synchronization
- Precision Time Protocol (PTP): Achieves higher accuracy than NTP for applications requiring extreme precision