Consensus with Raft
How a cluster agrees on a single ordered log despite failures, and when your design needs that.
22 min readThe problem consensus solves
Some decisions must be made exactly once by the whole cluster: who is the leader, whether a lock is held, which configuration is current, the order of committed transactions. Consensus algorithms let a group of nodes agree on a sequence of values even when some crash or messages are delayed, as long as a majority is up and can talk. Raft is the most widely implemented; it powers etcd (and therefore Kubernetes), Consul, CockroachDB ranges and many other systems.
Leader election
Raft time is divided into numbered terms. Followers expect regular heartbeats from the leader. If a follower hears nothing for a randomised election timeout, often 150 to 300 ms, it increments the term, votes for itself and asks the others for votes. A node grants one vote per term, and only to a candidate whose log is at least as up to date as its own. A candidate with a majority becomes leader. Randomised timeouts make split votes rare, and the up-to-date rule guarantees a new leader already has every committed entry.
Log replication and commit
Clients send commands to the leader, which appends them to its log and sends them to followers. Once a majority has stored an entry, it is committed: the leader applies it to its state machine and replies to the client. Followers apply committed entries in the same order, so every replica's state is identical.
- A write costs one round trip from leader to a majority, plus a disk fsync
- 5 nodes tolerate 2 failures; 3 tolerate 1; even numbers add cost without adding tolerance
- A partitioned minority cannot commit anything, which is the CP choice in action
Using consensus in a design
You rarely run Raft yourself; you use a consensus-backed store for small, critical metadata and keep bulk data elsewhere. Typical uses: leader election for a scheduler, shard-to-node maps, feature flags and configuration, distributed locks with fencing tokens. A Raft group handles thousands to tens of thousands of writes per second, so do not put every user write through one group. Databases like CockroachDB scale by running many Raft groups, one per data range of up to a few hundred MB.
Fencing tokens
A node that holds a lock can pause for seconds in a garbage collection, lose the lock, and then wake up still believing it holds it. Protect writes with a fencing token: a number that increases each time the lock is granted (the Raft log index works). The storage layer rejects writes carrying a token older than the highest it has seen, so a zombie leader cannot corrupt data. Without fencing, a lock is only a hint, and two writers will eventually overlap.
Key takeaways
- Consensus lets a majority agree on one ordered log despite crashes and delays
- Raft elects a leader per term with randomised timeouts and commits entries on majority acknowledgement
- Use consensus-backed stores for small critical metadata, not bulk data
- Protect lock holders with fencing tokens so paused nodes cannot do damage
Sign in to track lessons and earn badges.