ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/elector/tasks/task-5-3-election-timeout
TASK

Implementation

Add randomized election timeouts. When a follower does not hear from a leader within its timeout, it becomes a candidate. Randomization helps prevent multiple nodes from starting elections simultaneously.

Sample Test Cases

Random timeout in range 150-300msTimeout: 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_election_timeout","msg_id":2}}
{"src":"c0","dest":"n1","body":{"type":"get_election_timeout","msg_id":3}}
{"src":"c0","dest":"n1","body":{"type":"get_election_timeout","msg_id":4}}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"n1","dest":"c0","body":{"type":"election_timeout_reply","in_reply_to":2,"msg_id":1}}
{"src":"n1","dest":"c0","body":{"type":"election_timeout_reply","in_reply_to":3,"msg_id":2}}
{"src":"n1","dest":"c0","body":{"type":"election_timeout_reply","in_reply_to":4,"msg_id":3}}

Hints

Hint 1
Use random timeout between 150-300ms
Hint 2
Reset timeout on heartbeat
Hint 3
Different timeouts reduce split votes
OVERVIEW

Theoretical Hub

Randomized Timeouts

If all nodes used the same timeout, network hiccups could cause multiple simultaneous elections, splitting votes and delaying leader selection. Random timeouts spread out elections, usually letting one node win quickly.

Key Concepts

randomizationtimeoutsplit brain prevention
main.py
python
Implement Randomized Election Timeout - The Elector | Build Distributed Systems