How Does the Raft Consensus Algorithm Work?

Three compact servers connected through a network switch in a distributed cluster

How Does the Raft Consensus Algorithm Work? Explainer

Raft is a consensus algorithm that helps several servers behave like one reliable service by agreeing on an ordered log of commands. Each server runs the same deterministic state machine, so applying the same committed commands in the same order leads to the same result. The difficulty is maintaining that order when messages are delayed, machines restart, or a leader disappears. Raft separates the problem into leader election, log replication, and safety rules designed to keep committed history from being lost or rearranged.

At any moment, a server is normally a follower, candidate, or leader. Followers expect periodic messages from a leader. If a follower’s election timer expires, it increments its term, becomes a candidate, votes for itself, and asks the other servers for votes. A candidate that receives a majority becomes leader for that term. Randomized election timeouts reduce the chance that several candidates repeatedly split the vote. Every message carries a term number, and a server that discovers a newer term steps back so the group does not continue following an outdated leader.

Clients send changes to the leader. The leader appends each command to its local log and sends AppendEntries requests to followers. When an entry from the leader’s current term is stored on a majority of the cluster, the leader can mark it committed, apply it to its state machine, and notify followers of the commit position. Followers reject an append whose preceding index and term do not match their own log. The leader then backs up and retries until the follower’s conflicting suffix is replaced by the leader’s history. These matching rules make identical index-and-term pairs represent the same command and the same preceding log.

A majority is what allows Raft to survive a minority of crash failures without two separate histories both becoming committed. In a five-server cluster, three servers form a majority, so the service can usually continue after two servers fail. During a network partition, only a side that can assemble a majority can elect a leader and commit new entries. A former leader isolated with too few peers may accept a client connection temporarily, but it cannot safely commit additional commands; once it learns of a newer term, it returns to follower status and reconciles its uncommitted suffix.

Client behavior and maintenance complete the design. If a client times out after sending a command, it may not know whether the command committed, so practical services attach client identities and request sequence numbers to suppress accidental duplicate execution. Logs also cannot grow forever. A server can compact older committed entries into a snapshot of the state machine and retain enough log information to continue replication safely. Membership changes are especially delicate because two configurations must not form disjoint majorities that decide different histories. Implementations therefore use a defined reconfiguration protocol and persist term, vote, and log state before responding in ways that safety depends on. Read-only operations also require care when a client needs assurance that a response reflects current leadership rather than a stale, isolated server. These details are operational, but they are part of a correct consensus system rather than optional polish.

Raft does not make a distributed system immune to every failure. Disk corruption, flawed state-machine code, Byzantine behavior, misconfigured membership changes, and unsafe client retry logic need separate protections. Committed data still requires durable storage, snapshots or log compaction, and carefully tested recovery. Latency also includes the network trip to a majority, so distant members can slow commits. Raft’s contribution is a disciplined agreement protocol: one leader proposes an order, majorities establish durable overlap between terms, and safety rules ensure a newly elected leader contains the committed history the service must preserve.

Raft lets a cluster agree on a replicated command log by electing a leader and committing entries stored on a majority. Key terms include follower, candidate, leader, term, election timeout, vote, AppendEntries, log index, commit index, quorum, state machine, and snapshot.

A follower whose randomized timer expires becomes a candidate, starts a new term, and requests votes. A majority elects the leader. Servers grant limited votes and compare log freshness so a candidate missing committed history cannot easily replace a more up-to-date one.

Only a partition containing a majority can elect a leader and commit new entries. A smaller side may retain old state but cannot safely advance the log. When communication returns, newer-term leadership and log-matching rules bring followers back into agreement.

Explore more "Explainers"

Discover additional explainers across politics, science, business, technology, and other fields. Each explainer breaks down a complex idea into clear, everyday language—helping you better understand how major concepts, systems, and debates shape the world around us.