← Lessons

quiz vs the machine

Gold1420

System Design

Leader Election with Raft

How Raft picks a single leader using terms, votes, and randomized timeouts.

5 min read · core · beat Gold to climb

Why elect a leader

In a replicated cluster, having one leader simplifies ordering: clients write only to the leader, which replicates entries to followers. Raft makes leadership a first class concept so the cluster always agrees on who is in charge.

Terms and votes

Time is divided into terms, each a monotonically increasing number. A follower that hears nothing from a leader for its election timeout becomes a candidate, increments the term, and requests votes.

  • Each server grants at most one vote per term.
  • A candidate wins when it collects votes from a majority.
  • Majority guarantees at most one leader per term.

Avoiding split votes

If two candidates start at once, votes may split and no one wins. Raft uses randomized election timeouts so one server usually times out first, becoming the sole candidate and winning cleanly.

Key idea

Raft elects exactly one leader per term by requiring a majority vote, and breaks ties with randomized timeouts so the cluster converges quickly.

Check yourself

Answer to earn rating on the learn ladder.

1. Why does a candidate need a majority of votes to become leader?

2. What problem do randomized election timeouts solve?