Quick revision
The short answer
A follower that hears no heartbeat within a randomised election timeout becomes a candidate, increments the term, votes for itself and asks for votes; it wins with a majority, and nodes grant one vote per term and only to candidates whose log is at least as up to date. The leader replicates with AppendEntries, which carries the index and term of the preceding entry so followers reject mismatches until the logs agree, and an entry is committed once it is on a majority and belongs to the leader's current term. A leader cannot commit an older-term entry just by counting replicas, because such an entry can still be overwritten by a later leader; it commits older entries indirectly by committing an entry of its own term, which is why new leaders append a no-op.
What to remember
In this answer, we walked through Raft's three mechanisms. Elections start when a randomised timeout passes, need a majority of votes, and are restricted to candidates whose logs are at least as up to date, so new leaders hold every committed entry. Replication uses AppendEntries with the previous index and term, stepping back until logs match and then overwriting conflicting suffixes. An entry commits when it is on a majority and from the leader's current term; older-term entries commit only indirectly, which is why leaders append a no-op. Throughout, safety never depends on timing; timing only affects availability.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A follower that hears no heartbeat within a randomised election timeout becomes a candidate, increments the term, votes for itself and asks for votes; it wins with a majority, and nodes grant one vote per term and only to candidates whose log is at least as up to date. The leader replicates with AppendEntries, which carries the index and term of the preceding entry so followers reject mismatches until the logs agree, and an entry is committed once it is on a majority and belongs to the leader's current term. A leader cannot commit an older-term entry just by counting replicas, because such an entry can still be overwritten by a later leader; it commits older entries indirectly by committing an entry of its own term, which is why new leaders append a no-op.
Why this question is asked
Raft is the consensus protocol most engineers meet in practice, inside etcd, Consul, KRaft and distributed SQL databases. Knowing its internals helps us read their logs, tune their timeouts and understand outages. Interviewers ask this question to see whether we know the three mechanisms, election, replication and commitment, and especially the subtle rule about entries from earlier terms that the Raft paper illustrates with its well-known figure 8. In this answer, we will recap terms and roles, walk through an election, follow replication and log repair, explain the commitment rule with the scenario that motivates it, and see why timing affects only availability.
1A Quick Recap: Terms and Roles
Every Raft node is in one of three roles: follower, candidate or leader. Time is divided into terms, numbered 1, 2, 3 and so on, and each term has at most one leader. A term number acts as a logical clock: every message carries the sender's term, a node that sees a higher term immediately adopts it and becomes a follower, and messages with an older term are rejected.
Each node keeps a log of entries, each with an index (its position) and the term in which the leader created it. Nodes persist their current term, the vote they cast in that term, and their log, before replying to anything, so that a crash and restart cannot make them forget a vote or an acknowledged entry.
2Electing a Leader
A leader sends heartbeats, empty AppendEntries messages, regularly, for example every 100 ms. A follower that hears nothing for its election timeout, a random value in a range such as 1,000 to 2,000 ms, assumes the leader is gone. It increments its term, becomes a candidate, votes for itself and sends RequestVote to all other nodes.
A node grants its vote if it has not already voted in this term and if the candidate's log is at least as up to date as its own: the candidate's last entry has a higher term, or the same last term and an index at least as high. A candidate that collects votes from a majority becomes leader and starts sending heartbeats. The up-to-date rule is essential: since a committed entry is on a majority, and a winner needs a majority of votes, at least one voter has every committed entry, and it refuses to vote for a candidate missing it. So every new leader already holds all committed entries.
Randomised timeouts make split votes rare: usually one follower times out first and wins before others start. If votes do split, the candidates time out again with new random values and retry in a higher term.
3Replicating and Repairing Logs
Clients send commands to the leader, which appends them to its own log and sends AppendEntries to each follower. Each message includes prevLogIndex and prevLogTerm, the index and term of the entry just before the new ones. A follower accepts only if its own log has an entry at prevLogIndex with that term. If not, it rejects, and the leader steps back, trying an earlier prevLogIndex, until it finds the point where the two logs agree. From there, the follower deletes any conflicting entries after that point and takes the leader's entries.
This consistency check gives Raft its log matching property: if two logs have an entry with the same index and term, they are identical up to that entry. The leader never changes its own log; it only overwrites followers' uncommitted, conflicting suffixes.
6 messages in order. Play them, or step through them one at a time.
4When an Entry Is Committed
An entry is committed when the leader has stored it on a majority of nodes and it belongs to the leader's current term. The leader then advances its commit index, includes it in later messages, and every node applies committed entries to its state machine in log order. Once committed, an entry is permanent: every future leader will have it, thanks to the voting rule above.
5Why Older-Term Entries Cannot Be Committed by Counting
The restriction to the current term looks fussy until we see the scenario it prevents. Five nodes, S1 to S5. In term 2, S1 is leader and replicates an entry at index 2 only to S2, then crashes. In term 3, S5 becomes leader (votes from S3, S4 and itself, whose logs are as short), appends a different entry at index 2 with term 3, and crashes before replicating it. In term 4, S1 comes back and is elected again, and it continues replicating its old term-2 entry, which is now on S1, S2 and S3: a majority.
If S1 now declared that term-2 entry committed because a majority stores it, here is the danger. Suppose S1 crashes again. S5 can still win an election in term 5: its last entry has term 3, which is higher than the term-2 entries on S2, S3 and S4, so they consider its log more up to date and vote for it. S5 then overwrites index 2 on everyone with its term-3 entry. An entry that was 'committed' would be erased, and a client told it succeeded would lose its write.
Raft avoids this by refusing to commit older-term entries by counting. S1 must first replicate a new entry from its own term 4 to a majority. Once that term-4 entry is on a majority, S5 can no longer win (its last term 3 is lower than theirs), and committing the term-4 entry commits everything before it, including the old term-2 entry, indirectly. That is why a new leader appends a no-op entry at the start of its term: it lets the leader commit leftover entries from earlier terms promptly, and it also lets the leader learn its own commit index before serving linearizable reads.
| Step | What happens | Index 2 on majority? |
|---|---|---|
| Term 2 | S1 leads, writes (2, term 2) to S1, S2; crashes | no |
| Term 3 | S5 leads, writes (2, term 3) on S5 only; crashes | no |
| Term 4 | S1 leads again, copies (2, term 2) to S3 | yes: S1, S2, S3 |
| If S1 counted it as committed | S1 crashes; S5 wins term 5 (last term 3 > 2) | yes, but... |
| Term 5 | S5 overwrites index 2 everywhere with term 3 | the 'committed' entry is lost |
| Raft's rule | S1 must first commit a term-4 entry | then S5 cannot win |
6Timing Affects Availability, Not Safety
Raft's safety, never losing a committed entry and never having two leaders in a term, does not depend on timing at all. Messages may be delayed, reordered or lost; the term and log rules still hold. Timing only decides whether the cluster makes progress.
For good availability, Raft needs the broadcast time (one round trip with a disk write) to be much smaller than the election timeout, which in turn must be much smaller than the mean time between failures. If heartbeats take longer than the election timeout, followers keep starting elections and the cluster churns; if the timeout is huge, failover is slow. Typical settings are a heartbeat of about 100 ms and an election timeout around ten times that.
7Follow-up questions
What does the PreVote extension prevent?
A node that was partitioned away keeps timing out and incrementing its term. When it rejoins, its high term forces the healthy leader to step down, causing a needless election. With PreVote, a node first asks whether it could win, without changing its term, and peers that still hear from a leader say no, so a rejoining node cannot disrupt a working cluster.
Can a follower serve reads?
Not linearizable ones by default, because it may be behind. Raft systems serve consistent reads through the leader, which confirms its leadership with a quorum round or a lease, or let followers serve reads after asking the leader for its current commit index (the read index approach).
Why must a node persist its vote before replying?
If it voted, crashed, restarted and forgot, it could vote again for another candidate in the same term, allowing two leaders in one term, which breaks the core safety rule.
8Conclusion
In this answer, we walked through Raft's three mechanisms. Elections start when a randomised timeout passes, need a majority of votes, and are restricted to candidates whose logs are at least as up to date, so new leaders hold every committed entry. Replication uses AppendEntries with the previous index and term, stepping back until logs match and then overwriting conflicting suffixes. An entry commits when it is on a majority and from the leader's current term; older-term entries commit only indirectly, which is why leaders append a no-op. Throughout, safety never depends on timing; timing only affects availability.
Open with
A follower that doesn't hear a heartbeat within its randomised election timeout becomes a candidate: it increments the term, votes for itself and sends RequestVote. It needs a majority, and each node grants at most one vote per term, only to a candidate whose log is at least as up to date as its own, comparing the last entry's term and then the length.
Then, if they want more
That guarantees every new leader already has all committed entries. The leader replicates with AppendEntries, which includes the index and term of the entry before the new ones. A follower that doesn't have that entry rejects, and the leader steps back until the logs match, then overwrites the follower's conflicting suffix.
An entry is committed once it's stored on a majority and belongs to the leader's current term, and committed entries are applied in order.
The current-term condition matters because an entry from an earlier term can sit on a majority and still be overwritten: another node whose last entry has a higher term can win a later election and replace it.
So a leader commits older entries only indirectly, by getting an entry of its own term onto a majority, which commits everything before it. That's why leaders append a no-op at the start of their term. And none of the safety depends on timing; the timeouts only affect availability, with heartbeats much shorter than the election timeout.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Walk through how a Raft leader is elected, how entries are replicated and committed, and...": https://scrivio-interview-library.netlify.app/architecture/distributed-systems/8d9d986af4/