Skip to content
learn
  1. Learn
  2. High Level System Design
  3. Distributed Systems Theory

System Design HLD-08.06

Consensus: Raft

How Raft keeps a replicated log identical across a cluster: terms, leader election, log replication, commit by majority, and the safety rules.

Advanced 18 min Last reviewed

The big idea

ConsensusGetting a group of nodes to agree on a single value, or a sequence of values, even when some nodes fail or messages are lost. is the problem of getting several machines to agree, even when some crash and messages are lost. Its most useful form is agreeing on a log: an ordered list of commands. If every node applies the same commands in the same order to the same starting state, every node ends in the same state. That is a replicated state machineSeveral copies of a deterministic program that apply the same log of commands in the same order, so they stay identical., and it is how etcd, Consul, CockroachDB, and Kafka's KRaft controllers keep their critical data identical across servers and survive the loss of a minority of them. Stock exchanges use the same idea for order books: a matching engine replays one sequenced journal so a hot standby always holds an identical book.

Raft is the consensus algorithm most systems use today, because it was designed to be understandable. It splits consensus into three parts: elect a leader, have the leader replicate its log, and follow a few safety rules that guarantee a committed entry is never lost or changed.

Think of a committee keeping minutes. One chairperson writes each decision into the minutes and reads it out; a decision is final once a majority has written it into their own copy. If the chair leaves, the committee elects whoever has the most complete minutes as the new chair.

How it works

Step 1: Roles and terms

Every node is a follower, a candidate or the leader. Time is divided into numbered terms, each beginning with an election ( Quorums and leader election ). Followers expect regular heartbeats from the leader; if one hears nothing for its randomized election timeout, it becomes a candidate, increments the term, and asks for votes. A majority makes it leader for that term.

Step 2: Log replication

Raft log replication and commit A client sends a command to the leader. The leader appends it to its log as entry 7 in term 3 and sends AppendEntries to four followers. Three followers store it and acknowledge. With its own copy that is four of five, a majority, so entry 7 is committed and applied, and the leader answers the client. One slow follower catches up later. SET x = 5 AppendEntries Client Leader log: ... 6 | 7 (term 3) Follower 1 stored 7 Follower 2 stored 7 Follower 3 stored 7 Follower 4 slow, catches up Figure 1. The leader appends the command, replicates it with AppendEntries, and commits it once a majority (here 4 of 5) has stored it. Only then is it applied and acknowledged. The slow follower catches up later.
  1. A client sends a command to the leader, which appends it to its log with the current term.
  2. The leader sends the entry to followers in an AppendEntries message, along with the index and term of the entry just before it.
  3. A follower accepts only if its log matches at that previous position; otherwise it rejects, and the leader steps back and resends earlier entries until the logs match. This keeps all logs identical up to any point they agree on.
  4. Once a majority has stored the entry, the leader marks it committed, applies it to its state machine, replies to the client, and tells followers the new commit index so they apply it too.

Step 3: Safety

Two rules make committed entries permanent:

  • Election restriction: a node only votes for a candidate whose log is at least as up to date as its own (higher last term, or same last term and at least as long). Since a committed entry is on a majority, and a winner needs a majority of votes, the winner must have every committed entry.
  • Commit rule: a leader only counts replicas to commit entries from its own term; earlier entries become committed indirectly. This closes a subtle case where an old entry on a majority could still be overwritten.

Try it

This interactive needs JavaScript. The diagrams and text above cover the same idea.

Try this:

  1. Wait for a leader, then press Client write a few times. Entries appear on all logs and turn solid (committed) once a majority holds them.
  2. Kill the leader. Followers' timeout rings run down; one becomes a candidate, wins a majority, and a new term begins. Committed entries survive.
  3. Press Partition. If the leader is on the two-node side, it cannot commit anything; the three-node side elects a new leader with a higher term and keeps working. Heal the partition and watch the old leader step down and its uncommitted entries get replaced.

Going deeper

What consensus costs

Every committed write needs a round trip to a majority, and the leader is a throughput bottleneck. So consensus groups are kept small (3 or 5 nodes) and used for the data that must be exactly right: configuration, metadata, locks, leadership, or one group per shard in systems like CockroachDB that run thousands of Raft groups in parallel.

Pre-vote

In the playground you may notice a node cut off on the minority side starting election after election, its term climbing. When the partition heals, its high term forces the healthy leader to step down for no good reason. Production Raft implementations add a pre-vote phase: a node first asks whether it could win, and only increments its term if a majority would vote for it.

Reads

A leader cannot simply answer reads from its own state: it might have been deposed without knowing. Linearizable reads either go through the log, or the leader confirms it is still leader with a round of heartbeats (ReadIndex), or it relies on a lease. Reads from followers are cheaper but may be stale.

Trade-offs

OptionWins whenLoses when
Raft consensus group Critical metadata, configuration, locks and leadership that must never diverge Write latency (a majority round trip) and throughput limited by one leader
Asynchronous leader-follower High-throughput data where a small loss on failover is acceptable Guaranteed durability of acknowledged writes and automatic safe failover
Many small Raft groups (one per shard) Scaling consensus to large datasets Operational complexity and cross-group transactions

Common mistakes

  • Running consensus over large, write-heavy datasets that do not need it.
  • Deploying an even number of members, or all members in one failure domain.
  • Reading from the leader's state without confirming leadership, returning stale data.
  • Treating the consensus cluster as a general-purpose database.

In the wild

Raft

The Raft consensus algorithm (opens in a new tab)

The Raft site, accompanying the paper "In Search of an Understandable Consensus Algorithm" by Diego Ongaro and John Ousterhout (a condensed version won a best paper award at USENIX ATC 2014), describes Raft as a consensus algorithm "designed to be easy to understand", equivalent to Paxos in fault tolerance and performance. It separates consensus into relatively independent subproblems: leader election, log replication and safety. The site hosts an interactive visualization of a Raft cluster and lists many open source implementations in different languages.

Check yourself

  1. When is a Raft log entry committed?

    Show answer

    B. When a majority of nodes have stored it Majority storage guarantees any future leader will have it.

  2. Why does a node only vote for candidates with logs at least as up to date as its own?

    Show answer

    B. So any elected leader is guaranteed to have every committed entry Committed entries are on a majority, and the winner needs a majority of votes, so it must hold them.

  3. The leader is on the minority side of a partition. What happens to writes sent to it?

    Show answer

    B. They cannot commit, since it cannot reach a majority Without a majority it can append but never commit; the majority side elects a new leader.

  4. Why are Raft clusters usually 3 or 5 nodes?

    Show answer

    B. Each write needs a majority round trip, so more nodes add latency with diminishing safety gains Five tolerates two failures; larger groups cost more per write.

Recap

  • Consensus keeps a replicated log identical, giving identical replicated state machines.
  • Raft elects one leader per term with randomized timeouts and majority votes.
  • The leader replicates entries with consistency checks; a majority makes them committed.
  • Election restriction and the current-term commit rule keep committed entries permanent.
  • Use consensus for small, critical data; it costs a majority round trip per write.

Get new lessons by email

Get new posts on backend and AI internals. No spam, unsubscribe anytime.

Your email is only used to send new posts. Privacy

Next up