SkillSprout
Chapter 19 · Lesson 2
Intermediate
4 min read

Leader Election and Failover

Overview

What happens when the leader dies? Followers must notice and choose a new leader quickly, without two nodes claiming the job. This switch is called failover.

Here you will see how followers use heartbeats and a majority vote to pick a replacement.

Detailed Notes

What is leader election

Leader election is the process by which a group of nodes agrees on exactly one new leader. Failover is the whole recovery: detect the failure, elect or promote a new leader, and redirect clients to it.

The problem it solves

A leader is a single point of failure for writes. If it crashes and no one takes over, writes stop. If a human has to promote a replica at 3 a.m., the outage is long. Automatic election restores service in seconds, but it must be careful: choosing two leaders, or choosing a follower that missed recent data, causes corruption.

How it works

Here is the common approach used by systems like Raft (an election protocol used by etcd and many databases):

  1. Detect. Followers expect regular heartbeats from the leader (see the Heartbeats chapter). When none arrive before a timeout, a follower suspects the leader is gone.
  2. Candidate. That follower becomes a candidate, increases a counter called the term (a number identifying the current leadership period), and asks the others for votes.
  3. Vote. Each node votes for at most one candidate per term, and only if the candidate's log is at least as up to date as its own.
  4. Win. A candidate that gets votes from a majority (more than half of all nodes) becomes leader and announces itself. This idea has a name, quorum, covered in the next chapter.
  5. Redirect. Clients are directed to the new leader. A higher term always beats a lower one, so an old leader that returns steps down.

Why this is safe:

  • Two majorities always share at least one node, and each node votes once per term, so two leaders cannot win the same term.
  • Requiring an up-to-date log means the new leader does not lose writes that were already confirmed.
  • Election timeouts are randomised per node so followers rarely become candidates at the same instant and split the vote.

Practical points:

  • Use an odd number of voting nodes (3 or 5). Four nodes tolerate only one failure, same as three, so the extra node adds cost without resilience. Three tolerate 1 failure; five tolerate 2.
  • Spread nodes across failure domains, such as separate data centres or availability zones (isolated data centres within one region).
  • While the election runs, writes wait or fail. The system briefly prefers correctness over availability.

In a simple primary-replica database without Raft, a tool can do the same job by promoting the replica with the most complete log.

A simple example

A group project has a team lead. If the lead goes silent, one member says "I'll lead, vote for me." Members vote once, and whoever gets more than half the votes leads. If two volunteer at once and votes split, they wait a random few minutes and retry.

System design example

A managed database has a primary and a synchronous standby in another availability zone. When the primary fails, the platform detects missing heartbeats, promotes the standby, and updates the DNS name applications use. Applications see connection errors for around a minute and then reconnect. Because replication was synchronous, no acknowledged transaction was lost.

Summary

  • Failover = detect leader failure, elect a new leader, redirect clients.
  • Followers detect failure via missing heartbeats and then hold a vote.
  • A majority vote, one vote per term, and an up-to-date log keep the election safe.
  • Use odd cluster sizes, randomised timeouts, and nodes in different zones.

Architecture Diagram

Leader election and failover
Architecture
Old Leaderfailed

Followers vote

Candidateasks for votes
Voter 1
Voter 2
New Leadernext term

When the leader fails, followers vote and a majority picks a new one.

  1. 1Followers stop receiving heartbeats from the leader.
  2. 2A follower's timeout expires and it becomes a candidate with a higher term.
  3. 3A majority of nodes vote for it, checking its log is up to date.
  4. 4The new leader announces itself and clients are redirected.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course