SYS ARCHITECTLearning Platform
Settings
Theory

Raft Consensus Algorithm

📋 Overview

Raft is a consensus algorithm designed for manageability and understandability, serving as an alternative to the complex Paxos protocol. It ensures that a cluster of nodes can agree on a shared state (the "Replicated Log") even in the face of network partitions or node failures. Raft operates on a strong Leader-based model, where a single elected leader has complete authority over log management and client communication.


🏗️ Core Principles & Characteristics

  • Leader Election: Nodes use randomized election timeouts to trigger a vote. A candidate must secure a majority ($N/2 + 1$) to become the Leader.
  • Log Replication: The Leader accepts client commands, appends them to its log, and replicates them to followers via AppendEntries RPCs.
  • Safety (Log Matching): Raft guarantees that if a leader commits a log entry at a specific index, every future leader will also contain that entry.
  • Terms: Time is divided into terms of arbitrary length, each starting with an election. This acts as a logical clock to detect stale leaders.

⚖️ Trade-offs: Pros & Cons

  • Pros: Highly understandable and easier to implement than Paxos; strong consistency (CP); handles node failures and network partitions gracefully.
  • Cons: Leader Bottleneck: All writes must flow through a single node; Availability: If a majority is lost, the cluster cannot process writes; Latency: Every commit requires a majority network round-trip.

🌍 Real-World Implementation

  • Etcd: The distributed key-value store for Kubernetes uses Raft for all its coordination and state management.
  • Consul: Uses Raft for service discovery and configuration synchronization.
  • CockroachDB: Implements a variant of Raft to ensure consistent replication of data across globally distributed shards (Ranges).
  • InfluxDB: Uses Raft for managing cluster metadata and high-availability setups.

💡 Interview "Gotchas" & Tips

  • Randomized Timeouts: Why use them? To prevent "Split Votes" where multiple nodes start an election at once, causing a stalemate.
  • Log Compaction: Acknowledge that the log can't grow forever. Mention Snapshotting, where the state is saved to disk and the log is cleared.
  • Read-Index / Leases: Explain that for high-performance reads, the leader can avoid the log if it has a valid "Lease," proving it is still the authoritative leader.

📐 Suggested Architecture Primitives

  • Quorum (3 or 5 nodes): To balance fault tolerance and performance.
  • State Machine: The underlying logic (e.g., a DB engine) that executes commands once they are committed to the Raft log.
  • Heartbeats: Constant communication from Leader to Followers to maintain authority and prevent new elections.
  • Pre-Vote: An optimization to prevent a "disrupted" node from causing unnecessary elections when it rejoins the cluster.
Canvas