← All articles

Understanding Raft · Part 3

Understanding Raft, Part 3: Log Replication and Safety

How a Raft leader replicates its log with AppendEntries, what committed means, and the commit rule from Figure 8 that first implementations miss.

6 min read

From who to what

Part 2 gave the cluster a single leader. Now the leader has to do its job. It takes commands from clients and gets them into every server’s log in the same order, so the state machines stay identical. This is log replication, and the rules that make Raft correct are hidden inside it. It’s the part I’d read twice.

The log

Each entry holds the command, the term in which the leader created it, and its index, the position in the log. The term turns out to matter a lot. It is how servers notice that their logs disagree and how they fix it.

Raft log across a leader and two followers: each entry stores a term and a command; entries replicated on a majority are committed; a lagging follower is missing recent entries and will catch up.

Logs on three servers. Each cell is term · command. Entries stored on a majority are committed and can be applied. Follower 3 fell behind, and the leader will bring it up to date. (Compare Ongaro and Ousterhout 2014, Figure 6.)

The normal path is short. A client sends a command, the leader appends it, and the leader sends AppendEntries to the followers. Once the entry is stored on a majority, the leader marks it committed, applies it to its own state machine and answers the client. Followers learn about the commit in later messages and apply the entry too.

The interesting work is in the other cases, followers that crashed or fell behind, or that hold entries an old leader never committed. Raft repairs all of them with one RPC and one property.

The consistency check

AppendEntries carries more than the new entries. It also carries the index and term of the entry right before them, called prevLogIndex and prevLogTerm. The follower accepts the new entries only if its own log has a matching entry at that position. If not, it rejects the request.

The AppendEntries consistency check: the leader sends the index and term of the preceding entry; if the follower has no matching entry there it rejects, and the leader decrements nextIndex and retries until the logs converge.

A follower accepts new entries only when the entry before them matches in index and term. Otherwise it rejects, and the leader steps back and tries again. (Compare Ongaro and Ousterhout 2014, Figure 7.)

This small check works by induction. An empty log matches any other empty log. Every successful AppendEntries checked that the entry before the new ones already matched, so after the call the follower’s log matches the leader’s up to the last new entry. That gives Raft the Log Matching Property.

If two logs contain an entry with the same index and term, the logs are identical in every entry up to that index.

So one matching position vouches for the whole history before it, and one (index, term) check per message is enough to keep the entire log in line.

Repairing a follower

When a follower rejects, the leader moves its nextIndex for that follower one step back and tries again with an earlier entry. It keeps going back until it finds the last position where the two logs agree. From there it sends every entry the follower is missing and overwrites any conflicting entries the follower had.

Overwriting sounds scary the first time. It is safe because of what committed means, which the next section pins down. The entries a leader overwrites on a follower were never committed, so no state machine applied them and no client was told they succeeded. The leader’s log is the reference and the followers are made to match it. Crystal does the same backtracking, with one improvement from the paper. The follower sends a hint about where the conflict starts, so the leader can jump back a whole term at a time instead of one entry per round trip.

What committed means

“Stored on a majority” is the usual commit condition, and stated that plainly it hides a trap. Say an entry reaches a majority and the leader crashes before committing it. A new leader is elected. Can that entry be overwritten even though a majority once held it? If a committed entry could ever disappear, the cluster would have lied to a client.

Raft closes this with two rules. One limits who can become leader. The other limits what a leader may call committed.

Rule 1. Only an up-to-date candidate can win

In Part 2 I skipped a check in RequestVote. A server refuses its vote unless the candidate’s log is at least as up to date as its own. Up to date has a precise meaning. Compare the term of the last entry first, and if the terms are equal, compare the length of the log.

Every committed entry lives on a majority, and winning also needs a majority, so the winner shares at least one server with the group that holds the entry. The check means the winner is at least as current as that server. So any server that can win an election already has every committed entry. The paper calls this Leader Completeness, and the rest of the safety argument leans on it.

Rule 2. A leader only counts entries from its own term

This is the rule first implementations get wrong. It is tempting to commit an entry the moment it sits on a majority. The paper’s Figure 8 shows why that’s wrong.

An entry from an older term sits on a majority but was never committed. A new leader sees it on a majority and commits it with the naive rule. Then that leader crashes. Another server that never had the entry, but whose log is newer by the Rule 1 comparison, wins the next election and overwrites it. A committed entry is gone.

The fix sounds arbitrary until you have stared at Figure 8 for a while.

A leader may only commit an entry by counting replicas if the entry is from the leader’s current term.

Older entries get committed indirectly, when an entry from the current term is committed after them, and the Log Matching Property takes the whole prefix along. That’s why a new leader appends an empty entry in its own term right after it wins. Committing that entry commits everything before it. Crystal enforces this rule in one place, the function that advances the commit index, and a unit test replays the Figure 8 scenario against it.

The whole argument

Put together, the pieces depend on each other like this.

  • Election safety. One vote per term plus majorities means at most one leader per term (Part 2).
  • Log matching. The consistency check means two logs that agree at one point agree on everything before it.
  • Leader completeness. The vote restriction means a new leader already has every committed entry.
  • The commit rule. Counting only current-term entries means a future leader can never overwrite a committed one.

Chained together they give the property the whole system exists for, State Machine Safety. If any server applied an entry at some index, no server will ever apply a different entry at that index.

Next, the real thing

That is the complete algorithm on paper. The paper assumes a log in memory that never loses data and never stops growing, and a real database on real disks has neither. Part 4 is about Crystal itself: a write-ahead log that survives crashes, snapshots that keep the log small, and a single control loop that keeps the concurrency manageable.


Reference: Diego Ongaro and John Ousterhout, “In Search of an Understandable Consensus Algorithm,” USENIX ATC 2014, especially sections 5.3 and 5.4 and Figure 8. The diagrams are mine. Where one follows a figure from the paper, the caption says so. The code is in Crystal.