Search DistillSys

Find a concept

Type at least two characters to search lessons, designs, papers, and interview prep.

Consensusintermediate7 min read

Raft Consensus

Agree on one sequence of operations despite node crashes, delayed messages, and network partitions.

2–5 minute refresher
Mental model

Elect → Propose → Replicate → Majority → Commit → Apply

30second
refresher

Raft elects one leader to order commands. The leader appends each command to its log, replicates it to followers, and marks it committed after a majority stores it. Every healthy node eventually applies the same committed log in the same order.

What problem does Raft solve?

Copies alone do not establish authority. If several replicas accept competing writes, which sequence survives? Raft gives a cluster one durable, ordered history even as leaders fail and messages arrive late.

How it works

  1. Elect — followers time out, become candidates, increment the term, and request votes.
  2. Propose — the elected leader receives a client command and appends a log entry.
  3. Replicate — the leader sends the entry to followers using AppendEntries.
  4. Commit — after a majority acknowledges, the entry becomes committed.
  5. Apply — each node applies committed entries to its state machine in order.

Run an election

Interactive lab

Raft election playground

Advance terms, remove authority, and isolate a minority. Watch which nodes can participate in a majority election.

Current term1
All five nodes are followers. Start an election.
Majority required3 of 5
Decision guide

Key trade-offs

ChoiceWhat you gainWhat it costs
3-node clusterTolerates one failure with low coordination costNo progress after two failures
5-node clusterTolerates two failuresMore infrastructure and replication traffic
Synchronous majorityCommitted history survives leader failureWrite latency includes quorum round trips

What happens if?

What happens if?

The leader crashes before majority acknowledgement

The entry is uncommitted. A future leader may overwrite it. The client must retry because it cannot know whether the command survived.

What happens if?

The leader commits, then crashes before replying

The command is durable, but the client sees a timeout. A retry can execute the command twice unless the API uses an idempotency key or request identity.

What happens if?

A minority is isolated

It may continue reading local state if policy allows, but it cannot elect a leader or commit new entries. Safety is preserved by sacrificing progress in the minority.

Real systems

Raft appears in etcd, Consul, TiKV, CockroachDB ranges, and many control planes. It is best for relatively small consensus groups coordinating metadata or replicated state—not for broadcasting every event to thousands of nodes.

Staff+ interview modeWhy can an acknowledged client request still need idempotency?Show answer

The server may commit the request and fail before the response reaches the client. The client observes an ambiguous timeout and retries. Consensus protects the log’s order; idempotency protects the business operation from duplicate intent.

#raft#consensus#replication#leader-election