MongoDB

Raft Consensus Algorithm

Understand the Raft consensus algorithm and how it achieves fault-tolerant leader election and log replication in distributed systems.

S

srikanthtelkalapally888@gmail.com

Raft Consensus Algorithm

Raft is a consensus algorithm designed to be understandable, managing replicated logs in distributed systems.

Problem

In a distributed cluster, how do all nodes agree on the same sequence of operations even when some nodes fail?

Raft Roles

Leader:    Handles all client requests, replicates log
Follower:  Accepts log entries from leader
Candidate: Initiates leader election

Leader Election

1. Node starts as Follower
2. No heartbeat received in timeout (150-300ms)
   → Becomes Candidate
   → Increments term
   → Requests votes
3. Receives majority votes → Becomes Leader
4. Leader sends heartbeats to maintain authority

Log Replication

1. Client sends command to Leader
2. Leader appends to its log (uncommitted)
3. Leader sends AppendEntries to all Followers
4. Majority ACK → Leader commits entry
5. Leader notifies Followers to commit
6. Response to client

Safety

  • Election Safety: At most one leader per term
  • Leader Completeness: New leader has all committed entries
  • Log Matching: Same index/term → same command

Fault Tolerance

3 nodes → tolerate 1 failure
5 nodes → tolerate 2 failures
(N nodes → tolerate (N-1)/2 failures)

Used In

  • etcd (Kubernetes state store)
  • CockroachDB
  • TiKV
  • Consul

Raft vs Paxos

RaftPaxos
UnderstandabilityHighComplex
Leader electionExplicitImplicit
AdoptionGrowingOlder systems

Conclusion

Raft simplifies distributed consensus through clear leader election and log replication, making it the preferred algorithm for modern distributed datastores.

Share this article