Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Clock synchronisation

Practical notes on synchronising physical clocks in distributed systems: definitions (offset, drift, skew), Cristian and Berkeley algorithms, NTP timestamp math, error bounds and how drift limits synchronisation intervals.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Clock synchronisation is used when different machines in a distributed system must agree on physical time: for ordering events with real-time stamps, for log correlation, for security protocols (certificates, TLS), for distributed databases and caches, and for scheduled tasks. Engineers use these algorithms to keep servers’ clocks close enough to a common reference so distributed algorithms that depend on real time behave correctly.

Key ideas

Start with clear definitions (each defined term is bolded):

  • Clock (physical clock): a hardware or software device that produces a numerical reading intended to track real (wall) time. Readings are in seconds (SI unit) or derivatives.
  • True time: an ideal reference time (UTC). Real systems do not have direct access to true time; they estimate it.
  • Offset: the difference between a node’s clock reading and the true time (or the server’s time if using that as reference). If node time = C(t) and true time = T(t) then offset = C(t) − T(t).
  • Skew: difference in rate between two clocks (how fast one runs relative to another). If clock A gains 1 ms per second relative to B, skew is +1e−3 s/s for that interval.
  • Drift rate (ρ): maximum deviation of a clock’s running rate from the ideal rate 1. It is dimensionless (seconds per second). If a clock runs at rate 1 + ρ, ρ is the drift.
  • Precision: the maximum difference between any two clocks in a set (often used as the synchronization goal). If every pair of clocks differs by ≤ ε, we say precision ε is achieved.
  • Accuracy: the maximum difference between a clock and the true time (reference). Accuracy needs a trustworthy external time source.

Problem statement (formal): Given a set of machines each with a local physical clock C_i(t), design a protocol so that for all i, j and for all real times t, |C_i(t) − C_j(t)| ≤ ε (precision bound) after synchronization, and maintain this bound over time by resynchronising as needed. Assumptions depend on algorithm: e.g., bounded message delay, symmetric delays, or clock drift bounded by ρ.

Derivations and key theorems commonly asked in exams:

  1. Round-trip timestamp method (NTP-style) derivation. Draw two vertical timelines: left labelled Client, right labelled Server. Draw an arrow from Client at T1 to Server at T2 (label both times). Draw arrow back from Server at T3 to Client at T4. These are the four recorded timestamps. Assume network one-way delays are d1 (client→server) and d2 (server→client), and server clock is offset θ relative to client (server_time = client_time + θ). Then:

    • When client sends at its time T1, server receives at server time T2 = T1 + d1 + θ.
    • Server sends at server time T3, client receives at client time T4 = T3 + d2 − θ (solve carefully with sign convention; we will derive standard formula below). From these four equations you can eliminate d1 and d2 to derive the clock offset and round-trip delay.
  2. Offset and delay formulas (standard NTP result). Under the above definitions (timestamps T1..T4 recorded by client/server), the clock offset θ and round-trip delay δ satisfy:

    • θ = ((T2 − T1) + (T3 − T4)) / 2
    • δ = (T4 − T1) − (T3 − T2) These formulas assume the server timestamps are correct relative to its own clock and that timestamps are recorded when messages are sent/received. Derivation is standard and normally required in exams; ensure you can reproduce it step-by-step.
  3. Error bound with unknown asymmetric delays. If one-way delays are only known to lie in [d_min, d_max], the uncertainty in offset is at least (d_max − d_min)/2. This is important: network delay variability limits how well you can know offset.

  4. Drift accumulation. If a clock has maximum drift rate ρ, then after time Δt since last correct synchronization, the clock can be off by at most ρ·Δt. This gives a rule for how often to resynchronise: choose Δt so that ρ·Δt ≤ allowed_error.

  5. Algorithm summaries (what to draw and label in answers):

    • Cristian’s algorithm: client asks a time server for current time; server replies with its time Ts when it sent the reply. Client measures round-trip time RTT and (assuming symmetric one-way delays) sets local clock to Ts + RTT/2, and in practice uses the minimum RTT from several samples to reduce asymmetry error. Diagram: client→server request, server reply with Ts, annotate RTT as time difference between client send and client receive.
    • Berkeley algorithm: a master polls other nodes for their times, computes an average (optionally excluding outliers), and sends each node an offset to add or subtract. It does not rely on an external accurate clock; it agrees on a common internal time.
    • NTP (Network Time Protocol): uses repeated exchange of timestamps, keeps statistics, uses filtering and clock discipline algorithms; NTP uses the 4-timestamp exchange and estimates offset and delay like above, then applies smoothing and clock rate correction (not detailed here but mention exists).
  • PTP (IEEE 1588 Precision Time Protocol): achieves sub-microsecond accuracy on LANs with hardware timestamping; uses master–slave synchronization with Sync/Follow_Up and Delay_Request/Delay_Response messages; common in datacenters, telecom, and industrial control.

When to use which: use Cristian or NTP when a trusted external time server (or hierarchy of servers) is available. Use Berkeley when a set of machines must agree on a common time but no external reliable reference exists.

Formulas

