August 22, 2024 4 min read A to Z Series: R Home

Raft Consensus: Leader and Followers in Plain English

Raft is a consensus method where a leader coordinates followers so a cluster can agree on one replicated log.

I recommend Raft as the first consensus algorithm to learn because the leader and follower model matches how many engineers already reason about systems. It makes the hard parts visible without burying the idea in math.

The leader makes ordering visible

I use this section to force the failure case into the conversation, not just the clean path.

Clients send commands to the leader. The leader appends each command to its log and sends it to followers. When a majority has the entry, the leader commits it and tells followers to apply it.

Leader election example

This is where the concept becomes useful to me: it has to explain what a team does during a bad day.

A configuration store changes the timeout value from 3 seconds to 5 seconds. The leader writes the change to its log, followers copy it, and the cluster commits the new setting.

Raft state code

I use the code here to show the decision boundary, not just syntax.

The leader commits only after enough followers have the entry:

LogEntry entry = log.append(command);
int acknowledgements = followers.replicate(entry);

if (hasQuorum(acknowledgements + 1, cluster.size())) {
  log.commit(entry.index());
}

The leader appends the command, replicates it, and commits only after quorum. Without that majority, the entry is not safe to treat as durable cluster state.

Sequence diagram: commit through the leader

commit through the leader The command becomes real after a majority has the log entry, not merely when the leader receives it. sd commit through the leader Client Client Raft Leader Raft Leader Follower A Follower A Follower B Follower B Cluster Cluster Submit configurationchange. Replicate the log entry. Replicate the log entry and receive majorityack. Commit and apply the configuration change.

The command becomes real after a majority has the log entry, not merely when the leader receives it.

The agreement boundary

Continue with coordination

Written by Arunkumar Ganesan.

What I learnt is that consensus is not about being clever; it is about making several machines behave like one careful decision maker.

#DistributedSystems #Raft #Consensus #Architecture