Skip to content
~90s Visual Explainer

Raft Consensus Algorithm

How distributed systems achieve agreement across multiple nodes, even when failures occur.

N1 Follower N2 Follower N3 Follower All nodes waiting for leader... N1 Candidate N2 Follower N3 Follower Term = 1 N1 times out → becomes Candidate N1 Candidate N2 Follower N3 Follower RequestVote RequestVote N1 requests votes from N2, N3 N1 Candidate 3/3 votes N2 Voted: N1 N3 Voted: N1 YES YES MAJORITY ✓ N2, N3 grant votes → Majority! N1 LEADER N2 Follower N3 Follower N1 becomes LEADER N1 LEADER N2 Follower N3 Follower Leader sends heartbeats to maintain authority
1 / ?

Initial State: Three Followers

In a Raft cluster, all nodes start as followers. They're waiting to hear from a leader, but there isn't one yet.

Each follower maintains an election timeout — a random interval between 150-300ms. If a follower doesn't hear from a leader before this timeout expires, it takes action.

  • All nodes are in follower state
  • No leader exists yet
  • Each node has a random election timeout

Election Timeout: Becoming a Candidate

Node N1's election timeout fires first. It hasn't heard from any leader, so it transitions to candidate state.

As a candidate, N1 immediately:

  • Increments the current term to 1
  • Votes for itself (1 vote)
  • Resets its election timer
  • Prepares to request votes from other nodes

The term is a logical clock that increases with each election. It prevents stale leaders from causing confusion.

Requesting Votes

N1 sends RequestVote RPCs to all other nodes in the cluster (N2 and N3).

The request includes:

  • term: The candidate's current term (1)
  • candidateId: N1
  • lastLogIndex: Index of candidate's last log entry
  • lastLogTerm: Term of candidate's last log entry

The log information ensures that only candidates with up-to-date logs can become leader — this is crucial for maintaining consistency.

Votes Granted: Achieving Majority

Both N2 and N3 receive the vote request and decide to grant their votes to N1.

A node grants a vote if:

  • The candidate's term is ≥ the voter's current term
  • The voter hasn't already voted in this term
  • The candidate's log is at least as up-to-date as the voter's

N1 now has 3 out of 3 votes — a clear majority. In Raft, a candidate needs votes from (n/2) + 1 nodes to win. With 3 nodes, that's 2 votes.

Leader Elected!

With majority votes collected, N1 immediately becomes the leader.

As leader, N1 is now responsible for:

  • Receiving all client requests
  • Replicating log entries to followers
  • Deciding when entries are safely committed
  • Sending periodic heartbeats to maintain authority

There can only be one leader per term. If N2 or N3 tried to become leader in term 1, they would fail because they already voted for N1.

Heartbeats: Maintaining Leadership

The leader sends periodic heartbeat messages to all followers. These are empty AppendEntries RPCs sent every 50-100ms.

Heartbeats serve two purposes:

  • Prevent elections: Followers reset their election timers when they receive a heartbeat
  • Detect failures: If heartbeats stop, followers know the leader may have crashed

This is how Raft maintains cluster stability. As long as the leader is healthy and the network is functioning, no new elections occur.

What's Next?

Leader election is just Phase 1 of Raft. Once a leader is established, it handles log replication — ensuring all nodes have the same sequence of commands.