Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk11 min

How to Come Up With the Raft Consensus Algorithm Yourself

A step-by-step walkthrough of how Raft consensus is built from first principles: why one leader orders the log, how terms and elections replace it, why commits need the current-term rule, and how membership changes and snapshots fit in.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Raft is the answer you reach when you ask how several servers can agree on one sequence of commands even though any of them may crash. Start with one rule: a single server, the leader, orders every new command, and the others copy its log. Everything else in Raft exists to keep that arrangement correct through failures. Numbered terms and elections replace a leader that goes silent. Each new entry is checked against the entry before it. A voting rule stops a new leader from missing work that was already committed. The paper’s abstract describes the result as “Raft is a consensus algorithm for managing a replicated log” (Diego Ongaro and John Ousterhout, 2014). It solves replicated-log agreement, not every distributed-systems problem. The steps below follow the order in which a designer would hit each problem.

Start with identical copies of one state machine

A single server that holds your data is a single point of failure, so you add copies. Copies drift apart unless they agree on something precise. Suppose every server starts from the same empty state and runs the same deterministic state machine, meaning the same input always produces the same output. If each server applies the same commands in the same order, they end up with identical state. The group keeps working while some servers are down, as long as enough of them survive.

As an Amazon Associate I earn from qualifying purchases.

That reduces the whole problem to one question: how do the servers agree on a single sequence of commands? That sequence is the replicated log. Each command gets a position, called an index, and the state machine applies entries in index order. Agreeing on the log is what consensus means here.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The failure model determines what the rules must survive. Servers may crash and restart, and messages may be delayed, lost, or reordered. Servers are assumed not to lie. Raft is built for that setting, not for servers that might send false information.

Why route every change through one leader

Imagine two clients acting at the same moment. One asks for x = 1 and the other for x = 2, and each reaches a different server first. If every server orders commands by whatever arrives first locally, the servers will disagree about which write came last. Agreeing on each log position by voting is possible, but it has to happen for every single command, and it is the hard part of consensus.

Raft takes a simpler route. One server, the leader, accepts all client changes, assigns each one the next log position, and sends it to the others. Followers do not make ordering decisions; they copy the leader’s log. The normal path becomes append, replicate, then apply once the entry is safe. That path is much easier to reason about than a design in which every server orders independently.

The cost is that the leader is now a single point of control, so the design needs a way to replace it. That is the job of terms and elections.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Replace a failed leader with terms and elections

At any moment, every server is in one of three roles.

Role What it does How it changes
Follower Accepts entries from the leader and votes in elections Becomes a candidate when its election timeout passes without hearing from a leader
Candidate Asks the other servers for votes for itself Becomes leader after a majority votes for it; returns to follower if it hears from a valid leader or a newer term
Leader Accepts client commands, replicates the log, and sends heartbeats Steps down when it sees a higher term

Terms as a logical clock

Time in Raft is divided into numbered terms. Each term begins with an election, and each term has at most one leader. A term can also end with no leader if an election splits the vote; the next election starts a higher-numbered term. Every server stores its current term and updates it whenever it sees a larger one. A server that sees a higher term adopts it and becomes a follower. That single rule lets a stale leader discover it has been replaced, without any extra protocol.

How an election runs

  1. A follower receives no message from a leader for the length of its randomized election timeout.
  2. It increments its current term, becomes a candidate, and votes for itself.
  3. It sends RequestVote to every other server in parallel.
  4. Each server grants at most one vote per term, to the first candidate whose request passes the log comparison described under the election restriction below.
  5. A candidate that collects votes from a majority of the cluster becomes leader and immediately sends heartbeats.
  6. If no candidate wins before the timeout, the candidate starts another election in a higher term.

Why election timeouts are randomized

If every follower used the same timeout, they would often become candidates together, split the vote, and time out together again. A random timeout for each server makes it likely that one server starts first and wins before the others wake up. This reduces repeated split votes. It does not eliminate them, so an election can still fail and run again.

Heartbeats keep followers from starting elections

