Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Quorum and leader election

After this lesson: I can explain how machines agree on a leader or a value.

60 min lesson3 commandsValkey lab: learn a Redis-compatible key-value store with keys, expiry, lists and counters

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

WordWhat it meansExample
Consensusmachines agreeing on one value
Leaderthe one machine that makes decisions for now
Majoritymore than half of all the nodes
Quorumthe smallest group that may decide2 of 3
Electiona vote to choose a leader
Terma 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

NodesQuorum (majority)Failures the group survives
321
431
532
642
743

The cells students fill in are left blank in this preview.

Who may go on with some nodes down?

Code: python
# 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?

Code: python
# 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

Command
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

Command
v CONFIG GET min-replicas-to-write

Shown 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

Command
v CONFIG GET min-replicas-max-lag

Shown 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

TermCandidates and votesResult
4Node 1 has votes from 1, 2, 3Node 1 is leader (3 of 5)
5Node 1 has 2 votes and node 2 has 2 votes, node 5 is downNo leader (needs 3)
6Node 2 has votes from 2, 3, 4Node 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.

About this lab and its source: Valkey lab: learn a Redis-compatible key-value store with keys, expiry, lists and counters

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.