← Lessons

quiz vs the machine

Gold1450

System Design

The Raft Consensus Algorithm

Electing a leader and replicating a log so a cluster agrees on one sequence of commands.

6 min read · core · beat Gold to climb

What Raft solves

Raft keeps a replicated log consistent across a cluster even when nodes crash. It was designed to be understandable, splitting consensus into leader election, log replication, and safety.

Leader election

Time is divided into numbered terms. Each node is a follower, candidate, or leader. If a follower hears no heartbeat from a leader before its randomized election timeout, it becomes a candidate, increments the term, and asks for votes. A candidate that wins a majority becomes leader.

Log replication

The leader accepts client commands, appends them to its log, and sends them to followers. An entry is committed once a majority store it. Only then is it applied to the state machine and acknowledged to the client.

Safety

Raft only elects candidates whose log is at least as up to date as a majority, so committed entries are never lost. The term numbers let nodes reject stale leaders, preventing two leaders in the same term.

Why it stays simple

Randomized timeouts make split votes rare and quick to resolve. Strong leadership means all changes flow one direction, which is easier to reason about than peer to peer schemes.

Key idea

Raft achieves consensus by electing a single strong leader per term and committing log entries only after a majority of nodes replicate them.

Check yourself

Answer to earn rating on the learn ladder.

1. When is a Raft log entry considered committed?

2. How does Raft prevent two leaders in the same term?

3. Why does Raft use randomized election timeouts?