Systems · Consensus

Elect a Leader.

Consensus sounds abstract until you watch it happen. In Raft, every node is one of three roles — follower, candidate, or leader — and a single leader per term makes all the decisions. It stays leader by sending periodic heartbeats that reset everyone’s randomized election timer. When a follower’s timer runs out (it hasn’t heard from a leader), it becomes a candidate, bumps the term, votes for itself, and asks the rest for votes; whoever collects a majority wins and becomes leader. The randomized timers make split votes rare and self-correcting. Run the cluster, then kill the leader and watch a fresh election.

strong-leader consensus · majority vote (⌈n/2⌉+1) · randomized timers avoid split votes
term

A logical clock. Each election starts a new, higher term; one leader per term.

roles

Follower (waits), candidate (seeking votes), leader (sends heartbeats).

election timer

A randomized countdown; if it hits zero without a heartbeat, the node runs for leader.

majority

A candidate needs votes from more than half the nodes to become leader.

raft.js — followers, candidates, one leader
Ready
Five nodes start as followers with random election timers. Step advances time: when a timer hits zero, that node becomes a candidate and asks for votes. A majority makes it leader. Auto to run it live.
0
term
leader
0
elections

How it works

The whole protocol turns on two randomized timers and one rule: majority wins. Each follower waits a random election timeout (say 150–300 ticks). Because the timeouts differ, one node almost always times out first, becomes a candidate alone, and wins cleanly — this randomization is Raft’s elegant fix for split votes. A candidate votes for itself and requests votes; each node grants at most one vote per term, to the first qualifying candidate it hears. Collect a majority and you’re leader; then send heartbeats (empty log-append messages) fast enough to reset every follower’s timer before it expires, which is how a healthy leader stays in power. If two candidates do split the vote in the same term, nobody wins, the term ends with no leader, and fresh random timeouts break the tie on the next round.

1

Followers wait on a random timer

Every node begins as a follower with its own randomized election timeout. A heartbeat from a leader resets this timer; silence lets it count down.

2

Timeout → candidate, request votes

When a follower’s timer expires, it becomes a candidate, increments the term, votes for itself, and asks every other node for a vote for this term.

3

Majority → leader, then heartbeat

Each node grants at most one vote per term. A candidate that collects a majority becomes leader and immediately starts sending heartbeats to reset followers and suppress new elections.

Leader fails → new election

If the leader crashes or is partitioned away, its heartbeats stop, a follower times out, and the cycle repeats at a higher term — automatically restoring a single leader.

Roles
follower/candidate/leader
Vote needed
majority
Split votes
random timers fix them
Used in
etcd, Consul, TiKV

The code

# Raft leader election, per node (simplified) on election_timeout: # no heartbeat heard in time state = CANDIDATE term += 1 votes = {self} # vote for yourself for peer in others: request_vote(peer, term) # ask for their vote on request_vote(candidate, term): if term >= my_term and not_voted_this_term: grant_vote(candidate) # at most one vote per term on majority_votes: state = LEADER while leader: send_heartbeats() # reset followers' timers

Quick check

1. How many votes does a candidate need to become leader?

2. How does Raft avoid endless split votes?

3. What keeps a healthy leader in power?

FAQ

What is Raft?

A consensus algorithm keeping a cluster in agreement on a replicated log, designed to be easier to understand than Paxos. It uses a strong leader — one elected per term handling all requests and replication — and decomposes consensus into leader election, log replication, and safety, with randomized timeouts to avoid split votes.

What is a term in Raft?

Raft’s logical clock — a monotonically increasing number starting with each election, with at most one leader per term. Terms detect stale leaders and messages: an older-term message is rejected, and learning of a newer term makes a node step down to follower.

How does Raft handle a network partition?

If a partition cuts the leader off from a majority, the majority side elects a new leader at a higher term and keeps progressing; the old leader on the minority side can’t commit (no majority) and steps down once it sees the higher term when the partition heals. Only the majority side ever progresses.

How does Raft compare to Paxos?

Both give the same consensus guarantees, but Raft was designed for understandability: a single strong leader and a clear decomposition (election, replication, safety), versus Paxos’s more abstract, leaderless formulation. Raft is simpler to implement correctly, so etcd, Consul, and TiKV use it.

Keep going

Finished this one? 0 / 44 Labs done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Labs