Raft Consensus Algorithm
Raft is a consensus algorithm that enables a cluster of nodes to agree on a sequence of values (a replicated log) even when some nodes fail, by electing a single leader that coordinates all changes.
What It Really Means
In a distributed system, you often need multiple nodes to agree on the same data — the current leader of the cluster, the order of transactions, or the state of a configuration. Consensus algorithms solve this problem: they guarantee that all non-faulty nodes eventually agree on the same value, even if some nodes crash or messages are delayed.
Raft was designed by Diego Ongaro and John Ousterhout at Stanford in 2014 specifically to be understandable. Its predecessor, Paxos, is notoriously difficult to understand and implement correctly. Raft achieves the same safety guarantees as Paxos but decomposes the problem into three clean subproblems: leader election, log replication, and safety.
Raft is used in production by etcd (which powers Kubernetes), CockroachDB, TiKV (the storage layer of TiDB), Consul by HashiCorp, and many other systems. If you use Kubernetes, every cluster state change goes through a Raft-based consensus in etcd.
How It Works in Practice
The Three Roles
Every node in a Raft cluster is in one of three states:
- Leader: Handles all client requests, replicates log entries to followers
- Follower: Passive — responds to RPCs from the leader and candidates
- Candidate: Transitional state during leader election
Leader Election
- All nodes start as followers with a randomized election timeout (e.g., 150-300ms)
- If a follower does not hear from a leader before its timeout expires, it becomes a candidate
- The candidate increments its term number, votes for itself, and sends RequestVote RPCs to all other nodes
- A node grants its vote to the first candidate it hears from in that term (first-come, first-served)
- If a candidate receives votes from a majority (quorum), it becomes the leader
- The leader sends periodic heartbeats (empty AppendEntries RPCs) to maintain authority
The randomized timeout is critical — it ensures that in most cases only one node times out first, avoiding split votes. If a split vote does occur, candidates retry with new randomized timeouts.
Log Replication
- Client sends a write request to the leader
- Leader appends the entry to its local log (uncommitted)
- Leader sends AppendEntries RPCs to all followers with the new entry
- Each follower appends the entry to its log and responds with success
- Once a majority of nodes have acknowledged, the leader marks the entry as committed
- Leader responds to the client with success
- Followers learn about the commit in subsequent heartbeats and apply the entry
Real-World: etcd and Kubernetes
Kubernetes stores all cluster state (pods, services, deployments, config maps) in etcd, a distributed key-value store built on Raft. When you run kubectl apply, the API server writes to etcd. The etcd leader replicates the change to a quorum of nodes before acknowledging the write. This guarantees that cluster state is consistent even if an etcd node crashes during the write.
A typical etcd cluster has 3 or 5 nodes. With 3 nodes, the system tolerates 1 failure. With 5, it tolerates 2. Using more than 7 nodes is rare because the cost of replication (leader must wait for majority acknowledgment) grows with cluster size.