Agree on one leader or one value
Nodes must agree on configuration, leadership or a lock even when some crash.
Say it like this
The ring version and the leader must be agreed on by every node even if one crashes, so I keep that metadata in etcd, a Raft-replicated log, and never put high-volume data there.
Basic solution
Use a consensus service rather than building it: a small replicated log with leader election.
A write through a Raft leader
The leader appends the entry, replicates it, and acks the client only once a majority has written it to disk. The commit revision becomes the fencing token.
How it works
- A small cluster of 3 or 5 nodes elects a leader. Each node waits a randomised timeout (say 150 to 300 ms) without hearing from a leader; the first node to time out sends a vote request to the others and becomes leader once a majority has voted for it. The randomisation makes it unlikely that two nodes ask at the same moment.
- Every write goes to the leader. The leader appends the entry (for example, ring version 9 with its node list) to its own log on disk and sends the same entry to every follower.
- Each follower writes the entry to its own log and calls fsync so it survives a crash, then replies to the leader. The leader waits until a majority of nodes (itself plus 1 of 2, or itself plus 2 of 4) have confirmed, then marks the entry committed and replies to the client with the entry's revision number.
- Followers apply committed entries in log order to their copy of the data, so every node ends up with the same sequence of values in the same order, even though they received them at different times.
- If the leader dies, a follower stops hearing heartbeats, times out and starts a new election with a higher term number. The voting rule only lets a node win if its log contains every committed entry, so the new leader never loses a write the old leader acknowledged.
- Clients read from the leader for linearizable reads, meaning they see every committed write. If they can accept a few milliseconds of lag they can read from a follower, which answers faster because it does not need to confirm it is still the leader.
What it buys you
- One agreed value per key even when a minority of nodes crash or are partitioned away. Two nodes of five can be down and the cluster keeps committing.
- A revision number that increases with every committed write. Because it is agreed by consensus it never goes backwards, which makes it usable as a fencing token.
Worked example
Distributed cache cluster membership
- 60 cache nodes in a region, each holding a slice of the hash ring. Membership changes a few times a day when nodes are added or replaced; ring reads happen on every client start and every change.
- Membership lives in a 5-node etcd cluster. Write path: a change to the ring is one etcd transaction of about 5 KB; the leader appends it, replicates it to the 4 followers, and acknowledges once 2 of them have written it to disk. In-region latency is about 2 to 5 ms per write, almost all of it the followers' disk fsyncs.
- Load: a handful of writes per day and perhaps 60 watchers waiting for changes. etcd comfortably handles 10,000 writes per second, so throughput is irrelevant here. The point is correctness: every cache client must see the same ring, and a node must not be in two rings at once.
- Failure budget: 5 nodes tolerate 2 failures; 3 nodes tolerate 1. Spread across 3 zones (2, 2, 1), losing a whole zone removes at most 2 nodes and still leaves a majority of 3, so the cluster keeps working through a zone outage.
- What must never go in etcd: the cached values themselves (millions of writes per second) or driver locations. Every consensus write costs a disk fsync on a majority of nodes, so the cluster tops out in the tens of thousands of writes per second even for small values, and the data would not fit anyway.
Variants and when each wins
Raft (etcd, Consul, CockroachDB, TiKV)
- A leader-based protocol designed to be understandable, where one node orders all writes and a majority confirms each one. It wins by default today: reads from the leader are linearizable and the failure modes are well documented.
Multi-Paxos (Chubby, Spanner)
- The original consensus family, offering the same guarantees with a more flexible but harder-to-implement protocol. It wins only inside a product that already implements it correctly; never write it by hand.
ZAB (ZooKeeper)
- A Raft-like protocol with ephemeral nodes (keys that vanish when the client's session ends) and watches (notifications when a key changes), which make leases and locks natural to build. It wins when you want lock and election primitives out of the box, and its zxid is a ready-made fencing token.
Leader election via a lease in a database
- A single row holding the current leader and an expiry time, updated with a conditional write so only one node can claim it. It wins when you already have a strongly consistent database and need only one leader, because it adds no new cluster to run; it is only as safe as that database and its failover.
Recommendation: etcd or ZooKeeper for coordination metadata; a lease row when you already have a strongly consistent database and need only one leader.
Failure modes
Loss of majority
- You notice every write hanging or returning an error while reads of old data still work. It happens because 2 of 3 nodes are down, or because a partition has left no side holding a majority, so no leader can commit anything. Run 5 nodes across 3 zones so a zone loss still leaves a majority, and alert on whether a majority is reachable rather than on node count.
Treating it like a database
- You notice etcd at 100 percent disk I/O and leaders being re-elected every few minutes. It happens because high-volume or large values are being written through consensus, and every one of them costs a disk fsync on a majority of nodes. Keep only small metadata and pointers in the consensus store, and move the bulk data to a partitioned store that does not need agreement.
Stale reads from followers
- You notice a client reading the old ring version right after a change was committed. It happens because the read was served by a follower that had not yet applied the new entry. Route reads that must be current through the leader, which guarantees linearizable results, or have the client wait until its follower reports a revision at least as new as the one the write returned.
Two leaders during an election
- You notice an old leader still serving writes for a moment after a partition cut it off from the majority. It happens because the old leader has not yet noticed that it lost contact; it keeps believing it is in charge until its lease on leadership runs out. Make leader leases shorter than the election timeout so the old leader steps down before a new one can be elected, and attach fencing tokens to anything the leader writes elsewhere so late writes are rejected.
Slow disks
- You notice write latency jumping from a few milliseconds to hundreds. It happens because every commit requires an fsync on a majority of nodes, and a shared or overloaded disk makes each fsync slow. Give the consensus nodes dedicated SSDs that the application never writes to.
Interviewer follow-ups
Answer out loud before you open each one.
Why three or five nodes and not two or four?
A majority of 2 is 2, so two nodes tolerate zero failures and are worse than one. Four nodes need 3 for a majority and tolerate one failure, the same as three nodes, at more cost. Odd numbers give the best fault tolerance per node.
What is the difference between consensus and a quorum read or write in Cassandra?
A Cassandra quorum only guarantees overlap between readers and writers; concurrent writers can still conflict and are resolved by timestamp. Consensus serialises writes into one agreed log order, so there are no conflicts to resolve, at the cost of a leader and higher latency.
How would you use etcd for leader election in the matching service?
Each matcher candidate for a region tries to create the key region:nyc:leader with a lease tied to its session. Exactly one succeeds. The others watch the key and retry when it disappears. Every write the leader makes to the trip store carries the etcd revision as a fencing token.
What happens to clients during a leader election?
Writes fail or block for the election window, typically a few hundred milliseconds to a couple of seconds. Clients should retry with backoff. Reads from followers can continue if the application accepts slightly stale data.
Connections
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon EC2 Auto Scaling | Cluster manager | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |