Cristian's Algorithm & Precision Engineering
@magmacomputing/tempo-plugin-ntp implements Flaviu Cristian's probabilistic clock synchronization algorithm over HTTP(S) with modern performance enhancements: high-resolution monotonic baselines, sub-millisecond Server-Timing headers, statistical jitter rejection, and Exponential Moving Average (EMA) smoothing.
1. Cristian's Algorithm Over HTTP
When a client queries a time endpoint over HTTP:
: Client records its local timestamp immediately before sending the HTTP request. : Server records the current atomic timestamp when handling the request. : Client records its local timestamp immediately upon receiving the response.
Client Server
| |
|--- HTTP Request (T0) ----------->|
| | (T_server)
|<-- HTTP Response (T1) -----------|
| |Round-Trip Time (RTT)
Estimated Server Timestamp at Reception ( )
Assuming symmetrical network latency (outbound delay
Clock Drift Offset ( )
Uncertainty Window ( )
The maximum theoretical clock error bounds are:
2. Sub-Millisecond Precision via Server-Timing
Standard HTTP Date headers have a coarse 1-second resolution (Date: Fri, 02 Oct 2026 06:15:00 GMT), which introduces up to
tempo-plugin-ntp automatically prioritizes sub-millisecond timestamps delivered via the standard HTTP Server-Timing header:
HTTP/1.1 200 OK
Date: Fri, 02 Oct 2026 06:15:00 GMT
Server-Timing: clock=1727856900123.45
Content-Type: application/jsonIf Server-Timing contains a clock=..., server_time=..., or epoch=... metric, the plugin parses the high-precision floating-point epoch timestamp, achieving sub-millisecond synchronization accuracy.
3. Statistical Jitter Rejection & Filtering
Transient network spikes (e.g., cell tower handoffs, packet retries) can cause anomalous RTT measurements. If an asynchronous network delay is heavily skewed in one direction, the symmetrical assumption (
The plugin protects against skew distortion using RTT Threshold Filtering:
- Samples exceeding the configurable
maxAcceptableRttMsthreshold (default:1000ms) are immediately discarded. - High-latency or asymmetrical network spikes are rejected without updating the active drift calibration.
4. Exponential Moving Average (EMA) Smoothing
Rather than jumping the clock offset instantaneously upon every sync sample (which could cause time to appear to skip or stutter), new samples are smoothed into the running offset using an Exponential Moving Average:
Where:
is the smoothing factor (default: 0.3).- On the very first sample (
), to establish immediate baseline calibration.
5. Monotonic Time Progression with Zero O(N) Overhead
Once calibrated, calling Tempo.ntp.now() is a purely synchronous,
Because the offset Tempo.ntp.now() millions of times per second inside render loops or high-frequency calculation pipelines with zero performance penalty.