How a group picks one machine to lead, and picks a new one when it fails.
Leader election is how a group of machines picks exactly one of them to be in charge of a job, and picks a new one if that machine fails.
In a single program, "who is in charge" is easy: there is only one. In a group of servers, each server can only see the others through a network, and the network can be slow or broken. A server cannot tell "the leader is dead" apart from "the leader is just slow or cut off".
A leader election algorithm gives the group clear rules so that all running servers agree on one leader. Followers watch the leader using heartbeats. When the heartbeats stop for too long, the followers start a new election.
A good election has two promises. Safety: at most one leader at a time. Liveness: if enough machines are working, a leader is eventually chosen. Because of the limits of networks, an exact guarantee of both at once is impossible in theory (the FLP result), so real systems use timeouts and majorities.
| Method | How it works | Good and bad |
|---|---|---|
| Bully | A node that notices the leader is gone asks all higher ids. The highest running id wins | Simple and quick. A network split can create two leaders. Many messages |
| Ring | Nodes pass an election message around a ring, collecting ids. The biggest id wins | Fewer messages. Slow, and a broken link breaks the ring |
| Raft style vote | Random timeouts, a candidate asks for votes, a majority wins in a numbered term | Safe and well tested. Needs a majority alive |
| Paxos based | Leader chosen through Paxos rounds (as in Multi-Paxos) | Very safe. Harder to build |
| Lock or lease (ZooKeeper, etcd, Consul) | All nodes try to create the same key with a lease. The one that succeeds leads, and renews the lease | Easy to add to an app. You depend on the store and on clock-safe leases |
| Static or by hand | An operator picks the leader | Simplest. Slow recovery and a human in the loop |
| Idea | What it is | Why it matters |
|---|---|---|
| Heartbeat | A regular I am alive message | How followers notice a dead leader |
| Timeout | How long to wait before starting an election | Short is fast but jumpy. Long is calm but slow |
| Term or epoch | A number that goes up with each new leader | Lets everyone ignore a stale old leader |
| Quorum | More than half of all nodes | Two groups can never both have a quorum |
| Lease | A leadership that expires unless renewed | A stuck leader loses power on its own |
Click a node to crash or restore it. The old leader was S7. In the majority mode the lowest running id fires its timer first in this demo.
| Term | Meaning in easy words |
|---|---|
| Leader (primary, master) | The one node currently in charge of a job |
| Follower (replica, standby) | A node that waits and can take over |
| Candidate | A node that wants to become leader and is asking for votes |
| Election | The process of choosing a leader |
| Heartbeat | A small regular message that shows a node is alive |
| Election timeout | How long to wait without a heartbeat before starting an election |
| Quorum | More than half of all nodes. Two groups cannot both have one |
| Term / epoch | A counter that rises with every new leader |
| Lease | A leadership or lock that ends by itself unless renewed |
| Fencing token | A rising number that lets storage reject writes from an old leader |
| Failover | Switching the work from a failed leader to a new one |
| Choice | We gain | We lose |
|---|---|---|
| Short timeout | Fast failover | False elections when the network is slow or the leader is busy |
| Long timeout | Stable leadership | A longer outage when the leader really fails |
| Majority vote | Safety on partitions | No leader when a majority is lost |
| Bully (highest id) | Very simple | Two leaders possible on a network split |
| Lease in an external store | Easy to add to an app | A new dependency, and risk if clocks or pauses are wrong |
| No leader (all nodes work) | No election and no bottleneck | Needs conflict-free work or extra coordination |