What Is Leader Election?

By Oleksandr Andrushchenko — Published on
0 Likes
0 Dislikes
What Is Leader Election?
What Is Leader Election?

Leader election is a coordination mechanism used in distributed systems to select one node as the leader responsible for performing specific tasks or making certain decisions on behalf of a group.

Instead of allowing every node to perform the same coordination-sensitive operation, the system elects one active leader while the remaining nodes act as followers or standbys. If the leader fails, the system detects the failure and elects another node.

Table of Contents

Why Leader Election Exists

Distributed systems commonly run multiple instances for availability and scalability. Some operations, however, should be performed by only one instance at a time.

Consider three scheduler instances:

Scheduler A
Scheduler B
Scheduler C

If every scheduler independently runs the same daily billing job:

Scheduler A → Run Billing
Scheduler B → Run Billing
Scheduler C → Run Billing

the system can create duplicate work.

One solution is to elect a leader:

Scheduler A → LEADER → Run Billing
Scheduler B → Follower
Scheduler C → Follower

The followers remain available but do not execute leader-only operations.

If Scheduler A fails:

Scheduler A → Failed

Scheduler B → LEADER
Scheduler C → Follower

The system can continue operating without permanently assigning leadership to one machine.

This pattern appears in databases, distributed schedulers, message brokers, cluster controllers, storage systems, and other systems that need a single coordination point.

How Leader Election Works

The exact election algorithm depends on the distributed system, but the lifecycle generally contains three stages:

Choose Candidate
      ↓
Elect Leader
      ↓
Maintain Leadership
      ↓
Leader Fails
      ↓
Elect New Leader

The difficult part is ensuring that nodes agree on which leader is authoritative even when messages are delayed, machines crash, or parts of the network cannot communicate.

Leader Election Algorithms
Leader Election Algorithms

Candidate Selection

Nodes eligible for leadership participate in an election.

Node A → Candidate
Node B → Candidate
Node C → Candidate

The election protocol needs a deterministic way to determine which candidate becomes leader.

Depending on the system, this can involve votes, priorities, leases, sequence numbers, consensus protocols, or an external coordination service.

Leadership

After election, one node performs leader-specific responsibilities.

Node A → Leader
Node B → Follower
Node C → Follower

The leader might:

  • accept writes;
  • schedule jobs;
  • assign partitions;
  • coordinate replicas;
  • manage cluster metadata;
  • perform maintenance operations;
  • serialize conflicting decisions.

Followers can continue serving other operations depending on the architecture.

Leader Failure

If followers determine that the leader is no longer available, a new election begins.

Node A → Leader → Failure

Node B ─┐
        ├→ Election → Node C becomes Leader
Node C ─┘

During this transition, leader-dependent operations may temporarily pause.

The time required to detect the failure and elect a replacement contributes directly to the system's recovery time.

Leader vs Follower

Leader-based architectures divide responsibilities between one authoritative node and other participating nodes.

Leader Follower
Coordinates specific operations Observes or follows leader state
May accept authoritative writes May replicate those writes
Makes leader-only decisions Can become a future candidate
Maintains leadership status Monitors leader availability

Leader election does not necessarily mean followers are idle.

In a replicated database, followers may continuously replicate data and sometimes serve reads. In a scheduler cluster, followers may process ordinary tasks while only the leader performs global scheduling decisions.

The leader role should contain only responsibilities that genuinely require centralized coordination. Sending unnecessary work through the leader can create a bottleneck.

What Makes Leader Election Difficult

Leader election would be straightforward if failures were perfectly observable.

In a distributed system, a silent node does not necessarily mean a failed node.

Suppose Node A stops responding to Node B:

Node A     X     Node B

Several things could have happened:

  • Node A crashed;
  • Node A is overloaded;
  • Node A paused temporarily;
  • Node B has a network problem;
  • the network between A and B is partitioned;
  • messages are simply delayed.

