Sub topic 7
When an entry is committed, and why it is safe
This chapter assumes you know Raft's roles and terms, how RequestVote elects a leader, and how AppendEntries replicates log entries. Here you learn when a leader may call an entry committed, and why committed entries never disappear.
What "committed" means
An entry is committed when Raft guarantees it will be in the log of every future leader. Once an entry is committed, the state machine can safely apply it and the server can reply to the client.
Replicating an entry to a few servers is not enough. A leader can store an entry on a minority of servers and then crash. A new leader may never see that entry, and it can be overwritten. Only an entry stored on a majority is protected.
The commit rule
The leader tracks match_index, the highest log index known to be stored on each follower. The leader's own last index counts too. It advances commit_index to the highest index n that meets both conditions:
- A majority of servers have
match_index >= n. - The entry at index
nhas the leader's current term.
def advance_commit_index(current_term, commit_index, log_terms, match_index):
# log_terms[i - 1] is the term of entry i (entries are numbered from 1).
# match_index has one value per server, including the leader's own last index.
cluster_size = len(match_index)
for n in range(len(log_terms), commit_index, -1):
replicated = sum(1 for m in match_index if m >= n)
if replicated * 2 > cluster_size and log_terms[n - 1] == current_term:
return n
return commit_index
# Five servers. The leader is in term 3 and holds entries 1..4.
terms = [1, 2, 3, 3]
print(advance_commit_index(3, 0, terms, [4, 4, 3, 1, 1])) # 3The loop starts at the end of the log and counts down, so it finds the highest qualifying index. In the example, entry 4 is on only two servers, which is not a majority of five. Entry 3 is on three servers and has term 3, so the result is 3. Entries 1 and 2 are committed indirectly, because the log matching property says that everything before a committed entry is identical on every server holding it.
The leader sends its commit_index in later AppendEntries calls (as leader_commit). Followers advance their own commit index to min(leader_commit, index of last new entry).
Why only current-term entries are counted
Condition 2 is the rule people most often get wrong. A leader must not commit an entry from an earlier term just because it sits on a majority.
The Raft paper gives a scenario, shown here as a timeline.
Entry 2 (term 2) is now on a majority, yet a later leader can still erase it. The election rule compares last log terms, and a server whose last entry has a higher term wins. So counting replicas is unsafe for old entries.
The fix is that a leader counts replicas only for entries from its own term. When such an entry commits, all earlier entries commit with it. A new leader often appends a no-op entry in its own term right after election so earlier entries can commit sooner.
Why committed entries survive elections
Safety comes from two rules working together:
- Commit needs a majority. A committed entry sits on more than half the servers.
- Voting needs an up-to-date log. A server grants its vote only if the candidate's log is at least as up to date as its own. The comparison uses the last log term first, then the last log index.
Any majority of voters overlaps any majority that stored the entry, so at least one voter holds the committed entry. That voter refuses any candidate whose log is behind it. A candidate missing the entry cannot win, so every leader has every committed entry. This is the Leader Completeness property.
def handle_request_vote(s, req):
# s.log[0] is a sentinel entry with term 0, so real entries start at index 1.
if req.term < s.current_term:
return (s.current_term, False)
if req.term > s.current_term:
s.current_term = req.term
s.voted_for = None
s.role = "follower"
last_index = len(s.log) - 1
last_term = s.log[last_index].term
# Compare last log term first, then last log index.
candidate_up_to_date = (req.last_log_term, req.last_log_index) >= (last_term, last_index)
can_vote = s.voted_for is None or s.voted_for == req.candidate_id
if can_vote and candidate_up_to_date:
s.voted_for = req.candidate_id
s.persist() # currentTerm and votedFor must be durable before replying
s.reset_election_timer()
return (s.current_term, True)
s.persist()
return (s.current_term, False)The tuple comparison on the candidate_up_to_date line implements the "term first, then index" rule.
Common errors
- Counting the leader's own log toward the majority is correct, but forgetting to include it makes commits too slow or impossible in small clusters.
- Comparing only the last log index in the vote check, instead of the term first.
- Treating "replicated to a majority" as "committed" without the current-term check.
Exercise
A five-server cluster has a term 5 leader. Its log terms for entries 1 to 4 are [2, 4, 4, 5]. The matchindex values are [4, 3, 3, 1, 1]. What does advancecommitindex return when commitindex is 0, and why?
Show answer
It returns 0. Entry 4 (term 5) is on only one server, not a majority. Entry 3 is on three servers, which is a majority, but its term is 4, not the current term 5, so it cannot be committed by counting. The leader must wait until an entry from term 5 reaches a majority. Then entry 3 commits along with it.