Leader Election Explained: How Distributed Systems Choose a Leader

Leader election explained in plain terms means understanding how a group of computers that all do the same job can agree that exactly one of them is in charge. The central question is what happens when the machine coordinating a system fails, and the answer is an election: the remaining machines vote, one wins, and the rest accept the result, so the system carries on with a single leader.

Leader election is the process by which the nodes in a distributed system choose one of their number to coordinate the work. The leader is usually the node that accepts writes, assigns tasks, or makes decisions, while the followers replicate its state and stand ready to take over, and the election is what decides which node holds that role at any given time.

The idea is simple: a system that needs one coordinator must have a reliable way to pick one, and to pick a new one when the old one disappears. Without an election, two nodes might both believe they are the leader and issue conflicting orders, or none might step forward and the system would stall. This article explains what leader election is, how it works, and where it is used.

Key takeaways

  • Leader election picks exactly one node to coordinate a system.
  • Followers replicate the leader and stand ready to take over.
  • An election runs when the leader fails or a new term begins.
  • Safety means never having two leaders at the same time.
  • Raft, Paxos, and the bully algorithm are the best-known methods.

What leader election is

Leader election is needed wherever a distributed system has a role that only one node may hold. In a database cluster, one node accepts writes while the others replicate them. In a job scheduler, one node hands out work while the others wait. In a coordination service, one node proposes changes while the others approve them, and in every case the system needs a way to choose that node and to replace it when it fails.

The election must satisfy two properties. Safety means that at most one leader is elected in any given term, so two nodes never issue conflicting orders at the same time. Liveness means that an election eventually produces a leader, so the system does not stall forever waiting for one, and these two properties pull in opposite directions, since a system that is too careful may never elect anyone.

Terms, or epochs, are how elections are organised. Time is divided into numbered terms, and each term has at most one leader. When a follower stops hearing from the leader, it starts a new term and tries to become the leader itself, and the term number lets nodes reject messages from an old, deposed leader that has not yet realised it lost its role. Our guide to Raft explains the consensus algorithm whose election mechanism is the most widely used today, and our guide to Paxos explains the older algorithm that first solved the problem.

How leader election works

In the Raft algorithm, which is the clearest example, every node begins as a follower. Followers expect a regular heartbeat from the leader, and if one does not arrive within a randomised timeout, the follower assumes the leader has failed, increments the term number, votes for itself, and asks the other nodes for their votes.

Each node may vote only once per term, and it votes for the first candidate that asks, provided that candidate’s log is at least as up to date as its own. A candidate that wins a majority of the votes becomes the leader for that term, and it immediately starts sending heartbeats so that the other nodes know it is in charge and do not start elections of their own.

The majority rule is what keeps the election safe. Since any two majorities in the same group must share at least one node, and that node can vote only once per term, two candidates cannot both win a majority in the same term, so at most one leader is ever elected, even if the network is partitioned and several nodes try at once.

The bully algorithm works differently and is simpler. Each node has a unique identifier, and when a node notices the leader is gone, it sends an election message to every node with a higher identifier. If any of them answers, it steps back and waits; if none answers, it declares itself the leader and tells the rest. The highest-numbered live node always wins, which is why the algorithm is called the bully.

When a deposed leader recovers, it must not simply resume. Its term number is out of date, and the other nodes will reject its messages, so it rejoins as a follower, catches up on the log, and waits for a future election. This is what prevents the split-brain failure, in which two nodes both believe they are the leader and issue conflicting orders.

A dark server room with rows of glowing green racks, one rack standing brighter than the rest at the head of the row
A dark server room with rows of glowing green racks, one rack standing brighter than the rest at the head of the row, shows how leader election works: one node leads, and the rest follow until it fails.

Comparing leader election with other approaches

Leader election is one of several ways to choose a coordinator, and each was built with a different emphasis. Raft elects a leader through votes and terms, the bully algorithm elects the highest-numbered node, and a static configuration simply names a leader in advance, but each approach trades safety, speed, and simplicity in a different way.

The table below sets out the main contrasts between the leading approaches.

