Raft Log Replication and Safety

Topics Covered

Replicating Entries

The structure of the log

The AppendEntries RPC

The log matching property

Why the invariant matters

Pipelining and batching

The leader writes its own log in parallel

Ordering guarantees for clients

A command from start to finish

What a rejection tells the leader

Heartbeats and entries share one RPC

Entry size matters

Flow control

Once a leader is elected, it spends almost all of its time replicating log entries. Every client command becomes an entry in the leader log, the leader sends the entry to followers, and once a majority has stored it, the entry is committed and can be applied to every state machine. This section explains the structure of the log, the AppendEntries RPC and the consistency check that keeps all logs identical where it matters.

The structure of the log

The Raft log is a sequence of entries indexed from one. Each entry holds a command for the state machine and the term in which the leader received it. The term is the crucial piece of metadata: it identifies which leader created the entry and lets servers detect inconsistencies between logs. Entries are appended at the end and, on followers, may occasionally be deleted from the end when they conflict with the leader, but an entry that is committed is never deleted or changed on any server. Each server also tracks a commit index, the highest index known to be committed, and a last applied index, the highest index applied to its state machine.

The AppendEntries RPC

The leader replicates entries with AppendEntries. Each call carries the leader term, the leader identifier, the index and term of the entry immediately preceding the new ones, called prevLogIndex and prevLogTerm, a list of new entries, possibly empty, and the leader commit index. The follower first rejects the call if its term is higher than the leader term. Otherwise it checks whether its own log has an entry at prevLogIndex whose term matches prevLogTerm. If not, it rejects the call. If so, it deletes any existing entries that conflict with the new ones, appends any new entries it does not already have and advances its own commit index to the minimum of the leader commit index and the index of its last new entry.

Every AppendEntries names the entry just before the new ones. A follower appends only if its log agrees at that point.

The log matching property

The consistency check gives Raft a powerful invariant called the log matching property. If two logs contain an entry with the same index and term, then the logs are identical in all entries up to and including that index. The argument is by induction. A leader creates at most one entry per index in a given term and never changes it, so two entries with the same index and term carry the same command. And because a follower accepts new entries only if its entry at prevLogIndex matches prevLogTerm, every accepted entry is preceded by an entry the leader also has, and so on back to the beginning of the log.

Why the invariant matters

Log matching means servers never need to compare whole logs. To know whether a follower agrees with the leader up to index 1,000, it suffices that the follower has an entry at index 1,000 with the same term as the leader entry. This makes the consistency check a single comparison per RPC, regardless of log length. It also gives a simple way to describe how far two logs agree: the highest index at which they share an entry with the same term. Everything before that point is identical, and everything after it may differ.

Pipelining and batching

Sending one entry per RPC and waiting for each acknowledgment would limit throughput to one entry per round trip. Real implementations batch many entries into a single AppendEntries and pipeline requests, sending the next batch before the previous one is acknowledged. The leader tracks the next index to send each follower optimistically and falls back if a follower rejects. Combined with group commit on disk, where many entries are persisted with a single fsync, this lets a Raft group handle tens of thousands of entries per second even though each commit still costs a round trip and a disk sync.

The leader writes its own log in parallel

A naive leader would persist an entry to its own disk before sending it to followers, adding a full disk sync to the critical path. The Raft dissertation notes that the leader can write to its own disk in parallel with sending to followers, and count itself toward the majority once its own write completes. With three servers, a commit then needs the faster of two disk syncs plus a round trip, rather than the leader sync followed by a follower sync. etcd and other implementations use this optimization, which can noticeably reduce commit latency on slower disks.

Ordering guarantees for clients

Because the leader assigns indexes in the order it receives commands, and committed entries apply in index order, the replicated state machine sees commands in a single total order consistent with the order the leader received them. Two commands from the same client connection, sent one after another, apply in that order. Commands from different clients are ordered by arrival at the leader. This total order is what makes Raft a linearizable foundation, provided reads are also handled carefully, as discussed at the end of this lesson.

A command from start to finish

Follow a single command through the system. A client sends set x to 5 to the leader. The leader appends an entry at index 101 with its current term, 6, and starts writing it to its own disk. In parallel it sends AppendEntries to each follower with prevLogIndex 100, prevLogTerm 6 and the new entry. Each follower checks that its entry at 100 has term 6, appends entry 101, syncs it to disk and replies with success. When the leader has its own write done and one follower success, in a three server cluster, it has a majority, so it advances its commit index to 101, applies set x to 5 to its state machine and replies to the client. On the next heartbeat, followers see leader commit 101 and apply the entry themselves. Every step of the safety argument later in this lesson is about making this simple path stay correct when servers crash at arbitrary points along it.

What a rejection tells the leader

A rejection means either that the follower has seen a higher term, in which case the leader steps down, or that the logs disagree at prevLogIndex. The leader distinguishes these by the term in the reply. A log mismatch is not an error; it is the normal signal that the follower needs repair, which the later section on repairing logs explains. Treating mismatches as failures and retrying the same request unchanged, a mistake in some early implementations, leaves the follower permanently stuck.

Heartbeats and entries share one RPC

Using the same RPC for heartbeats and replication is a deliberate simplification. Every heartbeat also carries the consistency check and the commit index, so a follower that missed a message learns it is behind at the next heartbeat, and commitment information reaches followers even when no new entries arrive. A separate heartbeat message would need its own rules and could disagree with the replication stream. One RPC with one set of rules keeps the protocol small.

Entry size matters

Raft treats entries as opaque, but their size shapes performance. Small entries, a few hundred bytes, are dominated by per entry overhead and benefit most from batching. Large entries, such as megabyte values, consume leader bandwidth for every follower and inflate memory, since entries stay in memory until they are applied and possibly until a snapshot. etcd limits request size to about one and a half megabytes by default for this reason, and systems that need large values store them elsewhere and replicate only references.

Flow control

A leader that sends entries faster than a slow follower can persist them will pile up messages in memory. Implementations cap the number of unacknowledged messages or bytes per follower and switch a follower into a probing mode, sending one message at a time, after a rejection or a long silence, returning to fast pipelined replication once the follower acknowledges again.

Log matching by induction

Each AppendEntries names the index and term of the preceding entry, and followers accept only if they match. By induction, two logs that share an entry with the same index and term are identical up to that point, so one comparison checks agreement.