θ = ((T2 − T1) + (T3 − T4)) / 2 where θ = estimated offset (seconds); T1 = client send time (s), T2 = server receive time (s), T3 = server send time (s), T4 = client receive time (s). Applies to the 4-timestamp exchange when clocks record send/receive instants.

δ = (T4 − T1) − (T3 − T2) where δ = round-trip delay (seconds); same symbol meanings as above. Applies to the same 4-timestamp model.

error_due_to_asymmetry ≤ (d_max − d_min) / 2 where d_min and d_max are lower and upper bounds on one-way delay (seconds). This gives the best possible bound on offset when only delay bounds are known.

max_clock_error_after_Δt ≤ ρ · Δt where ρ = maximum drift rate (dimensionless s/s), Δt = time since last sync (seconds). Use to choose synchronization interval. For maintaining precision between any two clocks, use max_pairwise_clock_separation_after_Δt ≤ 2ρ · Δt (both clocks may drift in opposite directions), so choose Δt ≤ ε/(2ρ) for target precision ε.

client_set_time (Cristian) ≈ Ts + RTT/2 where Ts = server time included in reply (s), RTT = measured round-trip time (s). Applies when assuming symmetric network delay.

Worked examples

Example 1 — NTP 4-timestamp numerical (university-exam style): Given (all times in seconds): T1 = 1000.000 (client send), T2 = 1000.250 (server receive), T3 = 1000.260 (server send), T4 = 1000.520 (client receive). Step 1: compute offset formula: θ = ((T2 − T1) + (T3 − T4)) / 2 = ((1000.250 − 1000.000) + (1000.260 − 1000.520)) / 2 = (0.250 + (−0.260)) / 2 = (−0.010) / 2 = −0.005 s. Step 2: compute round-trip delay δ = (T4 − T1) − (T3 − T2) = (1000.520 − 1000.000) − (1000.260 − 1000.250) = 0.520 − 0.010 = 0.510 s. Interpretation: estimated server clock = client clock + θ, so server is 5 ms behind client. Best client correction is to add θ (i.e., subtract 0.005 s). Round-trip delay is 510 ms. Final answers: offset = −0.005 s (server is 5 ms behind client), RTT = 0.510 s.

Example 2 — drift and resynchronisation interval: A machine’s crystal has maximum drift rate ρ = 1×10−6 (one microsecond per second). Allowed accuracy (max error relative to the reference) is 5 ms. How often must it sync? Use max_clock_error = ρ·Δt ≤ 0.005 s. Δt ≤ 0.005 / (1×10−6) = 5000 s ≈ 83.33 minutes. So resynchronise at least every 83.3 minutes.

Example 3 — Berkeley algorithm trace (small cluster): Machines A (master), B, C report times at poll instant (their local clocks): A=10:00:05, B=10:00:02, C=10:00:11. Master converts to seconds relative to 10:00:00: A=5 s, B=2 s, C=11 s. Compute average (simple): (5 + 2 + 11) / 3 = 6 s. Compute offsets to send: A: +1 s, B: +4 s, C: −5 s. Master sends these offsets; each node applies the offset gradually or immediately according to policy. Final agreed time will be 10:00:06. Show in exam: list steps, exclude outliers if policy says so, explain master is authoritative for the round.

Common mistakes

  • Mixing sign conventions in the NTP formulas. Always derive using consistent definitions of offset (server_time = client_time + θ) and check signs.
  • Forgetting the assumption of symmetric or bounded delays when quoting tight bounds on accuracy.
  • Not accounting for drift between synchronisations — computing a perfect offset once but not scheduling resynchronisation.
  • Using average in Berkeley without excluding outliers (faulty clocks) — this reduces robustness.

For your exam

Question types you will see:

  • Definitions and short notes: define offset, skew, drift, precision vs accuracy.
  • Derivation: derive the NTP offset and delay formulas starting from one-way delays; draw the two-line timestamp diagram with arrows and label T1..T4.
  • Numerical: given four timestamps compute offset and RTT; given drift rate compute sync interval.
  • Algorithm trace: step through Cristian’s or Berkeley algorithm on example delays/times and show resulting clock settings.
  • Comparison: explain when to use Cristian vs Berkeley vs NTP and list assumptions.

What a full-marks answer includes:

  • Clear definitions for terms used.
  • Correct diagram with labelled T1..T4 and d1/d2 if needed.
  • Step-by-step algebra derivation of θ and δ, with clear sign convention.
  • Numerical answers with units and reasoning about error bounds and resynchronisation interval.
  • Mention of assumptions (symmetric delay or known bounds, bounded drift ρ) and limitations.

Quick check

  1. What is the difference between precision and accuracy in clock synchronisation?
  2. In the 4-timestamp exchange, if T1=0, T2=2, T3=2, T4=4, what are offset and RTT?
  3. If drift rate ρ = 2×10−6 and you want max error ≤ 10 ms, how often must you sync? Answers: 1) Precision = max difference between any two clocks; accuracy = difference from true time/reference. 2) θ = ((2−0) + (2−4))/2 = (2 −2)/2 = 0 s; δ = (4−0) − (2−2) = 4 s. So offset 0 s, RTT 4 s. 3) Δt ≤ 0.01 / (2×10−6) = 5000 s ≈ 83.33 minutes.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?