← Lessons

quiz vs the machine

Platinum1820

Concurrency

Leader Election With Heartbeats

Choosing a single coordinator and detecting when it disappears.

6 min read · advanced · beat Platinum to climb

Leader Election With Heartbeats

Many systems need exactly one node to coordinate, the leader. Leader election chooses that node, and equally important, it detects when the leader fails so a new one takes over.

A common design uses heartbeats. The leader periodically sends a small message to every follower saying it is alive. Each follower runs an election timeout. If it hears a heartbeat before the timeout, it stays a follower. If the timeout elapses with no heartbeat, the follower suspects the leader is dead and starts an election, becoming a candidate and asking others to vote for it. A node that wins a majority of votes becomes the new leader and starts sending heartbeats.

Two ideas keep this correct. First, elections are scoped to a term, a monotonically increasing number, so a node only votes once per term and a stale leader from an old term is rejected once it sees a higher term. Second, randomized timeouts make it unlikely that many followers time out at the same instant and split the vote, which would force another round.

  • Heartbeats signal liveness and reset follower timeouts.
  • Terms prevent two leaders from both being accepted as current.
  • Random timeouts reduce split votes and speed up convergence.

This is the heart of Raft and similar consensus protocols.

Key idea

Leader election picks one coordinator using votes scoped to increasing terms, while heartbeats and randomized timeouts let followers detect a dead leader and elect a new one without two leaders coexisting.

Check yourself

Answer to earn rating on the learn ladder.

1. What triggers a follower to start an election?

2. Why do election terms matter?

3. What problem do randomized timeouts address?