What is Raft

Raft is a consensus algorithm designed to be understandable. It solves the same problem as Paxos — getting multiple nodes to agree on a sequence of values — but decomposes it into three clean sub-problems: leader election, log replication, and safety. etcd, Consul, CockroachDB, and TiKV all use Raft internally.

How it works

Every Raft node is in one of three states: leader, follower, or candidate.

Leader election. One node is the leader at any time. The leader sends periodic heartbeats to followers. If a follower stops receiving heartbeats (the leader crashed or is partitioned away), it becomes a candidate and starts an election. It increments the term number (a logical clock) and requests votes from other nodes. If it receives votes from a majority, it becomes the new leader. Randomized election timeouts prevent split votes.

Log replication. The leader receives client requests and appends them to its log. It sends the new entries to all followers. A follower appends the entry to its own log and acknowledges. Once a majority of nodes have acknowledged an entry, the leader commits it — the entry is durable and applied to the state machine. Followers learn about the commit and apply it to their own state machines.

Safety. Raft guarantees that committed entries are never lost. A candidate cannot win an election unless its log is at least as up-to-date as a majority of nodes. This ensures the new leader always has all committed entries. Entries are never overwritten — only appended.

The result is a replicated state machine. Every node applies the same log entries in the same order, so their state machines converge to identical states. If you build a key-value store on top of Raft, every node has the same keys and values.

Why it matters

Raft is the consensus algorithm you are most likely to encounter in practice. Understanding its leader election and log replication explains how etcd keeps Kubernetes configuration consistent, how CockroachDB replicates transactions, and why a 3-node cluster tolerates exactly 1 failure.

See How Consensus Works for the full Raft walkthrough with term diagrams and failure scenarios.