1. Architectural Overview & The Raft Safety Invariants
Raft (Ongaro & Ousterhout, 2014) decomposes replicated state machine consensus into three cleanly separated subproblems: Leader Election, Log Replication, and Safety. Every cluster node is in one of three states at any instant: FOLLOWER, CANDIDATE, or LEADER.
The 4 Non-Negotiable Raft Rules
- Monotonic Terms Act as Logical Clocks: Every RPC (
RequestVote,AppendEntries) carries the sender'scurrentTerm. Any node observingrpc.term > currentTermimmediately updatescurrentTerm, clearsvotedFor, and steps down toFOLLOWER. - Election Restriction (§5.4.1): A voter denies
RequestVoteif the candidate's log is less up-to-date than its own. Logs are compared bylastLogTermfirst; if terms are equal, the longerlastLogIndexwins. - Log Matching Property (
prevLogIndex/prevLogTerm):AppendEntriesis rejected unless the follower's log contains an entry atprevLogIndexwhose term matchesprevLogTerm. If a follower has conflicting uncommitted entries afterprevLogIndex, they are truncated and overwritten. - Current-Term Quorum Commit (§5.4.2): A leader marks index $N$ committed once a majority ($\lfloor C/2 \rfloor + 1$) of the cluster has replicated $N$ and
log[N].term === currentTerm.
2. Verification & Running the Lab
node demo.js
node test.js