System designLoading progress…

Consensus and replicated logs

Explain consensus, logical ordering, leases and fencing under stated failure and timing assumptions.

Consensus and replicated logs

Consensus protocols let a group agree on an ordered history of decisions despite failures. Raft splits the problem into leader election, log replication and safety rules. Storing an entry on a majority is part of the story but not all of it: terms stop old leaders, each server votes once per term, a candidate needs a log at least as up-to-date as the voter's, and a leader commits an entry by counting replicas only if it is from its own current term.

A term-3 leader and a 5-node group. Entries and their terms:

entry 1 (term 2): nodes storing it3entry 2 (term 2): nodes storing it3entry 3 (term 3): nodes storing it3
entrytermonmajority (3)?current term?
123/5yesno
223/5yesno
333/5yesyes
committed up to entry 3 a leader commits an entry by counting replicas only when it is from ITS OWN term; older entries become committed along with it election and replication rules (terms, one vote per term, up-to-date logs) are what make "stored on a majority" safe
Change how many nodes store each entry.

Predict first. Can ignoring term and election rules break a majority-based sketch?

Chapter context

Coordination lets multiple processes agree on selected shared decisions even though messages can be delayed or lost and participants can stop. Agreement is achieved by a protocol, not by adding a shared timestamp or an informal majority rule.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

For a fixed n-member majority group, quorum size is ⌊n/2⌋+1\lfloor n/2\rfloor+1. A 2f+1-member group can lose f members while retaining a majority, provided the remaining participants can communicate and the protocol's other assumptions hold. Fencing requires monotonically ordered tokens and enforcement at the resource.

Worked example

Five members require three votes, allowing two unavailable members while a communicating majority remains. Separately, a resource has already accepted token 8 from a new owner. An old owner's delayed operation bearing token 7 must be rejected, regardless of whether the old process still believes it owns the lease.

Reference code

The following example uses the Python standard library and does not call a remote service.

members=5
quorum=members//2+1
assert quorum == 3 and members-quorum == 2
highest=8
def accept(token):
    global highest
    if token < highest: return False
    highest=token
    return True
assert not accept(7)
assert accept(8)
assert accept(9)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

A timeout cannot establish a remote process's state. Quorums require full protocol rules. Fencing is ineffective if the resource accepts stale tokens or an attacker can invent authorized tokens.