Course contentsShow
← Computer Science
Lesson 2663 of 2,87259. Consensus and ReplicationFree lesson

Leader Election: Voting Process

Understand how candidates request votes, how servers grant votes, and how a candidate becomes leader with majority votes.

Leader Election: Voting Process

You'll learn how candidates request votes, how servers grant votes, and how a candidate wins the election in Raft.

How Candidates Request Votes

When a server transitions to the Candidate state (triggered by an election timeout), it increments its current term and sends RequestVote RPCs to all other servers in the cluster. This RPC includes:

  • The candidate's current term
  • The candidate's ID
  • Information about the candidate's log (for log consistency checks)

Think of this as a political candidate calling each voter personally to ask for their support.

How Servers Grant Votes

Each server can grant at most one vote per term, following a "first-come, first-served" rule. A server will vote for a candidate if:

  1. The candidate's term is at least as current as the server's term
  2. The server hasn't already voted for someone else this term
  3. The candidate's log is at least as up-to-date as the server's log

This prevents split votes from compromising safety—once you've committed to supporting one candidate, you stick with them for that entire term (election cycle).

Becoming Leader with Majority Votes

A candidate becomes the leader when it receives votes from a strict majority (more than half) of the servers in the cluster. For example, in a 5-node cluster, you need 3 votes; in a 7-node cluster, you need 4 votes.

Why majority? Because two different majorities must overlap by at least one server, preventing two candidates from both winning in the same term. Once elected, the leader immediately sends heartbeat messages (empty AppendEntries RPCs) to establish authority and prevent new elections.

Key Takeaway: Raft's voting process ensures exactly one leader per term through majority quorums and one-vote-per-term constraints, preventing split-brain scenarios while maintaining availability.