Why This Matters
Getting multiple computers to agree on something sounds simple, but it is one of the hardest problems in computer science. A consensus algorithm ensures that even if some nodes crash or messages are delayed, the surviving nodes all agree on the same value. Without consensus, you cannot have reliable leader election, consistent replication, or atomic commits.
The Raft protocol was designed to be understandable -- a response to the complexity of Paxos, which is notoriously difficult to implement correctly. If you understand Raft, you understand the foundation of systems like etcd, CockroachDB, and TiDB.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Raft consensus: propose, replicate to followers, wait for majority, commit.
Code Example
// Simplified Raft consensus simulation
class RaftNode {
constructor(id) {
this.id = id;
this.log = [];
this.commitIndex = -1;
this.currentTerm = 0;
this.state = "follower"; // follower, candidate, leader
}
}
class RaftCluster {
constructor(size) {
this.nodes = Array.from({ length: size }, (_, i) => new RaftNode(i));
this.leader = null;
this.electLeader(0);
}
electLeader(nodeId) {
const node = this.nodes[nodeId];
node.currentTerm++;
node.state = "leader";
this.leader = node;
// In real Raft, this requires majority vote
console.log(`Node ${nodeId} is leader for term ${node.currentTerm}`);
}
propose(value) {
if (!this.leader) throw new Error("No leader");
const entry = {
term: this.leader.currentTerm,
index: this.leader.log.length,
value: value
};
// Step 1: Leader appends to its own log
this.leader.log.push(entry);
console.log(`Leader appended: ${value} at index ${entry.index}`);
// Step 2: Replicate to followers
let acks = 1; // Leader counts as 1
const majority = Math.floor(this.nodes.length / 2) + 1;
for (const node of this.nodes) {
if (node.id === this.leader.id) continue;
if (node.state !== "leader") {
node.log.push({ ...entry });
acks++;
console.log(` Node ${node.id} acked`);
}
}
// Step 3: Check majority
if (acks >= majority) {
this.leader.commitIndex = entry.index;
console.log(`Committed: ${value} (${acks}/${this.nodes.length} acks)`);
return true;
}
return false;
}
}
const cluster = new RaftCluster(5);
cluster.propose("SET x = 1");
cluster.propose("SET y = 2");Interactive Experiment
Try these exercises to deepen your understanding:
- Kill 2 of 5 nodes by setting their state to "dead" and skip them during replication. Can the cluster still commit?
- Kill 3 of 5 nodes. What happens now? This demonstrates the safety property of consensus.
- Add a term check: followers should reject entries from a leader with a lower term number.
- Implement a
getCommittedLogmethod that returns only entries up to the commit index.
Quick Quiz
Coding Challenge
Write a function called `hasMajority` that takes an array of boolean votes (true = yes, false = no) and returns true only if a strict majority voted yes. This simulates the core decision mechanism in Raft and Paxos.
Real-World Usage
Consensus algorithms power the most critical infrastructure in modern computing:
- etcd (Raft): The key-value store behind Kubernetes. Every cluster configuration change goes through Raft consensus.
- CockroachDB (Raft): A distributed SQL database that uses Raft to replicate data across nodes for strong consistency.
- ZooKeeper (Zab): Apache ZooKeeper uses its own consensus protocol (Zab) for distributed coordination, locking, and configuration management.
- Google Spanner (Paxos): Google's globally distributed database uses Paxos for consensus across data centers worldwide.
- Blockchain: Proof of Work and Proof of Stake are consensus mechanisms for decentralized networks without trusted leaders.