Node B cannot immediately distinguish all of these conditions.

This creates the central problem of leader election: how can the system replace an unavailable leader without accidentally creating two valid leaders?

This is part of the broader coordination problem described in Leader Election and Distributed Coordination.

Failure Detection and Heartbeats

Leader-based systems often use heartbeats to indicate that the leader is still active.

Leader → heartbeat → Followers
Leader → heartbeat → Followers
Leader → heartbeat → Followers

If followers stop receiving heartbeats for long enough, they suspect that the leader has failed.

Last heartbeat
      ↓
Wait timeout
      ↓
Leader suspected unavailable
      ↓
Start election

The timeout creates a trade-off.

A short timeout detects real failures quickly, but normal network delays or temporary pauses can trigger unnecessary elections.

A long timeout reduces false failure detection but increases recovery time after a real failure.

Heartbeats therefore provide failure suspicion, not perfect knowledge that another machine has stopped executing.

This distinction matters because the old leader may still be alive even after other nodes elect a replacement.

Terms, Epochs, and Generations

Distributed systems commonly assign a monotonically increasing number to each leadership period.

The terminology varies:

Term
Epoch
Generation
View

Conceptually:

Term 41 → Node A is leader
Term 42 → Node C is leader
Term 43 → Node B is leader

A higher term represents newer leadership.

Suppose Node A was leader during term 41 but became disconnected from the cluster. The remaining nodes elect Node C in term 42.

Node A → Leader, Term 41

Network partition

Node C → Leader, Term 42

If Node A later communicates with nodes that know about term 42, its term is stale.

41 < 42

Node A must stop acting as leader

Terms give the protocol an ordering for leadership changes and help distinguish current authority from stale authority.

Quorum-Based Election

Many distributed systems require a candidate to receive support from a quorum before becoming leader.

A common quorum is a majority of participating voting nodes.

For a five-node cluster:

Nodes = 5
Majority = 3

A candidate therefore needs at least three votes.

Node A → votes for C
Node B → votes for C
Node C → votes for C

Node C → Leader

The important property is that two different majorities cannot be completely disjoint.

For example, in a five-node cluster:

Majority 1: A B C
Majority 2: C D E

The two groups overlap.

Consensus protocols use quorum intersection together with additional protocol rules to preserve safety across elections and replicated state changes.

A majority alone is not a complete leader-election algorithm. Voting rules, terms, state freshness, persistence, and message ordering still matter.

Split Brain

Split brain occurs when different parts of a distributed system simultaneously behave as if they have authoritative leadership.

Consider a network partition:

Node A    Node B
Leader      |
   X        |
Node C    Node D
          Node E

If both sides independently continue making authoritative decisions, their state can diverge.

For example:

Old Leader A → assigns job to Worker 1
New Leader D → assigns same job to Worker 2

or in a database:

Leader A → accepts Write X
Leader D → accepts Write Y

Resolving the two histories later can be difficult or impossible without application-specific conflict resolution.

Leader-election protocols therefore need mechanisms that prevent isolated minorities or stale leaders from continuing to make authoritative decisions.

The trade-offs created by network partitions are closely related to the concepts discussed in CAP Theorem: Practical Trade-Offs and Real-World Examples.

Fencing Stale Leaders

Electing a new leader does not physically stop the previous leader from executing.

Suppose Node A pauses long enough for the cluster to replace it:

Term 17
Node A → Leader
         ↓
       Pause

Term 18
Node B → New Leader

Node A → Resumes

If Node A can still modify an external resource, two leaders may perform conflicting operations.

Fencing prevents stale leaders from successfully changing protected resources.

One approach uses the leadership term as a fencing token:

Node A → term 17
Node B → term 18

The downstream resource remembers the highest accepted term.

Highest accepted term = 18

Node A sends term 17
17 < 18

Reject operation

The old leader can continue running, but it can no longer successfully perform authoritative operations.

