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

Implementation

Implement heartbeats from the leader to followers. The leader periodically sends AppendEntries (empty for now) to maintain authority. Followers that do not receive heartbeats become candidates.

Sample Test Cases

Leader sends heartbeatTimeout: 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":"become_leader","msg_id":2,"term":1}}
{"src":"c0","dest":"n1","body":{"type":"wait","msg_id":3,"duration_ms":150}}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"n1","dest":"c0","body":{"type":"become_leader_ok","in_reply_to":2,"msg_id":1}}
{"src":"n1","dest":"n2","body":{"type":"append_entries","msg_id":2,"term":1,"leader_id":"n1","prev_log_index":0,"prev_log_term":0,"entries":[],"leader_commit":0}}
{"src":"n1","dest":"n3","body":{"type":"append_entries","msg_id":3,"term":1,"leader_id":"n1","prev_log_index":0,"prev_log_term":0,"entries":[],"leader_commit":0}}

Hints

Hint 1
Leader sends heartbeats periodically
Hint 2
Followers reset timeout on heartbeat
Hint 3
Missing heartbeats trigger election
OVERVIEW

Theoretical Hub

Heartbeats

Heartbeats serve dual purposes: they prevent followers from starting elections, and they carry log replication data (in full Raft). A leader that stops sending heartbeats will be replaced.

Key Concepts

heartbeatlivenessfailure detection
main.py
python
Add Heartbeat Mechanism - The Elector | Build Distributed Systems