Skip to the content.

Leader Election is a distributed systems concept.

👉 In a cluster of distributed servers (like database replicas, job schedulers, or coordination services), you often need one node to act as the “leader” to coordinate work, while others act as followers.

Why is leader election needed?

Tools commonly used

Example Scenarios

➡️ Trade-off: Leader election introduces some latency and complexity but is critical for high availability and fault tolerance in distributed systems.


Leader election can be done in different ways depending on the system. Two of the most common approaches are:


🔹 1. Zookeeper-based Leader Election

Zookeeper is a distributed coordination service. It doesn’t elect a leader automatically, but it provides primitives to build it.

How it works (using Ephemeral Sequential Znodes):

  1. All candidate servers (say S1, S2, S3, S4) connect to Zookeeper.
  2. Each one creates an ephemeral sequential znode under /election.

    • Example:

      /election/node_0001
      /election/node_0002
      /election/node_0003
      
  3. Zookeeper assigns numbers automatically (like tickets).
  4. The server with the lowest sequence number becomes the leader.
  5. If the leader crashes, its ephemeral znode is deleted automatically.
  6. The next lowest node becomes the new leader.

👉 This ensures only one leader at a time and automatic failover.


🔹 2. Consensus Protocol (Raft, Paxos, etc.)

This is used by etcd, Consul, Kubernetes, and modern databases.

How Raft Leader Election Works:

  1. Nodes start in Follower state.
  2. If a follower doesn’t hear from a leader within a timeout, it becomes a Candidate.
  3. The candidate requests votes from other nodes.
  4. If it gets majority votes, it becomes the Leader.
  5. The leader periodically sends heartbeats to followers to prevent new elections.
  6. If the leader fails, the process repeats.

👉 Advantage:


âś… Comparison

Aspect Zookeeper (Ephemeral Znodes) Raft/etcd
Simplicity Simpler, app handles leader logic Built-in consensus protocol
Consistency Needs careful design Strong consistency (via majority votes)
Recovery Ephemeral node deletion triggers new leader Automatic re-election
Use Cases Custom job schedulers, Hadoop, Kafka Kubernetes, databases, key-value stores

đź’ˇ Example in real systems


👉 In system design interviews, if you’re asked about leader election, you can mention: