ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/elector/tasks/task-5-1-node-states
TASK

Implementation

Implement the three states from Raft: Leader, Follower, and Candidate. Each node starts as a Follower. Candidates request votes. Leaders coordinate the cluster.

Sample Test Cases

Node starts as followerTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1","n2","n3"]}}
{"src":"c0","dest":"n1","body":{"type":"get_state","msg_id":2}}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"n1","dest":"c0","body":{"type":"state_reply","in_reply_to":2,"msg_id":1,"state":"follower","term":0}}

Hints

Hint 1
Define an enum for states
Hint 2
All nodes start as followers
Hint 3
State transitions happen on specific events
OVERVIEW

Theoretical Hub

Raft Roles

In Raft, every node is in one of three states: Follower (passive, responds to leaders), Candidate (seeking to become leader), or Leader (handles all client requests). This clear state machine simplifies reasoning about the protocol.

Key Concepts

state machineleader electionRaft roles
main.py
python
Implement Node States (Leader, Follower, Candidate) - The Elector | Build Distributed Systems