Raft Consensus Algorithm
Raft Consensus Algorithm
Introduction: The Problem of “Shared Truth”
In a distributed cluster, how do we make sure all servers agree on the same value (e.g., “The current configuration is X”), even if some servers crash or the network is slow?
For years, Paxos was the only answer, but it was notoriously difficult to understand and implement. In 2013, Raft was introduced as a simpler alternative. It decomposes the problem into three clear sub-problems: Leader Election, Log Replication, and Safety.
What Problem does it solve?
- Input: A series of commands (Log) from clients.
- Output: A replicated, consistent log across all healthy nodes.
- The Promise: The cluster acts as a single reliable state machine. If a majority (Quorum) is alive, the system continues to work.
Core Idea (Intuition)
Raft is like a Parliamentary System:
- One Leader: Only the Leader can accept new commands from clients.
- The Follower’s Job: Followers simply replicate what the Leader tells them.
- Term & Election: If the Leader stops sending “heartbeats,” a Follower becomes a Candidate and starts an election. Each election happens in a “Term” (like a school year).
The golden rule: “The Leader is always right.” But to become Leader, you must prove you have the most up-to-date log.
How it Works
1. Leader Election
- Nodes start as Followers.
- If a Follower hears no heartbeat for a random timeout, it becomes a Candidate.
- It asks for votes. If it gets a Majority (e.g., 3 out of 5), it becomes the Leader.
2. Log Replication
- Client sends a command to the Leader.
- Leader writes it to its log and sends it to all Followers.
- Once a Majority of Followers have written the log, the Leader “commits” it and tells the Followers to do the same.
3. Safety
- Raft ensures that if a command is committed, it will never be lost or overwritten by a future Leader.
Typical Business Scenarios
✅ Service Discovery: etcd (used by Kubernetes) uses Raft to store cluster metadata.
✅ Distributed Databases: CockroachDB and TiDB use Raft to ensure data consistency across different data centers.
✅ Configuration Management: Storing global flags or secrets that must be identical across all servers.
❌ High-Write Performance: Raft requires a majority to acknowledge every write. This adds network latency. For pure speed without strict consistency, use Gossip or NoSQL (AP).
Performance & Complexity
- Availability: nodes must be healthy.
- Latency: At least one round-trip to the majority of nodes for every write.
Summary
“Raft is consensus made human. By electing a strong leader and using a majority-rule for logs, it ensures that your cluster always shares one single truth.”
