In distributed databases, maintaining a consistent, replicated state machine across unreliable network nodes requires a consensus algorithm. While Paxos was historically dominant, Raft is designed for understandability and powers modern infrastructure like etcd (Kubernetes), Consul, and Kafka KRaft.\n\n### 1. The Three Node Roles\nAt any instant, a Raft node operates in one of three states:\n* Leader: Handles all client read/write requests, coordinates log replication, and broadcasts heartbeats.\n* Follower: Passive state; responds to leader RPCs and heartbeat pulses.\n* Candidate: Transitions here if leader heartbeats cease; requests votes to become the new leader.\n\n### 2. Leader Election & Randomized Timeouts\n1. Heartbeat & Election Timeout: The leader periodically broadcasts heartbeats (AppendEntries RPCs with empty payload). Followers expect a heartbeat within a randomized window (e.g., 150ms to 300ms).\n2. Candidate Transition: If a follower's election timer expires without a heartbeat, it increments the term number, votes for itself, and broadcasts RequestVote RPCs.\n3. Quorum Majority: A candidate becomes leader as soon as it secures votes from a strict majority of nodes ($\lfloor N/2 \rfloor + 1$). In a 5-node cluster, 3 votes are required.\n4. Split-Vote Prevention: Randomizing the election timeout prevents two candidates from starting elections at the exact same millisecond and splitting the vote.\n\n### 3. Log Replication & Commit Safety\n* When a client sends a write to the leader, the leader appends it to its local log.\n* The leader replicates the entry to followers via AppendEntries RPCs.\n* Once a majority of followers acknowledge writing the entry to disk, the leader marks the entry Committed, applies it to its local state machine, and replies to the client.\n* Log Matching Invariant: If two logs contain an entry with the same index and term, they are guaranteed to be identical up to that index.\n\n### Key Architectural Comparison\n| Consensus Dimension | Raft Protocol | Two-Phase Commit (2PC) | Paxos Protocol |
| :--- | :--- | :--- | :--- |
|---|---|---|---|
| Leader Model | Strong, authoritative Leader | Centralized Coordinator | Dual/Multi-Proposer support |
| Complexity | Modular (Election separated from Replication) | Simple but blocking | Extremely complex edge states |
| Production Adoption | Kubernetes (etcd), CockroachDB, Kafka KRaft | Relational Distributed DBs (XA Transactions) | Google Spanner, Chubby |\n