Raft is a consensus algorithm that keeps a replicated log identical across a group of servers, so that every node applies the same commands in the same order even when some machines crash or the network drops messages. In database replication, Raft is the mechanism that decides which node accepts writes, when a write is durably committed, and how a new leader is chosen after a failure.
Raft was published in 2014 by Diego Ongaro and John Ousterhout as an understandable alternative to Paxos. It splits consensus into three separable problems: leader election, log replication and safety. Systems such as etcd, Consul, CockroachDB, TiKV, YugabyteDB and the KRaft mode of Apache Kafka use Raft (often called Raft consensus) to replicate metadata or user data in databases and distributed storage.
How Raft replicates a log
Raft replicates a log by routing every write through a single elected leader, which appends the command to its own log and then sends it to followers with AppendEntries messages. An entry becomes committed once a majority of the cluster has stored it, and only committed entries are applied to the state machine, which in a database is the storage engine or the key-value store.
Each log entry carries an index and a term number. Terms are logical clocks: every election starts a new term, and any node that sees a higher term immediately steps down to follower. The log matching property guarantees that if two logs contain an entry with the same index and term, the logs are identical up to that point, because each AppendEntries call includes the index and term of the preceding entry and followers reject mismatches.
The table below lists the three roles a Raft node can hold and what each role does.
| Role | Responsibility | Typical count |
|---|---|---|
| Leader | Accepts client writes, replicates entries, sends heartbeats | Exactly one per term |
| Follower | Stores entries passively, votes in elections | All other healthy nodes |
| Candidate | Requests votes after an election timeout | Temporary, during elections only |
Leader election and terms
Raft elects a leader when a follower stops receiving heartbeats within its election timeout, usually a randomized value between 150 and 300 ms in the original design and often one to several seconds in production. The follower increments its term, becomes a candidate, votes for itself and sends RequestVote messages; the first candidate to collect votes from a majority becomes leader for that term.
Randomized timeouts make split votes rare, because one node usually times out first and wins before others start campaigning. The election restriction ensures safety: a node grants its vote only if the candidate’s log is at least as up to date as its own, so a newly elected leader always holds every committed entry. Many implementations add a PreVote phase so that a partitioned node cannot disrupt a healthy cluster by inflating the term after it reconnects.
The following parameters control how quickly a Raft group detects failure and how stable it remains.
| Parameter | Typical range | Effect of a higher value |
|---|---|---|
| Heartbeat interval | 50 ms to 500 ms | Less network traffic, slower failure detection |
| Election timeout | 150 ms to 10 s | Fewer false elections, longer write outage |
| Snapshot threshold | 10,000 to millions of entries | Larger logs, less frequent snapshot work |
| Max entries per append | Tens to thousands | Better throughput, larger message bursts |
The election timeout must be much larger than the round-trip time between nodes; a common rule is at least ten times the heartbeat interval. Setting it too low across regions produces leader flapping, in which the cluster spends more time electing than serving writes.
Quorum, fault tolerance and cluster size
A Raft cluster of N nodes tolerates the failure of (N-1)/2 nodes, because every commit and every election requires a majority quorum. Any two majorities overlap in at least one node, and that overlap is what prevents two leaders from committing conflicting entries in the same term and rules out split-brain.
| Cluster size | Quorum size | Failures tolerated |
|---|---|---|
| 3 nodes | 2 | 1 |
| 4 nodes | 3 | 1 |
| 5 nodes | 3 | 2 |
| 7 nodes | 4 | 3 |
Odd cluster sizes are the norm because adding a fourth node raises the quorum without improving fault tolerance. Larger groups also slow writes, since the leader must wait for more acknowledgments. Production systems therefore run three or five voting members per Raft group and add non-voting learners or read replicas when they need more read capacity.
Membership changes use joint consensus or, in most modern implementations, single-server changes that add or remove one node at a time, so that the old and new configurations can never form disjoint majorities.
Raft compared with other replication approaches
Raft differs from classic primary-replica setups because failover is built into the protocol rather than handled by an external tool. The comparison below places Raft next to related techniques in the replication topic.
| Method | Commit rule | Failover | Main trade-off |
|---|---|---|---|
| Raft | Majority acknowledgment | Automatic, built in | Write latency of one quorum round trip |
| Multi-Paxos | Majority acknowledgment | Automatic, design varies | Harder to understand and implement |
| Asynchronous replication | Primary only | External tooling | Possible data loss on failover |
| Synchronous replication | All or named replicas | External tooling | One slow replica blocks writes |
| Multi-primary replication | Local node | Not needed for writes | Conflict resolution required |
Compared with semi-synchronous replication, Raft gives a formal safety guarantee: no committed entry is ever lost while a majority survives. Compared with Multi-Paxos, Raft is functionally similar but enforces a strong leader and contiguous logs, which simplifies reasoning and testing.
Raft in practical database deployments
Distributed SQL and key-value databases rarely run one Raft group for the whole dataset. They split data into ranges or regions, often 64 MB to 512 MB each, and run a separate Raft group per range. This design, sometimes called Multi-Raft, spreads leadership across nodes so that write load is balanced, and it lets the system move or split ranges independently.
Reads need care. A leader can serve a linearizable read only after confirming it is still leader, either through a ReadIndex round of heartbeats or through a time-based leader lease that relies on bounded clock drift. Reads from followers are cheaper but may return stale data unless the follower first obtains the current commit index from the leader.
- Log compaction: snapshots replace old log entries so that disk usage and restart time stay bounded.
- Pipelining and batching: the leader sends new entries before earlier ones are acknowledged to raise throughput.
- Geo-replication: placing voters in three regions survives a regional outage but adds cross-region latency to every commit.
Limitations and common mistakes
Raft guarantees safety, not unlimited availability: without a majority, the cluster stops accepting writes. The leader is also a throughput bottleneck for its group, and every write pays at least one network round trip to a quorum plus a durable disk flush.
| Symptom | Likely cause | Remedy |
|---|---|---|
| Frequent leader changes | Election timeout too short or slow disks | Raise timeout, use faster fsync storage |
| Writes stall completely | Quorum lost after node failures | Restore nodes or recover membership |
| High commit latency | Voters spread across distant regions | Place a majority in nearby zones |
| Stale reads | Reads served by followers without ReadIndex | Use leader reads or read index |
A frequent mistake is treating a two-node cluster as highly available; it tolerates no failures at all. Another is disabling fsync for speed, which breaks the durability assumption that Raft’s safety proof depends on and can lose committed data after a power failure.
Explained without jargon
Imagine five clerks who must keep identical copies of the same ledger. One clerk is chosen as the coordinator. Every new entry goes to the coordinator first, who reads it out to the others. As soon as at least three of the five have written it down, the entry counts as final, and everyone applies it in the same order.
If the coordinator falls silent, the others wait a short, slightly different time, and the first one to notice asks for votes. Whoever gets a majority becomes the new coordinator. Raft is simply a precise set of rules for this routine, so that nobody ever loses a final entry as long as most clerks are still working.
A typical use case
An online payments team runs a distributed SQL database across three availability zones in one cloud region. Each data range is replicated by its own Raft group with three voters, one per zone, and the election timeout is set to about three seconds to avoid false failovers caused by short network hiccups.
During a maintenance window, one zone loses power. The Raft groups whose leaders lived in that zone hold new elections, and within a few seconds the surviving nodes in the other two zones become leaders. Because each group still has two of three voters, writes continue, and no committed transaction is lost. When the zone returns, its nodes catch up from snapshots and the log, and the team observes only a brief rise in write latency.