Leader Election & Heartbeats

In many distributed systems, someone needs to be "in charge."

This role is called the Leader (or Master).


1. Leader Election

Leader election is the process of picking one node to be the coordinator among a group of identical nodes.

Why do we need it?

If all nodes try to coordinate at once, you get conflicts and data corruption.

How it works

  1. Nodes start an election.
  2. They use a Consensus Algorithm (like Raft) to agree on one leader.
  3. Once elected, the leader handles the coordination.

2. Heartbeats

How do we know if the leader is still alive?

Nodes send periodic signals called Heartbeats.

The Workflow


3. What if the Leader Fails?

When the heartbeat stops:

  1. The followers detect the failure.
  2. They trigger a new election.
  3. A new leader is chosen, and the cluster continues.
stateDiagram-v2
    [*] --> Follower
    Follower --> Candidate: Timeout (No Heartbeat)
    Candidate --> Leader: Receives Majority Votes
    Leader --> Follower: New Leader Elected / Steps Down
    Candidate --> Follower: Another Leader Elected

Split-Brain Problem (Scary!)

What if a network partition happens and the leader is separated from half the cluster?

Result: Massive data corruption.

The Solution: Quorum

A leader must always have a Majority (Quorum) of nodes supporting it.


Key Takeaways

In production, you rarely implement this yourself. You use tools like Etcd or ZooKeeper to handle the heavy lifting.