Aspect Raft election Bully algorithm Static leader
How the leader is chosen Majority vote per term Highest live identifier Named in configuration
Safety guarantee At most one per term At most one, if reliable None, if the leader fails
Failure recovery Automatic, fast Automatic, slower Manual, needs an operator
Complexity Moderate Low Very low
Best for Databases and consensus Small, stable clusters Single-node systems

The practical result is that Raft is the right choice for systems that need a strong safety guarantee, the bully algorithm suits small clusters where simplicity matters, and a static leader is enough only where a human operator can step in when it fails.

Where leader election is used

Distributed databases use leader election to choose the node that accepts writes. In a replicated database, one node is the primary, taking writes and sending them to the replicas, and when it fails, the replicas elect a new primary from among themselves, so that writes can resume without an operator intervening.

Message brokers use leader election for each partition. In Kafka, every partition has a leader broker that handles reads and writes, and follower brokers replicate its log, and when the leader broker fails, the followers elect a new leader for that partition, so that producers and consumers see only a brief interruption.

Job schedulers use leader election to avoid running the same job twice. Only the leader schedules work, while the followers wait, and if the leader fails, a follower is elected and takes over the schedule, so that jobs continue without duplication or gaps.

Coordination services use leader election internally. ZooKeeper and etcd both elect a leader among their own servers, and that leader is the one that proposes changes, while the followers approve them, which is how the service itself stays consistent while it helps other systems coordinate.

Cloud control planes use leader election for their controllers. A Kubernetes cluster, for example, runs several copies of its controller manager, but only the elected leader is active, and the others stand by, so that a failure of the active controller is handled in seconds rather than minutes.

Strengths and limits

The greatest strength of leader election is that it makes a system self-healing. A failed leader is replaced automatically, without an operator, and the system continues with only a brief pause, which is why it is a standard building block in almost every distributed system.

It is also a strength that an elected leader simplifies everything else. Once a leader exists, writes go to one place, decisions are made in one place, and the rest of the system can be simple followers, which is far easier to build and to reason about than a system in which every node is equal.

The limits are real, though. An election takes time, during which the system may be unavailable, and a leader is a bottleneck, since all writes flow through one node. If the network is partitioned, the side without a majority cannot elect a leader at all, and it must wait, which is the price of the safety guarantee.

Leader election checklist

  • Elect a leader automatically; never rely on a manual choice.
  • Use terms so a deposed leader cannot resume by mistake.
  • Require a majority of votes, not just the most votes.
  • Randomise election timeouts to avoid repeated split votes.
  • Make followers reject messages from an old term.
  • Test leader failure during heavy write traffic.
  • Test a network partition that splits the cluster in half.
  • Watch election frequency; frequent elections mean instability.
  • Make sure a new leader has the most up-to-date log.

Frequently asked questions

What is leader election in simple terms?

It is the process by which a group of computers agrees that exactly one of them is in charge. The others follow it, replicate its state, and elect a replacement if it fails, so the system always has a single coordinator.

Why does a distributed system need a leader?

Because some decisions must be made in one place. Writes to a database, the assignment of work, and the ordering of changes are all easier and safer when one node decides and the rest follow, rather than every node deciding for itself.

What is a term in leader election?

It is a numbered period of leadership, like a session. Each term has at most one leader, and the term number lets nodes tell a current leader from an old one, so a deposed leader cannot resume and cause a conflict.

How does Raft elect a leader?

A follower that stops hearing heartbeats starts a new term, votes for itself, and asks the others for their votes. A candidate that wins a majority becomes the leader, and it sends heartbeats so the others know it is in charge.

What is split-brain, and how does election prevent it?

Split-brain is when two nodes both believe they are the leader and issue conflicting orders. Elections prevent it by requiring a majority of votes, since two majorities must share a node, and that node can vote only once per term.

When should I not rely on leader election?

When the system must keep writing on both sides of a network partition, or when a single leader would be a bottleneck for the workload. In those cases a leaderless design, or a system with several independent leaders, may fit better.

The bottom line

Leader election explained in one line is this: let the nodes vote, require a majority, and never allow two leaders in the same term.

It is the mechanism that lets a distributed system replace a failed coordinator on its own, and the reason a cluster can lose its leader and carry on without anyone noticing.

This article is general information about how leader election works, not advice about designing coordination for a particular system.

Latest articles

Scroll to Top