ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/timekeeper/tasks/task-4-1-5-wait-out-uncertainty
TASK

Implementation

Spanner achieves external consistency by waiting out the uncertainty window: before committing a transaction at timestamp T, wait until TrueTime.now().earliest > T. This guarantees causally-later transactions see this commit.

Implement a commit handler with wait-out-uncertainty:

Request:  {"type": "commit", "msg_id": 1, "key": "x", "value": "v1"}
Response: {"type": "commit_ok", "in_reply_to": 1, "commit_ts": 1234567, "wait_ms": 7}

Request:  {"type": "commit_stats", "msg_id": 2}
Response: {"type": "commit_stats_ok", "in_reply_to": 2, "total_commits": 5, "total_wait_ms": 35, "avg_wait_ms": 7}

Sample Test Cases

Commit stores valueTimeout: 10000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"commit","msg_id":2,"key":"x","value":"v1"}}
{"src":"c1","dest":"n1","body":{"type":"kv_read","msg_id":3,"key":"x"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Commit stats with no commitsTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"commit_stats","msg_id":2}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "commit_stats_ok", "total_commits": 0, "total_wait_ms": 0, "avg_wait_ms": 0, "in_reply_to": 2, "msg_id": 1}}

Hints

Hint 1
Before committing, wait until TrueTime.now().earliest > commit_timestamp
Hint 2
This guarantees that any future read will see this commit
Hint 3
The wait duration equals the uncertainty window
Hint 4
This is how Spanner achieves external consistency without locks
Hint 5
Track total wait time for performance analysis
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

external consistencycommit waitSpannerlinearizability
main.py
python
Wait-Out-Uncertainty for External Consistency - The Timekeeper | Build Distributed Systems