The leader sends AppendEntries messages periodically, even when it has nothing new to send. Each one resets the followers’ election timers. While heartbeats arrive comfortably inside the timeout, nobody starts an election. A missing heartbeat is how followers detect a failed leader, so the heartbeat interval and the election timeout must be chosen together.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Copy the log with a consistency check

A log entry carries three things: its index, the term in which the leader created it, and the command. The leader appends a client command to its own log, then sends each follower an AppendEntries request containing the entries that follower lacks. The request also carries prevLogIndex and prevLogTerm, which identify the entry immediately before the new ones.

The follower accepts the request only if it already has an entry at prevLogIndex with term prevLogTerm. This prefix check is the core of replication. If it fails, the follower rejects the request, the leader steps back one position for that follower, and it retries until the two logs agree at some point.

When the check passes, the follower compares the new entries with its own. If an existing entry at the same index has a different term, the logs have diverged there. The follower deletes that entry and everything after it, then appends the leader’s entries. Only uncommitted entries are ever removed this way; the commitment and election rules below ensure that committed entries never conflict.

A worked example

Suppose the leader’s log holds terms 1, 1, 2, 2 at indexes 1 through 4, and a follower holds terms 1, 1, 1 at indexes 1 through 3. The leader sends entries 3 and 4 with prevLogIndex 2 and prevLogTerm 1. The follower has index 2 with term 1, so the check passes. Its entry at index 3 has term 1 rather than 2, so it is replaced. Afterward both logs read 1, 1, 2, 2.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Now suppose the follower holds only index 1. The leader’s first request, with prevLogIndex 2, is rejected because the follower has no entry there. The leader steps back to prevLogIndex 1, which matches, and sends entries 2 through 4 in one request.

These checks maintain the Log Matching property stated in the paper: if two logs contain an entry with the same index and term, the logs are identical in every entry up to that index.

When an entry counts as committed

An entry is committed once it is stored on a majority of servers in a way that guarantees it will survive future leader changes. Only committed entries may be applied to the state machine, and they are applied in index order. Followers learn the commit point from the leaderCommit field of later AppendEntries requests.

The naive rule and where it breaks

The obvious rule is that once a leader sees an entry on a majority, the entry is committed. A five-server scenario shows why Raft needs a refinement.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. In term 2, server S1 is leader. It writes entry E at index 2 and copies it to S2 only, then crashes.
  2. In term 3, S5 wins an election with votes from S3 and S4, whose logs end in term 1. S5 writes its own term-3 entry at index 2 and crashes before replicating it.
  3. S1 recovers and wins term 4 with votes from S2 and S3. It copies E to S3, so E now sits on S1, S2, and S3, a majority.
  4. If S1 treated E as committed, it would be wrong. S5 recovers with a last entry from term 3, which is newer than the term-2 entries on S2 and S3. It can win term 5, and its term-3 entry would overwrite E on the servers that stored it.

The current-term rule

Raft adds one condition: a leader advances the commit point by counting replicas only for an entry from its own current term. In the scenario above, S1 in term 4 does not count E by replica count. Instead it appends a new term-4 entry at index 3 and replicates that. Once the term-4 entry is on a majority, it is committed, and E at index 2 becomes committed with it, because committing an index commits every entry before it. By then S5’s last entry from term 3 is older than the last entries on a majority, so S5 cannot collect enough votes.

The commit rule is therefore not “any entry on a majority is committed.” Earlier entries become committed indirectly, when a current-term entry after them is committed.

Why a majority alone is not enough: the election restriction

Counting replicas protects committed entries only if every future leader already contains them. Raft enforces this through the vote. A server grants its vote only if the candidate’s log is at least as up to date as its own. Logs are compared by their last entries:

  • The log whose last entry has the higher term is more up to date.
  • If the last terms are equal, the longer log is more up to date.

Suppose an entry is committed, which means a majority of servers store it. Any candidate that wins also needs a majority of votes. The two majorities overlap in at least one server, and that server holds the committed entry. It refuses any candidate whose log is less current than its own. The paper proves, by induction over terms, that a candidate whose log passes this comparison must already contain every entry committed in earlier terms. This is the leader-completeness property: every leader holds every entry committed before its term.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Both halves are needed. Without the majority requirement, a candidate could win with too few supporters and miss committed work. Without the up-to-date comparison, a stale candidate could win a majority and overwrite entries that were already committed. Leaders also never overwrite or delete entries in their own logs.