This is the same fundamental stale-owner problem that appears with distributed leases and locks.

Leader Election vs Distributed Locking

Leader election and distributed locking are closely related, but they usually model different scopes of ownership.

Leader Election Distributed Lock
Selects an active coordinator Protects a specific resource or operation
Leadership may last for minutes, hours, or longer Ownership often lasts for one critical section
Followers monitor and replace the leader Clients compete for individual locks
Often controls cluster-wide responsibilities Often controls resource-level concurrency

For example, electing one scheduler responsible for global coordination is a leader-election problem.

Preventing two workers from simultaneously modifying the same resource is usually a distributed-locking or concurrency-control problem.

Both require careful handling of ownership expiration, stale processes, and partial failures.

Leader Election in Databases

Replicated databases frequently use leader election during failover.

Normal operation might look like:

               ┌→ Replica B
Primary A ─────┼→ Replica C
               └→ Replica D

If Primary A fails, the system needs to choose a replacement:

Primary A → Failed

Replica B ─┐
Replica C ─┼→ Election
Replica D ─┘
              ↓
        Replica C becomes Primary

The election cannot simply choose any reachable replica.

The system may need to consider which replica contains the latest committed state, whether a quorum is available, and whether the old primary can still accept writes.

The replication side of this architecture is covered in What Is Database Replication?.

Leader election provides failover coordination, while replication ensures multiple nodes contain copies of the underlying data. Both mechanisms are needed for many highly available database designs.

Leader Election in Distributed Workers

Leader election is also useful when many identical application instances exist but one cluster-wide task must have a coordinator.

Consider a service with four instances:

Instance A
Instance B
Instance C
Instance D

Every instance can serve API requests, but only one should perform scheduled cleanup:

Instance A → API + Leader Tasks
Instance B → API
Instance C → API
Instance D → API

If A fails:

Instance A → Failed
Instance C → New Leader

Instance B → API
Instance C → API + Leader Tasks
Instance D → API

This avoids permanently assigning the scheduled task to one machine while preserving redundancy.

However, leader election should not be used as a substitute for idempotency. A leader can fail after performing an operation but before recording that it completed.

The replacement leader may retry the same operation.

For this reason, leader-triggered workflows often still need the techniques described in Idempotency and Deduplication in Distributed Systems.

When Leader Election Is Not Needed

Leader election introduces coordination overhead and a temporary availability gap during elections. It should only be introduced when some responsibility actually requires unique authority.

For example, suppose multiple workers consume independent jobs from a queue:

Queue
  ↓
┌─────────┬─────────┬─────────┐
Worker A  Worker B  Worker C

If the queue already assigns each message to one consumer according to the required delivery semantics, electing a leader to distribute every job can add an unnecessary bottleneck.

Similarly, independent stateless API instances usually do not need a leader:

Load Balancer
      ↓
┌─────────┬─────────┬─────────┐
API A     API B     API C

Any healthy instance can process a request.

Possible alternatives to leader election include:

  • message queues;
  • partition ownership;
  • database uniqueness constraints;
  • idempotent operations;
  • optimistic concurrency control;
  • distributed locks for short-lived resource ownership;
  • stateless processing where no unique coordinator is necessary.

A leader should exist because the architecture needs centralized authority for a specific responsibility, not simply because the application runs on multiple machines.

Production Design Example

Consider a notification platform running five scheduler instances. Each scheduler can serve internal requests, but exactly one should create scheduled notification batches.

Scheduler A
Scheduler B
Scheduler C
Scheduler D
Scheduler E

The cluster uses a coordination service to maintain leadership.

At startup, all instances are followers:

A → Follower
B → Follower
C → Follower
D → Follower
E → Follower

An election begins. Scheduler C receives the required quorum and becomes leader for term 81.

A ─┐
B ─┤
C ─┼→ C elected
D ─┤
E ─┘

Leader = C
Term   = 81

Scheduler C begins scanning for notification schedules that are ready to run.

