ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/timekeeper/tasks/task-4-1-2-monotonic-clock
TASK

Implementation

A MonotonicClock wraps the system clock and guarantees the returned value never decreases. Implement this wrapper and track what information is lost.

Request:  {"type": "mono_read", "msg_id": 1}
Response: {"type": "mono_read_ok", "in_reply_to": 1, "time_ms": 1234567, "corrections": 0}

Sample Test Cases

Mono read returns timeTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"mono_read","msg_id":2}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Two reads are non-decreasingTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"mono_read","msg_id":2}}
{"src":"c1","dest":"n1","body":{"type":"mono_read","msg_id":3}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}

Hints

Hint 1
Wrap the system clock to always return >= previous value
Hint 2
Use max(current, last_returned) as the strategy
Hint 3
Track how many times the wrapper prevented a backward jump
Hint 4
Information lost: you cannot detect when time actually went backward
Hint 5
time.monotonic() in Python provides this natively
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

monotonic clockclock wrapperinformation lossordering guarantee
main.py
python
Implement Monotonic Clock Wrapper - The Timekeeper | Build Distributed Systems