Change membership without two independent majorities

Adding or removing servers looks harmless, but switching the configuration all at once is dangerous. Servers do not switch at the same instant. For a while, part of the cluster may still use the old membership to form a majority, while another part has already moved to the new membership. Those two majorities need not overlap, so they could elect two different leaders for the same term.

The extended paper’s answer is joint consensus. During the transition, a configuration called Cold,new requires that decisions, including elections and commitment of log entries, gain a majority of the old configuration and a majority of the new one. Because every decision needs both, no two independent majorities can act.

The transition sequence

  1. The leader appends a joint-configuration entry to its log and replicates it. From that point, the joint rules apply.
  2. Once the joint entry is committed under those rules, the leader appends the entry for the new configuration.
  3. When the new configuration is committed, servers that are not part of it can be shut down. A leader that is not in the new configuration steps down at that point.

The paper also suggests adding new servers as non-voting members first, so they can catch up on the log before they count toward majorities. This avoids stalls caused by a server that is far behind.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Snapshots keep the log from growing without limit

The log grows with every command, so a server cannot keep it indefinitely. The paper describes snapshots: a server periodically saves its state machine’s current state together with the index and term of the last entry that state reflects, and then discards the log entries up to that point.

The metadata matters. The entry right after a snapshot still needs a prevLogIndex and prevLogTerm for the consistency check, and the snapshot’s last index and term supply them even though the earlier entries are gone. When a leader finds that a follower is behind the start of its retained log, it sends the snapshot through an InstallSnapshot request, then resumes normal replication.

Safety does not depend on timing; progress does

The properties described so far, namely that servers apply the same commands at the same index, that committed entries survive leader changes, and that logs converge, hold however slow the network is. Timing cannot make Raft unsafe. It can prevent Raft from making progress.

Progress requires a stable leader that stays in charge long enough to do work. The paper’s guidance is that typical communication and processing times should be comfortably shorter than the election timeout, and the election timeout should be comfortably shorter than the time between server failures. If messages routinely take close to the timeout, followers keep concluding that the leader is gone, elections repeat, and the cluster spends its time electing instead of committing. The system pauses without corrupting its history.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The timing values in the paper’s evaluation are context for those experiments, not default settings for production systems. Choose timeouts from the latency and failure rates of your own environment.

How Raft compares with Paxos

Paxos is the standard reference point. The authors state that Raft is equivalent to (multi-)Paxos in result and comparable in efficiency, while its structure is easier to understand. That is their characterization, reported in the extended paper, not a universal verdict.

Aspect Raft Paxos, as the paper frames it
Result Authors state equivalence with (multi-)Paxos The reference algorithm for the comparison
Efficiency Authors describe it as comparable to (multi-)Paxos Same comparison
Structure Decomposed into leader election, log replication, and safety Basic Paxos agrees on one value; multi-Paxos extends it to a log
Leadership Explicit leader, with terms governing changes Basic Paxos does not require a leader; multi-Paxos commonly uses one
Learnability evidence In a user study, 33 of 43 students answered more Raft questions correctly than Paxos questions Same study, same students, after learning both algorithms

The study involved 43 students at two universities, as reported by the authors. It is a specific classroom-style result, not an estimate for all engineers, and it does not show that Raft is easier in every implementation context.

What a faithful implementation must add

The paper explains the algorithm; it is not a drop-in implementation. Understanding the rules is necessary but not sufficient. Before relying on your code, check that it handles these points:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Persist currentTerm, votedFor, and the log to stable storage before replying to any RPC. Without a durable votedFor, a restarted server could vote twice in one term.
  • Reject stale terms, and tolerate retried or duplicated RPCs without corrupting the log.
  • Apply committed entries to the state machine in index order, exactly once.
  • Implement snapshot transfer and membership transitions as separate paths, each tested under crashes and restarts.
  • Choose election timeouts from measured latency in your own environment, not from the paper’s values.

Primary sources

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.