ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/store/tasks/task-8-2-1-read-index
TASK

Implementation

Implement the "read index" optimization: the leader records the current commit index, confirms it still leads via heartbeats, then serves the read. Linearizable but no log write needed.

Request:  {"type": "read_index", "msg_id": 1, "key": "x"}
Response: {"type": "read_index_ok", "in_reply_to": 1, "value": "42", "read_at_index": 5, "heartbeat_confirmed": true, "linearizable": true}

Request:  {"type": "read_via_log", "msg_id": 2, "key": "x"}
Response: {"type": "read_via_log_ok", "in_reply_to": 2, "value": "42", "log_index_used": 6, "latency_ms": 15}

Request:  {"type": "compare_read_methods", "msg_id": 3, "num_reads": 100}
Response: {"type": "compare_read_methods_ok", "in_reply_to": 3, "read_index_avg_ms": 2, "read_via_log_avg_ms": 15, "both_linearizable": true}

Sample Test Cases

Read index returns correct valueTimeout: 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":"read_index","msg_id":2,"key":"x"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Compare read methods shows performance differenceTimeout: 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":"compare_read_methods","msg_id":2,"num_reads":50}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}

Hints

Hint 1
Leader records current commitIndex before serving the read
Hint 2
Leader confirms it still has majority support via a round of heartbeats
Hint 3
Only after majority responds does the leader serve the read at that commitIndex
Hint 4
This is linearizable but cheaper than writing the read to the log
Hint 5
Compare to the naive approach: put every read through the Raft log
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

read indexlinearizable readsheartbeat confirmationleader lease
main.py
python
Implement Read Index for Linearizable Reads - The Store | Build Distributed Systems