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

Implementation

The HLC receive rule is more complex than Lamport's. On receiving a message with HLC (msg.pt, msg.lc):

  1. new_pt = max(local.pt, msg.pt, now_ms())
  2. If new_pt == local.pt == msg.pt: new_lc = max(local.lc, msg.lc) + 1
  3. Elif new_pt == local.pt: new_lc = local.lc + 1
  4. Elif new_pt == msg.pt: new_lc = msg.lc + 1
  5. Else (new physical time): new_lc = 0

Implement a hlc_receive handler:

Request:  {"type": "hlc_receive", "msg_id": 1, "remote_pt": 1000, "remote_lc": 5}
Response: {"type": "hlc_receive_ok", "in_reply_to": 1, "pt": 1000, "lc": 6}

Sample Test Cases

Receive from ahead remote advances localTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"n2","dest":"n1","body":{"type":"hlc_receive","msg_id":2,"remote_pt":9999999999999,"remote_lc":5}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "n2", "body": {"type": "hlc_receive_ok", "pt": 9999999999999, "lc": 6, "in_reply_to": 2, "msg_id": 1}}
Receive from behind remote uses localTimeout: 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":"n2","dest":"n1","body":{"type":"hlc_receive","msg_id":3,"remote_pt":0,"remote_lc":0}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}

Hints

Hint 1
On receive: take max of local pt, received pt, and current physical time
Hint 2
If the max pt equals local pt and received pt, increment lc from max of both lc values
Hint 3
If max pt equals only one side, take that sides lc and increment
Hint 4
If max pt is the new physical time, reset lc to 0
Hint 5
HLC must always advance - never go backward
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

HLC mergereceive ruleclock synchronizationcausal consistency
main.py
python
Implement HLC Receive Rule - The Identifier | Build Distributed Systems