Five committee members record concert seat sales, each in their own book. When does a decision count? Their rule: the chair reads out a decision, "seat A-12 goes to Budi", and it becomes official once at least three of the five have written it down. If the chair disappears, the rest elect a new chair, and the winner needs a majority, at least three votes.
Servers need the same kind of rule. How several servers (called nodes) agree on one thing, such as the order of data changes or who the leader is, even when some nodes die or the network breaks, is called consensus. In the committee picture, each committee member is one node, their book is that node's record of decisions, and the chair is the leader, the server that decides.
Why consensus is needed
In the Why Distributed Systems Are Hard lesson, split brain happens when two nodes both think they're the leader, and the same seat gets sold to two people. Consensus prevents it with one rule: a decision only counts if a majority of nodes agree. Two separated groups can't both have a majority, so only one side can decide.
The most widely used consensus algorithm today is Raft, designed on purpose to be easy to understand. The Lab runs a simplified Raft for TicketTune's seat register, the record of who owns which seat. There are five nodes, n1 to n5. Each node keeps a log, an ordered list of decisions, and each entry (one line) in the log is one seat decision such as A-12 = budi.
Raft: leader, follower, candidate, term
Every node has one role:
- Leader: the only one that takes writes from clients, then copies them to the other nodes.
- Follower: passive, takes copies from the leader and answers vote requests.
- Candidate: a follower that is trying to become leader.
A term is a numbered period of office. Every election raises the term by one. At the start of the Lab, n1 is the leader of term 1. The leader sends a heartbeat (a regular message, every 50 ms in this model) so the followers know the leader is alive. A node that sees a higher term than its own takes that term at once and becomes a follower, an old leader included.
Log replication: committed after a majority
A client writes through the leader. The leader adds the entry to its log, then sends AppendEntries (a "store this entry" message) to every follower. The new entry is committed (official, never lost) once a majority of nodes hold it. The leader counts too. This is the first write in the Lab:
| ms | What happens |
|---|---|
| 0 | a client asks n1 for A-13 = siti |
| 0 | n1 adds entry #2 to its log, not committed yet |
| 2 | n2, n3, n4, n5 store it and ack ("stored"): 5 of 5 |
| 2 | majority reached: committed, the client gets an OK |
The leader doesn't wait for every node. In a 5-node cluster, 3 nodes holding the entry (the leader + 2 followers) are enough to commit, so 2 slow or dead nodes don't stop the system.
Majority and quorum
The minimum number of votes needed is called the quorum. For consensus, the quorum is a strict majority: more than half. The formula is Math.floor(N / 2) + 1, or, the same thing, yes votes > N / 2. N is the number of nodes in the cluster:
| Cluster size | Quorum | May die |
|---|---|---|
| 3 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
| 7 | 4 | 3 |
The exercise builds two functions for this rule:
function hasQuorum(votes) {
const yes = votes.filter(Boolean).length; // count the trues
return yes > votes.length / 2;
}
function canCommit(clusterSize, yesVotes) {
return yesVotes > clusterSize / 2;
}
console.log(hasQuorum([true, true, true, false, false])); // true: 3 > 2.5
console.log(canCommit(4, 2)); // false: 2 is not more than 2
votes.filter(Boolean)keeps the elements that aretrue, so its.lengthis the number of yes votes.- Use
>, not>=. Exactly half is not a majority:canCommit(4, 2)must befalse, whilecanCommit(4, 3)istrue. - With no
trueat all,yesis 0, and0 > 2.5isfalse.
A 4-node cluster is no stronger than a 3-node one: both can only lose 1 node. Worse, 4 nodes split 2 and 2 have no majority on either side, so the whole cluster stops taking writes. That's why consensus clusters have an odd size: 3, 5 or 7.
Leader election
A follower that gets no heartbeat for an election timeout (a limit on how long to wait for a heartbeat) assumes its leader is dead. In Raft, this timeout is random, between 150 and 300 ms, so nodes don't try to become leader at the same moment. In the Lab each node has a fixed number: n5 180 ms, n3 210, n1 250, n4 270, n2 290. Then that follower:
- Becomes a candidate, raises the term (1 becomes 2), and votes for itself.
- Sends RequestVote (a request for votes) to every node.
- Each node gives one vote per term, to the first candidate that asks, and only if that candidate's log is at least as up to date as its own.
- Got a majority (3 of 5)? The candidate becomes leader and sends heartbeats right away.
Kill n1 in the Lab and wait: after 180 ms, n5 becomes a candidate for term 2 and gets votes from n2, n3, n4. With its own vote, that's 4 of 5: n5 becomes the leader of term 2. No node becomes leader without votes. Because each node gives only one vote per term, two candidates can't both get a majority, so there are never two leaders in the same term.
Partitions: the old leader on the minority side
A partition means the network splits, so nodes on one side can't talk to nodes on the other. After the A-13 = siti write above, here is a sequence you can run in the Lab: cut the network, write through n1, wait for the election timeout, write through n5, then reconnect.
| ms | Side n1, n2 |
Side n3, n4, n5 |
|---|---|---|
| 5 | n1 writes A-14 = dewi, held by 2 of 5: not committed |
|
| 185 | no heartbeat: n5 becomes a candidate for term 2, gets 3 votes, becomes leader |
|
| 187 | n5 writes A-14 = maya, held by 3 of 5: committed |
|
| 237 | the network heals: n1 sees term 2 and becomes a follower. n1 and n2 drop A-14 = dewi and copy n5's log |
So A-14 is not Dewi's. Her entry only reached 2 nodes, never committed, and its client never got an OK. Only the side with a majority (3 of 5) can elect a leader and commit. The minority side stops taking writes: consistency is kept by giving up availability on that side. That's the "CP" side in the CAP Theorem lesson.
📌 Paxos, ZAB, and the systems that use consensus
- Paxos (Leslie Lamport) is an older, more general consensus algorithm, famously hard to understand and to implement correctly. Raft was created to fix that.
- ZooKeeper uses ZAB, a close relative of Paxos. etcd (where Kubernetes keeps its state, the condition of its whole system) and Consul use Raft. Distributed databases such as CockroachDB and TiDB run Raft per slice of data.
- A theoretical result (FLP, 1985) shows that on a network that can delay messages without limit, no algorithm can always reach agreement. Raft stays safe (never two different decisions), and in practice it finishes because its timeouts are random and the network delivers the messages in the end.
- Don't write your own Raft for production (an app that real users use). Use etcd, Consul or ZooKeeper.
🎯 When to use consensus
Consensus is slow compared with local work: every write waits for a majority of nodes. Use it for state that is small but critical:
- Leader election and locks: only one server may run the "close sales at 23:59" job.
- Config: prices and settings that must be the same on every server.
- Service discovery: the list of servers that are up.
High-volume data (click logs, login sessions) stays in a normal database or cache.
⚠️ Half is not a majority
- An even cluster. 4 nodes are no stronger than 3, and they can split 2 and 2 with no majority.
>=in the exercise. Exactly half is not a majority:canCommit(4, 2)must befalse. Use>.- Forgetting the leader counts. In a 5-node cluster, the leader plus 2 followers is already 3: enough to commit.
- Consensus for all data. Every write waits for a majority of nodes, so use it only for small critical state.
TL;DR: Consensus makes several servers (nodes) agree even when some die or the network breaks. In Raft, one leader takes writes and copies them to the followers (the other nodes) with AppendEntries (a "store this entry" message). An entry (one decision in the log) commits, which means it is official and never lost, once a majority of nodes hold it, the leader included. Quorum (the minimum number of votes) = a strict majority, Math.floor(N / 2) + 1 or yes > N / 2. A 5-node cluster needs 3 and survives 2 dead nodes, and exactly half (2 of 4) is not a majority. A follower that gets no heartbeat (a regular message from the leader) for an election timeout (random, 150 to 300 ms) becomes a candidate. The candidate raises the term (the numbered period of office), asks for votes, and becomes leader with a majority. Every node gives one vote per term. When the network splits, only the majority side can elect a leader and commit. Writes to an old leader on the minority side never commit and are dropped when the network heals. Use odd clusters, use etcd, Consul or ZooKeeper (ready-made consensus programs), and keep only small critical state (data about the system's condition) there.
⚡ Only the leader takes writes
Meaning: Raft is a way for several servers (nodes) to agree on the order of data. In Raft, the leader is the only node that takes writes from clients, then copies them to the followers with AppendEntries (a "store this entry" message). Followers are passive: they store copies and give votes. A candidate is a follower that is trying to become leader.
⚡ Committed after a majority
Guess first: in a 5-node cluster, a new entry (one decision in the log) is committed, which means official, only when enough nodes hold it. How many nodes must hold it?
3, and the leader counts. So the leader + 2 followers are enough, and 2 slow or dead nodes don't stop the system.
Meaning: A committed entry is official and will never be lost.
⚡ Half is not a majority
Write:
function hasQuorum(votes) {
const yes = votes.filter(Boolean).length; // count the trues
return yes > votes.length / 2;
}
function canCommit(clusterSize, yesVotes) {
return yesVotes > clusterSize / 2;
}
console.log(hasQuorum([true, true, true, false, false])); // true: 3 > 2.5
console.log(canCommit(4, 2)); // false: 2 is not more than 2
Meaning: A majority means more than half. hasQuorum counts the yes votes (true) in the votes array, and canCommit compares the number of yes votes with the cluster size. Use >, not >=: canCommit(4, 2) must be false, because 2 of 4 is only half. That's why consensus clusters have an odd size: 3, 5 or 7.
⚡ Partitions: only the majority side decides
Meaning: When the network splits (a partition), the 5 nodes end up on a side of 2 (the minority) and a side of 3 (the majority). A-14 = dewi, written through the old leader on the minority side, only reaches 2 nodes, so it never commits (becomes official) and is dropped when the network heals. The majority side (3 of 5) elects a new leader and commits A-14 = maya.