The Raft consensus algorithm in System Design Interviews: When to Use It and What to Say
Raft is a consensus algorithm that elects a leader to replicate a log of commands across a set of servers, guaranteeing that all non-failed nodes see the same sequence of operations. In an interview you can frame it as "a way to keep a distributed state machine consistent despite crashes and network partitions".
The problem Raft solves
Imagine you are building a distributed key-value store that must never return stale data. The store is replicated across three machines for availability, but each write must be reflected on a majority before it is considered committed. Without a coordination protocol, a network glitch could cause two nodes to accept different writes for the same key, leading to divergent state. Raft provides a deterministic way to decide which write wins and to propagate that decision safely.
How Raft works – step by step
1. Roles and terms
- Leader: Handles all client writes, appends them to its log, and replicates them.
- Followers: Receive log entries from the leader and apply them once they are committed.
- Candidates: A follower that times out without hearing from a leader starts an election.
- Term: A monotonically increasing number that groups elections; each term has at most one leader.
2. Leader election
- A follower starts a random election timeout (e.g., 150-300 ms). If no AppendEntries RPC arrives, it becomes a candidate.
- The candidate increments its term, votes for itself, and sends RequestVote RPCs to all other nodes.
- Each node grants its vote if the candidate’s log is at least as up-to-date as its own. Once a candidate receives votes from a majority, it becomes leader.
- The new leader sends empty AppendEntries heartbeats to assert authority and prevent new elections.
3. Log replication
- Client request arrives at the leader.
- Leader creates a log entry
{term, command}and appends it to its local log. - Leader sends AppendEntries RPCs containing the new entry to all followers.
- Followers write the entry to their logs and reply with success.
- Once the leader receives acknowledgments from a majority, it marks the entry as committed and applies it to its state machine.
- Followers apply entries in order once they learn they are committed (via subsequent heartbeats).
4. Safety guarantees
- Leader completeness: A leader is only elected if its log contains all entries from previous terms that were committed.
- Log matching: If two logs contain an entry at the same index with the same term, the logs are identical in all entries up through that index.
- Commit rule: An entry is considered committed only after a majority of nodes have stored it.
Small worked example
Assume a three-node cluster: A, B, C. All start at term 1 with empty logs.
- Election: A’s timeout expires, it becomes candidate, increments term to 2, and votes for itself. It asks B and C. Both grant their votes because A’s log is as up-to-date as theirs. A becomes leader.
- Write: Client sends
PUT foo=1. A appends{term:2, cmd:PUT foo=1}at index 1 and sends AppendEntries to B and C. - Replication: B and C store the entry and reply success. A now has a majority (2/3) and marks the entry committed, applying it to its key-value store.
- Failure: Suppose C crashes before storing the entry. A continues to send heartbeats; B acknowledges. The entry remains committed because a majority (A and B) have it. When C recovers, it receives the missing entry in a later AppendEntries RPC and catches up.
Where Raft shows up in classic interview questions
| Interview question | Where Raft appears | Reason for using Raft |
|---|---|---|
| Design a distributed log service (e.g., Kafka-like) | Leader election and log replication layer | Guarantees ordered, durable writes across brokers |
| Build a highly available configuration store (e.g., etcd, Consul) | Consensus for storing key-value pairs | Strong consistency needed for service discovery |
| Design a fault-tolerant multiplayer game state server | Replicated state machine for game moves | Prevents divergent game state when players connect to different shards |
In each case the algorithm lives at the coordination layer: the component that decides which node writes the next command and when it is safe to consider the command visible to clients.
Trade-offs and the alternative interviewers love to ask about
Pros of Raft
- Understandable: The leader-centric model is easier to explain than Paxos’s proposer/acceptor dance.
- Deterministic leader: Simplifies client routing; all writes go through one node.
- Strong consistency: All committed entries are visible to a majority before being applied.
Cons of Raft
- Leader bottleneck: All writes funnel through the leader, limiting throughput to roughly the leader’s network and CPU capacity.
- Higher latency on leader change: Election timeouts add a pause (often a few hundred milliseconds) before the new leader can accept writes.
- More network traffic: Heartbeats and replication messages increase bandwidth, especially in large clusters.
Common alternative: Paxos (or its variants)
Interviewers may ask you to compare Raft with Paxos. Paxos provides the same safety guarantees but without an explicit leader role; instead, any proposer can try to get a value accepted. This makes Paxos harder to reason about and to implement correctly, and in practice Multi-Paxos deployments also run with a stable leader, because competing proposers can block each other. When asked, you can say: "Raft and Multi-Paxos give the same safety guarantees and similar performance; Raft’s advantage is a complete, easier-to-follow description of leader election, log replication, and membership changes."
30-second spoken version
"Raft is a leader-based consensus protocol that keeps a replicated log consistent across a cluster. It elects a leader via a term-based vote, the leader appends client commands to its log and replicates them to followers, and an entry is considered committed once a majority have stored it. It gives you strong consistency and a simple mental model, but the leader can become a throughput bottleneck and elections add latency. In interviews you’ll see it in designs for distributed logs, configuration stores, or any service that needs a fault-tolerant state machine."
Common mistakes candidates make
- Over-detailing internals: Spending too much time on the exact RPC fields or on log compaction when the interview is focused on high-level design.
- Ignoring failure scenarios: Failing to mention what happens on leader crash, network partition, or split-brain, which are core to Raft’s value.
- Skipping the trade-off discussion: Interviewers expect you to weigh Raft against alternatives; simply stating "Raft is the best" looks shallow.
- Mis-stating safety: Claiming Raft provides linearizability without clarifying that it requires a majority for commits.
- Confusing Raft with replication only: Raft is not just data replication; it is a consensus protocol that decides which value to replicate.
How to practice this
- Study the worked design in the System Design AI library. It walks through a Raft-based distributed log, showing the diagram and notes for each component.
- Draw your own board on the canvas: sketch the leader, followers, client path, and the RPC flow for election and replication.
- Run a mock interview: explain Raft in 30 seconds, then answer follow-up questions about failure handling and alternatives. Use the rubric to check you covered definition, problem, steps, trade-offs, and pitfalls.
Questions people ask
When should I choose Raft over other consensus algorithms?
Pick Raft when you need strong consistency, a clear mental model, and can tolerate a single leader bottleneck. It shines in services like distributed logs or configuration stores where ordered writes are essential.
How does Raft handle a leader crash?
Followers detect the missing heartbeats, start a new election, and a candidate that gathers a majority of votes becomes the new leader. Uncommitted entries on the crashed leader may be overwritten if the new leader does not have them; committed entries are never lost.
What is the main performance drawback of Raft?
All writes go through the leader, so the system’s write throughput is limited by the leader’s capacity. Additionally, elections introduce a pause of a few hundred milliseconds.
Can Raft work with more than three nodes?
Yes. Raft scales to dozens of nodes, but the majority quorum grows, which can increase latency for commit and make elections slower. Large clusters often shard the consensus group.