Back to 20 Concepts
consensusExpert

Raft Distributed Consensus & Quorum Log Replication

Raft provides fault-tolerant distributed consensus via Leader Election, Heartbeats, Log Replication, and strict Quorum Supermajority (N/2 + 1) voting, preventing split-brain states.

Intuitive Mental Model

The Parliamentary Council: The council elects 1 Prime Minister (Leader). Every new law (Log Entry) proposed by the PM must be stamped and approved by a majority (3 out of 5 ministers) before becoming permanent law.

Architecture Blueprint & CodeProduction Standard
interface AppendEntriesRPC {
  term: number;         // Leader's current term
  leaderId: string;
  prevLogIndex: number; // Index of log entry immediately preceding new ones
  prevLogTerm: number;
  entries: LogEntry[];  // State machine commands to replicate
  leaderCommit: number; // Leader's commitIndex
}

Key Architectural Takeaways

  • Quorum Rule: An N-node cluster tolerates up to floor((N-1)/2) server failures (e.g. 5 nodes tolerate 2 failures).
  • Leader Completeness: Any committed log entry is guaranteed to be present in all future leaders' logs.
  • Used in production: Kubernetes etcd, CockroachDB, HashiCorp Consul, Apache Kafka (KRaft).
Common Architectural Pitfall

Deploying even-numbered consensus clusters (e.g. 4 nodes); 4 nodes require 3 votes for quorum (tolerating only 1 failure—identical to a 3-node cluster).

Production Best Practice

Always deploy odd numbers of nodes (3, 5, or 7) to maximize fault tolerance.