How machines agree on one answer
Consensus is how several machines agree on one value, such as which of them is the leader, even when some fail or cannot be reached. Without it two machines can both believe they are in charge.
You will learn the idea behind it, the majority vote, and how a group elects a leader.
By the end of this lesson
Note
I can explain how machines agree on a leader or a value.
Key words
| Word | What it means | Example |
|---|---|---|
| Consensus | machines agreeing on one value | |
| Leader | the one machine that makes decisions for now | |
| Majority | more than half of all the nodes | |
| Quorum | the smallest group that may decide | 2 of 3 |
| Election | a vote to choose a leader | |
| Term | a numbered period with at most one leader |
The cells students fill in are left blank in this preview.
A majority is over half of all nodes
Suppose a group of five nodes must choose a leader. If each node asked only itself, two nodes could each pick a different leader.
The fix is the majority: a decision counts only if more than half of all the nodes agree. A group of five needs three. This smallest deciding group is the quorum. Two groups can never both hold a majority, because they would share a node.
Worked example: quorum sizes
| Nodes | Quorum (majority) | Failures the group survives |
|---|---|---|
| 3 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
| 6 | 4 | 2 |
| 7 | 4 | 3 |
The cells students fill in are left blank in this preview.
Who may go on with some nodes down?
# Five nodes. Can the nodes that are still up act on their own?
nodes = 5
for up in [5, 4, 3, 2, 1]:
if up * 2 > nodes:
print(up, "up: more than half, can go on")
else:
print(up, "up: not more than half, must wait")
# Change nodes to 6 and run again. Is 3 up enough?
A network split lets only one side act
A network partition cuts the nodes into groups that cannot talk to each other. Each side cannot tell whether the other has crashed or is just cut off.
If both sides chose a leader, the data would split into two versions. This is split brain. With a quorum rule only the side with a majority may act. The smaller side stops and waits.
Reads and writes can use quorums too
A write can wait for w nodes to store it, and a read can ask r nodes. Suppose w plus r is more than the number of nodes. Then every read group shares a node with the last write group, so it sees the newest value.
With five nodes, w = 3 and r = 3 always overlap. w = 2 and r = 2 can miss each other.
Do a write and a read always meet?
# A write must reach "w" nodes and a read asks "r" nodes. Do they always share one?
nodes = 5
for pair in [[3, 3], [2, 2], [4, 2], [1, 5]]:
w = pair[0]
r = pair[1]
if w + r > nodes:
print("write", w, "read", r, ": always share a node")
else:
print("write", w, "read", r, ": may miss each other")
# Change nodes to 7 and run again.
Make a tiny client called v
v() ( printf '%s\r\n' "$*" | nc -w 1 valkey 6379 )Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask how many replicas a write needs
v CONFIG GET min-replicas-to-writeShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask how late a replica may be
v CONFIG GET min-replicas-max-lagShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
An election picks one leader for a term
Time is split into numbered terms. A node that hears no leader for a while becomes a candidate in a new term and asks for votes. Each node votes once per term.
A candidate with a majority becomes the leader of that term. If nobody gets a majority, because the votes split, that term has no leader and a new one starts.
Worked example: two elections with five nodes
| Term | Candidates and votes | Result |
|---|---|---|
| 4 | Node 1 has votes from 1, 2, 3 | Node 1 is leader (3 of 5) |
| 5 | Node 1 has 2 votes and node 2 has 2 votes, node 5 is down | No leader (needs 3) |
| 6 | Node 2 has votes from 2, 3, 4 | Node 2 is leader (3 of 5) |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think a majority means more than half of the nodes that are still up. It means more than half of all the nodes, up or not. If two of five nodes are up, they are not a majority, even though they are all that is left.