Raft protocol
Distributed Consensus
Programming
votedFor variable
Computer Science

In Raft distributed consensus, what do I set votedFor to?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In the Raft distributed consensus algorithm, servers work together to maintain a consistent state across a distributed system. For the election of a leader among these servers (or nodes), the votedFor variable plays a crucial role. Here we will explore what votedFor is, how it is used in Raft, and the various considerations around setting its value during elections.

Understanding votedFor in Raft

votedFor is a variable stored on each node in the Raft cluster. It tracks the candidate that the node has voted for in the current term, where a term is a logical time period in Raft's logical clock. The main purpose of votedFor is to prevent nodes from voting for multiple candidates in the same term, thus ensuring that the election process is fair and achieves a clear majority.

Technical Workflow

Each node in a Raft cluster can be in one of three states: follower, candidate, or leader. votedFor is most relevant in the follower and candidate states:

  1. Follower: Initially, all nodes are followers. A follower remains in this state as long as it receives regular heartbeat messages from a leader or candidate.
  2. Candidate: If a follower does not receive a heartbeat within a certain timeout period, it assumes there is no active leader and transitions to a candidate state to initiate an election. At this point, it increments its current term, votes for itself, and resets its votedFor to its own ID, thus recording that it has voted for itself.
  3. Leader: The candidate that receives votes from a majority of the nodes in the cluster becomes the leader.

Voting Rules

  • Each node will reset its votedFor to null at the start of a term or when its term is incremented.
  • When transitioning to a candidate, it sets votedFor to its own server ID.
  • When a vote request is received, a node will vote for the requesting candidate if:
    1. The node’s votedFor is null or the requesting candidate’s ID (allowing it to vote for the same candidate multiple times in a single term if needed);
    2. The candidate’s term is at least as large as the node’s current term;
    3. The candidate’s log is at least as up-to-date as the voter's log.

Example Scenario

Consider a cluster with three nodes: A, B, and C. If A times out and becomes a candidate in term 5, A will vote for itself and set votedFor = A. If B and C also timeout and transition to candidates, they will set votedFor to their respective IDs after increasing their term numbers.

If node A then requests a vote from node B and C:

  • B and C will check if their votedFor is either null or A, and if A's log is at least as updated as theirs.
  • If both conditions are satisfied, they may vote for A, each setting votedFor = A.

Table Summary of votedFor Rules in Raft

ConditionvotedFor SettingOutcome
Follower with no timeoutsnull or unchangedRemains follower
Follower timing out, becomes candidateown IDVotes for self, requests votes
Receives vote request from candidatenull, or candidateChecks candidate’s term/log, then votes
New term startsnullResets for new election

Additional Considerations

  • Persistence: The votedFor data should be persisted to stable storage before a node replies to a vote request. This ensures that the state is maintained even if the node crashes and needs to be restarted.
  • Security and Integrity: Care must be taken to ensure that the votedFor field cannot be tampered with, as it could affect the fairness and integrity of the election process.

The use of votedFor is a simple yet powerful mechanism to ensure that no node in a Raft cluster votes for more than one candidate in the same term, supporting the principle of one-vote-per-term per node. This is crucial for maintaining the consistency and reliability of the distributed consensus.


Course illustration
Course illustration

All Rights Reserved.