TASK
Implementation
Implement follower reads with bounded staleness. Clients can opt to read from any follower if they accept data up to T seconds stale. This scales read throughput linearly with cluster size.
Request: {"type": "follower_read", "msg_id": 1, "key": "x", "max_staleness_ms": 5000}
Response: {"type": "follower_read_ok", "in_reply_to": 1, "value": "42", "actual_staleness_ms": 200, "served_by": "n2", "linearizable": false}
Request: {"type": "follower_read", "msg_id": 2, "key": "x", "max_staleness_ms": 0}
Response: {"type": "follower_read_ok", "in_reply_to": 2, "value": "42", "served_by": "n1", "linearizable": true, "reason": "redirected_to_leader"}Sample Test Cases
Follower read within staleness boundTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1","n2","n3"]}}
{"src":"c1","dest":"n1","body":{"type":"follower_read","msg_id":2,"key":"x","max_staleness_ms":5000}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Zero staleness redirects to leaderTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1","n2","n3"]}}
{"src":"c1","dest":"n1","body":{"type":"follower_read","msg_id":2,"key":"x","max_staleness_ms":0}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Hints
Hint 1▾
Clients opt in to reading from followers if they accept data up to T seconds stale
Hint 2▾
Followers track their applied index; reads are served if the data is fresh enough
Hint 3▾
This distributes read load across all replicas, not just the leader
Hint 4▾
The staleness bound is configurable per-request
Hint 5▾
Compare latency: follower reads avoid the leader bottleneck
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
follower readsbounded stalenessread scalabilityconsistency tradeoff
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
import sys
import json
def main():
# Your implementation here
for line in sys.stdin:
msg = json.loads(line)
print(json.dumps(msg), flush=True)
if __name__ == "__main__":
main()