π What is Raft Consensus?
Raft is a consensus algorithm used in distributed systems to make a cluster of servers agree on a single sequence of operations (a log).
-
Imagine you have 5 servers.
-
They must all agree on the same log of events
[SET x=1] β [SET y=3] β [DEL x] -
Even if some servers crash or messages get lost, Raft ensures that:
- All surviving servers eventually agree on the same log.
- The log is applied in the same order everywhere.
This makes Raft ideal for databases, blockchain, and distributed systems.
βοΈ Why Consensus Is Hard
- Network messages can be delayed, reordered, or lost.
- Servers can crash and restart.
- Without consensus, two servers might think they are the "truth," leading to data divergence.
Raft solves this by electing one leader and having all followers trust it.
ποΈ The Core Ideas of Raft
1. Leader Electionβ
- At any time, one server is the leader, others are followers.
- Followers start an election if they donβt hear from a leader within a timeout.
- Votes are requested; the one with the majority becomes leader.
- Guarantee: only one leader at a time.
2. Log Replicationβ
- Clients send requests (commands) to the leader.
- The leader appends the command to its log and replicates it to followers.
- Once a majority acknowledges, the leader commits the entry.
- Followers also commit that entry.
β This ensures that all nodes eventually have the same ordered log.
3. Safety Rulesβ
- Election restriction: Candidates must have the latest log to win leadership.
- Log matching: If two logs have an entry at the same index and term, theyβre identical before that point.
- Commitment: Only entries known by a majority are committed.
π Raft States
Each server cycles between 3 roles:
- Follower β passive, just waits for leaderβs messages.
- Candidate β requests votes during election.
- Leader β handles client requests, replicates logs.
π Timeline Example
Initial state: 5 servers, no leader
β
Timeout (no heartbeat received)
β
One follower becomes Candidate, requests votes
β
Majority vote β Leader elected
β
Client: "SET x=10"
β
Leader appends log [SET x=10], replicates to followers
β
Majority responds OK β Leader commits entry
β
Followers commit entry β State is consistent
π οΈ Where Raft Is Used
- etcd (Kubernetes metadata store)
- Consul (HashiCorp service discovery)
- TiKV (Rust-based distributed KV store, uses raft-rs)
- Databend (uses openraft)
π― Key Takeaways
- Raft = Consensus via Leadership.
- Only leader talks to clients.
- Majority (quorum) ensures fault tolerance.
- Handles crashes, restarts, partitions.
- Simpler than Paxos, but equally powerful.