ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/identifier/tasks/task-2-4-3-clock-backward
TASK

Implementation

NTP can adjust the system clock backward. Physical timestamps break. Lamport clocks keep working but lose time correlation. HLC handles it gracefully by keeping its physical component and incrementing the logical counter.

Implement a simulation that demonstrates this:

  1. simulate_clock_backward sets the HLC's internal notion of "now" backward by a given amount
  2. After the backward jump, HLC.tick() should still produce advancing timestamps
  3. Report the drift between HLC.pt and actual physical time
Request:  {"type": "simulate_backward", "msg_id": 1, "offset_ms": -5000}
Response: {"type": "simulate_backward_ok", "in_reply_to": 1}

Then tick and observe HLC advances despite backward clock:

Request:  {"type": "hlc_tick", "msg_id": 2}
Response: {"type": "hlc_tick_ok", "in_reply_to": 2, "pt": 1234567, "lc": 1, "drift_ms": 5000}

The drift_ms field shows how far HLC.pt is ahead of the (simulated) physical clock.

Sample Test Cases

Simulate backward responds okTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"simulate_backward","msg_id":2,"offset_ms":-5000}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "simulate_backward_ok", "in_reply_to": 2, "msg_id": 1}}
Tick after backward still advancesTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"hlc_tick","msg_id":2}}
{"src":"c1","dest":"n1","body":{"type":"simulate_backward","msg_id":3,"offset_ms":-10000}}
{"src":"c1","dest":"n1","body":{"type":"hlc_tick","msg_id":4}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}

Hints

Hint 1
When system clock goes backward, HLC keeps its pt and increments lc
Hint 2
This means HLC never goes backward, preserving ordering
Hint 3
Lamport clocks also handle this, but lose physical time correlation
Hint 4
Physical clocks break entirely on backward adjustments
Hint 5
Track the drift between HLC.pt and actual physical time
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

NTP adjustmentclock backwardmonotonic guaranteeHLC resilience
main.py
python
HLC Handles Backward Clock Gracefully - The Identifier | Build Distributed Systems