从零实现 Raft:不可靠网络里的共识

Raft from Scratch: Consensus over an Unreliable Network

Your multi-agent system IS a distributed system — elect a leader on a deterministic simulated network

6 labs9 AI-mentored sessions~5 hoursBilingual · EN / 中
Start learning →

About this course

A build-it-yourself course: no paper-memorizing, no passive animations. In every
session an AI mentor walks you through writing a deterministic simulated network in
pure Python — injecting delay, loss and partitions with your own hands — then watching
the nodes YOU wrote elect a leader, replicate logs, and hold consensus through disasters.
It follows the topic skeleton of the Raft paper and MIT 6.824 — leader election, log
replication, safety, a replicated KV state machine, crash recovery & snapshots,
membership changes — with every session rewritten as original hands-on teaching for the
online sandbox: formulas typeset, timelines drawn, partition boundaries draggable.
And everything replays deterministically: same seed, same split-brain, frame by frame —
better than a real network for truly seeing consensus. By the end you'll know it in
your bones: your multi-agent system IS a distributed system.

What you'll learn

  • Hand-write a deterministic discrete-event simulated network with injectable delay, loss and partitions
  • Implement the follower/candidate/leader state machine, randomized timeouts and majority elections
  • Implement log replication, conflict backtracking and the current-term-only commit rule
  • Replay the paper's Figure 8 scenario and storm-test four safety invariants across hundreds of seeds
  • Build a Raft-backed replicated KV state machine handling client retries, idempotency and consistent reads
  • Implement crash recovery and log snapshots, and explain why membership changes go one server at a time

Syllabus

1The First Step of Consensus: Leader Election3 sessions
  • 1A Deterministic Sim-Network and Three Roles30 minStart →
  • 2Network Partition and Re-election30 minStart →
  • 3Randomized Timeouts and the Split Vote30 minStart →
2Log Replication: Making the Cluster Remember as One3 sessions
  • 1Heartbeats and Terms: How a Leader Stays in Power30 minStart →
  • 2Log Replication: Majority Ack Means Commit30 minStart →
  • 3Log Repair: Consistency Check and Backtracking30 minStart →
3Safety: Committed Entries Are Never Lost3 sessions
  • 1The Election Restriction: No Vote for Stale Logs30 minStart →
  • 2The Figure 8 Trap: Commit Only in Your Own Term30 minStart →
  • 3The Invariant Checker: Storm-Testing Safety over 100 Seeds30 minStart →
4From Log to App: a Replicated KV Store0 sessions

Sessions are on the way.

5Crashes, Recovery & Snapshots0 sessions

Sessions are on the way.

6To Production: Membership Changes & Engineering0 sessions

Sessions are on the way.

Same series · 人工智能全透析

AI, End to End

Synthesized from the best material across the web — rebuild the core of AI from scratch, the efficient way.