Knowledge Guide
HomeSystem DesignLeader and Follower

Consensus with Raft — Leader Election & Log Replication

The algorithm that ties leader, log, heartbeat & quorum together

Raft makes a set of replicas agree on an ordered log even as nodes crash — the engine behind etcd, Consul, CockroachDB. Three roles, coordinated by an ever-increasing term (a logical clock); each term has at most one leader.

Raft state machine: follower to candidate on election timeout, candidate to leader on winning a majority
Raft state machine: follower to candidate on election timeout, candidate to leader on winning a majority

Leader election

  1. A follower that hears no heartbeat before its (randomised) timeout becomes a candidate, bumps the term, votes for itself.
  2. It requests votes; each node grants one vote/term, only if the candidate's log is up-to-date.
  3. Win a majority (a quorum) → become leader, send heartbeats. Randomised timeouts make split votes rare.

Log replication

  1. Clients send commands to the leader, which appends to its log (the WAL idea).
  2. It replicates each entry; once a majority store it, the entry is committed and applied.
  3. Committed entries survive leader crashes — the election rule guarantees a new leader already has them.

Why no split-brain

Commit needs a majority; two leaders in one term are impossible (one vote per node); a partitioned old leader can't reach a majority, so it commits nothing and steps down on seeing a higher term. Same quorum overlap guarantee: any two majorities share a node.

Takeaways


Re-authored from-scratch; diagram hand-authored (SVG) for this guide.

🤖 Don't fully get this? Learn it with Claude

Stuck on Consensus with Raft — Leader Election & Log Replication? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Consensus with Raft — Leader Election & Log Replication** (System Design) and want to truly understand it. Explain Consensus with Raft — Leader Election & Log Replication from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Consensus with Raft — Leader Election & Log Replication** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Consensus with Raft — Leader Election & Log Replication** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Consensus with Raft — Leader Election & Log Replication** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes