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:
- 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.
- 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
votedForto its own ID, thus recording that it has voted for itself. - 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
votedFortonullat the start of a term or when its term is incremented. - When transitioning to a candidate, it sets
votedForto its own server ID. - When a vote request is received, a node will vote for the requesting candidate if:
- The node’s
votedForisnullor the requesting candidate’s ID (allowing it to vote for the same candidate multiple times in a single term if needed); - The candidate’s term is at least as large as the node’s current term;
- 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
votedForis eithernullor 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
| Condition | votedFor Setting | Outcome |
| Follower with no timeouts | null or unchanged | Remains follower |
| Follower timing out, becomes candidate | own ID | Votes for self, requests votes |
| Receives vote request from candidate | null, or candidate | Checks candidate’s term/log, then votes |
| New term starts | null | Resets for new election |
Additional Considerations
- Persistence: The
votedFordata 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
votedForfield 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.