Leader C
   ↓
Find due schedules
   ↓
Create notification jobs
   ↓
Message Queue
   ↓
Workers

The leader does not send millions of notifications itself. It only performs the coordination-sensitive scheduling step. Parallel workers handle the scalable processing.

Each generated batch also has a durable uniqueness key:

schedule_id + scheduled_execution_time

This protects against duplicate scheduling if leadership changes at an inconvenient moment.

Suppose C becomes isolated from the other four schedulers.

Scheduler C
    X
A B D E

The majority side no longer receives C's heartbeat and starts a new election.

Scheduler D becomes leader in term 82:

Leader = D
Term   = 82

C still believes it was leader in term 81, but operations requiring leadership include the term number.

A protected state store has already observed term 82:

Current term = 82

C request → term 81 → rejected
D request → term 82 → accepted

This fences C from performing stale leader operations.

When network connectivity returns, C discovers the newer term and becomes a follower.

The architecture therefore uses several independent protections:

Quorum
   ↓
Elect one leader
   ↓
Terms
   ↓
Order leadership generations
   ↓
Fencing
   ↓
Reject stale leaders
   ↓
Idempotency / uniqueness
   ↓
Protect repeated business operations

Leader election determines who should coordinate the work. It does not eliminate the need to make the work itself safe under retries and failures.

Monitoring Leader Election

Leader election is infrastructure that can directly affect application availability, so leadership behavior should be observable.

Metric What It Reveals
Current leader Which node currently owns leadership
Current term or epoch How leadership generations are progressing
Election count How frequently leadership changes
Election duration How long leader-dependent work remains unavailable
Heartbeat latency Communication health between cluster members
Heartbeat failures Potential node or network instability
Quorum availability Whether the cluster can safely elect or maintain leadership
Rejected stale operations Whether old leaders are attempting protected work

Frequent elections are especially important to investigate.

A cluster repeatedly changing leaders may be experiencing network instability, overloaded nodes, aggressive heartbeat timeouts, long runtime pauses, or resource exhaustion.

The leader can technically remain recoverable while the system still suffers severe performance degradation from constant elections.

Common Leader Election Mistakes

  • Assuming a silent leader is definitely dead. Network partitions and long pauses can make healthy nodes appear unavailable.
  • Allowing any isolated node to elect itself. This can create multiple leaders during a partition.
  • Ignoring quorum requirements. Leadership needs a protocol that preserves safety across failures.
  • Failing to identify leadership generations. Terms or epochs help distinguish current leadership from stale leadership.
  • Assuming a new election stops the old leader. The previous process may still be running.
  • Skipping fencing for critical external operations. Stale leaders can otherwise continue modifying resources.
  • Using extremely short failure-detection timeouts. Temporary delays can cause unnecessary elections.
  • Using extremely long failure-detection timeouts. Real failures take too long to recover from.
  • Putting all application work through the leader. The leader can become a scalability bottleneck.
  • Assuming leader election prevents duplicate business operations. Failures around handoff boundaries can still require idempotency.
  • Adding leader election where stateless or partitioned processing would work. Unnecessary coordination increases complexity and reduces availability.

The election mechanism should have explicit guarantees for candidate eligibility, failure detection, quorum, leadership generations, stale-leader behavior, and recovery after network partitions.

Conclusion

Leader election allows a distributed system to select one node as the authoritative coordinator for operations that should have a single owner. When that node fails, another eligible node can take over.

The difficult part is not choosing a node during normal operation. The difficult part is preserving one authoritative leadership history while nodes crash, messages are delayed, networks partition, and old leaders continue executing.

Production systems therefore combine leader election with mechanisms such as heartbeats, quorum voting, terms or epochs, fencing, replicated state, and idempotent operations.

The core principle is: leader election determines which node currently has authority, while the surrounding protocol must ensure that previous or isolated leaders can no longer exercise that authority safely.

Comments (0)