Goal

Implement each broadcast model of Module 1 on top of point-to-point links, prove the algorithms correct (safety and liveness), count their cost, and see exactly where the simple total order algorithms stop being fault tolerant.

Module 1 defined what each broadcast promises. This module shows how to keep those promises with nothing but point-to-point messages, and proves that the algorithms do. The plan of lecture 04.04 has two layers. The lower layer makes sure every message reaches every correct node, even if its sender crashes halfway (reliable broadcast). The upper layer decides when a received message may be delivered (FIFO, causal or total order).

The reliability layer and the FIFO and causal layers need no coordination at all: every node decides alone, from metadata carried by the messages. Total order is different. Both simple algorithms for it (a sequencer, or Lamport timestamps) work, but each stops completely when a single node crashes. Making the sequencer fault tolerant is exactly the consensus problem, which the rest of these notes solve.

The module follows lectures 04.04 (Broadcast Algorithms) and 04.05 (Broadcast Protocols, Part 2), and Kleppmann’s notes, Section 4.3, slides 81-86 (1).

Two layers on top of the network

Question. What does each layer have to guarantee, and which layer should handle lost packets?

application calls broadcast, receives deliver ordering layer FIFO, causal or total order: buffer + rule reliable broadcast eager relaying or gossip reliable links retransmit + deduplicate (TCP) network: may drop, delay, reorder
Figure 2.1. The two layers of lecture 04.04 on top of point-to-point links: first make every message reach every correct node, then decide when to deliver it.

The bottom of Figure 2.1 is the network of the system model: it may drop, delay, duplicate or reorder packets.

A reliable link is built from a lossy one by numbering messages, retransmitting until acknowledged, and discarding duplicates by number. TCP does all of this and is also FIFO. Lecture 04.04 notes that real systems almost always simply use TCP; only a system whose throughput depends on the broadcast (for example inside one data center, where loss is rare) might use UDP and fold a lighter retransmission scheme into the broadcast algorithm.

Note what a reliable link does not give: if the sender crashes, its retransmissions stop and the receiver may never get the message (condition 1 needs correct). That gap is what the next section closes.

Reliable broadcast: eager relaying

Question. How do the correct nodes agree on whether a message exists when its sender may crash after sending only some of the copies?

The first attempt of Kleppmann’s slide 81 sends directly to every node over reliable links. It satisfies best-effort broadcast and fails agreement, as Example 1.7 showed. The repair is to enlist every recipient: the first time a node receives a message, it forwards it to everybody else. Then a single correct node holding suffices to spread it.

// eager reliable broadcast at node i (Kleppmann slide 82)
on initialisation:
    seen := {}                        // ids of messages already handled
    count := 0
 
on request to broadcast m:
    m.id := (i, count)                // unique message id
    count := count + 1
    handle(m)
 
on receive m from a reliable link:
    handle(m)
 
procedure handle(m):
    if m.id not in seen:
        seen := seen + {m.id}
        for each node j != i:
            send m to j via reliable link
        deliver m to the application
LineWhy it is there
if m.id not in seeneach node handles a message once: gives no duplication, and stops the forwarding
send m to j for every the relay: after this step, the fate of no longer depends on the original sender
deliver m after the loopa node delivers only after it has relayed (this order matters for Remark 2.3)
Theorem 2.2 (Eager reliable broadcast is correct).

Assume crash-stop nodes and reliable links. Every execution of eager reliable broadcast satisfies reliable broadcast (Definition 1.6): integrity, validity and agreement. In an execution without crashes, each broadcast causes exactly point-to-point messages.

Agreement needed only one correct node that had delivered . This is precisely the situation of Figure 2.2, where the original sender crashed but B’s relay reached C.

Try it: step through the run. A crashes right after its first copy leaves, and B’s relay saves the broadcast. The last step counts the point-to-point messages: 5, because A sent only one of its two copies; a run without crashes sends . Then switch off re-broadcast on first receipt and step again: C never delivers m.

A B C m m A crashes relay m duplicate, ignored B and C both deliver m although A crashed
Figure 2.2. Eager reliable broadcast (Kleppmann slide 82). A crashes after its copy to B left; because B forwards m on first receipt, C still gets it. The dashed copy is redundant traffic, the price of the O(n squared) message count.
Remark 2.3 (Uniform agreement needs a majority).

Eager reliable broadcast does not give uniform agreement. Node may relay , deliver it, and crash before any copy leaves its machine; a reliable link from a crashed sender may lose those copies, so no correct node ever delivers although did. The standard fix: a node delivers only after it has received copies of (relays act as acknowledgements) from a majority of nodes. If , some correct node is in that majority and has already relayed , so every correct node gets it (2). The price is that a node must wait for nodes, an early sighting of the majority quorums that dominate Modules 3-9.

Gossip: relaying to a few random nodes

Question. Eager reliable broadcast sends messages per broadcast. With thousands of nodes that is too much. Can we relay less and still reach everybody?

Lecture 04.04 describes the gossip (or epidemic) idea: a node that has the message sends it to a few, say 3 or 4, randomly chosen nodes; each of those does the same, and so on. Like a rumour, the message spreads quickly, and no node depends on any particular other node. The price is that the guarantee becomes probabilistic: “every node receives the message with high probability”. To make that precise we use the simplest analysable model.

Definition 2.4 (Push gossip with fanout k).

Time proceeds in rounds. In every round, every node that has the message (an informed node) sends it to nodes, each chosen independently and uniformly at random among the other nodes. A node that receives the message becomes informed and starts gossiping in the next round. Nodes keep gossiping for a fixed number of rounds after they are informed.

The spread has two phases. While few nodes are informed, almost every copy reaches a new node, so the informed set grows by a factor of about per round: about rounds bring it to half the nodes. After that, most copies hit nodes that already know, and the question becomes how fast the last stragglers are found. The second phase is where the “high probability” comes from, and it has a clean proof.

Proposition 2.5 (The last phase of gossip).

In push gossip with fanout and no crashes, suppose that at the start of some round at least nodes are informed, and that informed nodes keep gossiping for the next rounds. Then

In particular, rounds leave some node uninformed with probability at most .

So with the first phase, about rounds, that is rounds, reach every node with high probability, while each informed node sends only messages per round.

Fine print: "forward once to three nodes" is not enough for large groups

Kleppmann’s slide 83 states the rule “when a node receives a message for the first time, forward it to 3 other nodes” and says it reaches all nodes with high probability. If each node forwards only once, that is not true for large : a constant fraction of nodes is missed. Heuristically, if a fraction of nodes ends up uninformed, about copies were sent in total, and a fixed node is missed by all of them with probability about , so ; for this gives , about 6% of the nodes. The high-probability guarantee needs nodes to keep gossiping for several rounds (as in Proposition 2.5), or a fanout that grows like , or a periodic “anti-entropy” exchange that repairs the stragglers, as in Demers et al. (3).

Lecture 04.04 draws the practical consequence. Gossip makes it very likely that the information reaches everybody soon, but it never tells you that a particular node has it now. If you update a replicated register from 3 to 4 by gossip and then answer a read from one randomly chosen replica, you may well return the stale 3. Systems such as Amazon’s Dynamo therefore combine gossip with reading and writing at several replicas, so that read and write sets overlap: the quorums of Module 3.

FIFO broadcast: per-sender sequence numbers

Question. Given reliable broadcast, how does a node make sure it delivers each sender’s messages in the order they were sent?

FIFO is a promise per sender, so a per-sender counter is enough (lecture 04.05: “[per] sender you need a counter, that’s basically it”). Each message carries its sender’s sequence number ; each receiver remembers, per sender, how many messages it has delivered, and holds back any message that arrives early.

// FIFO broadcast at node i, over reliable broadcast (Kleppmann slide 84)
on initialisation:
    sendSeq := 0
    delivered := [0, 0, ..., 0]       // delivered[j] = # delivered from j
    buffer := {}
 
on request to broadcast m:
    send (i, sendSeq, m) via reliable broadcast
    sendSeq := sendSeq + 1
 
on receive (j, s, m) from reliable broadcast:
    buffer := buffer + {(j, s, m)}
    while exists (k, s', m') in buffer with s' = delivered[k]:
        deliver m' to the application
        buffer := buffer - {(k, s', m')}
        delivered[k] := delivered[k] + 1

Two words about the interface. “Receive from reliable broadcast” is the delivery event of the lower layer; to the FIFO layer it is an arrival, not yet a delivery. And the message a node broadcasts comes back to itself through reliable broadcast, which is how a node FIFO-delivers its own messages. Kleppmann’s slide omits the line that removes delivered messages from the buffer; without it the algorithm is still correct (the condition s' = delivered[k] never matches a delivered tuple again) but the buffer grows forever.

Example 2.6 (The FIFO algorithm at node C).

Run the algorithm on the execution of Figure 1.2, with node order A, B, C. A broadcasts and ; B broadcasts . Node C receives them from reliable broadcast in the order .

Arrival at CDelivered by the loopBuffer afterwardsdelivered afterwards
nothing: delivered[A]
, then

The third arrival triggers two deliveries: once is out, delivered[A] becomes 1, which matches the waiting , and the while loop takes it as well.

Theorem 2.7 (The FIFO algorithm implements FIFO broadcast).

If the lower layer satisfies reliable broadcast, then every execution of the FIFO algorithm satisfies FIFO broadcast (Definition 1.8). More precisely, at every node and for every sender :

  1. (prefix) at all times, the messages from that has delivered are exactly those with sequence numbers , delivered in this order;
  2. (no starvation) if is correct and has received from reliable broadcast every message of with sequence number at most , then eventually delivers all of them.

Causal broadcast: dependency vectors

Question. FIFO looks only at the sender’s own previous messages. How can a node know which messages from other senders a message depends on?

The sender writes it down. When node broadcasts , it attaches a vector whose entry says how many messages from node it has delivered so far, and whose own entry is the number of messages it has itself broadcast before . A receiver delivers once it has delivered at least that many messages from every node. This is Kleppmann’s slide 85; lecture 04.05 calls the check “dependencies less than or equal to delivered”.

// causal broadcast at node i, over reliable broadcast (Kleppmann slide 85)
on initialisation:
    sendSeq := 0
    delivered := [0, 0, ..., 0]       // delivered[k] = # delivered from k
    buffer := {}
 
on request to broadcast m:
    deps := copy of delivered
    deps[i] := sendSeq
    send (i, deps, m) via reliable broadcast
    sendSeq := sendSeq + 1
 
on receive (j, deps, m) from reliable broadcast:
    buffer := buffer + {(j, deps, m)}
    while exists (k, d, m') in buffer with d <= delivered:   // componentwise
        deliver m' to the application
        buffer := buffer - {(k, d, m')}
        delivered[k] := delivered[k] + 1

For a message broadcast by node , write for its vector and for its own entry: the number of messages broadcast before . Vectors are compared componentwise, iff for every (Kleppmann slide 72). The delivery condition is

Remark 2.8 (These vectors are not vector clocks).

Kleppmann points out the difference. A vector clock (lecture 04.02) counts events observed from each node. Here counts messages from that have been delivered. A message received but still buffered does not count; this is what makes the algorithm respect causality defined through deliveries (Remark 1.4).

Try it: step through the lecture example. At each arrival at C, compare the message’s deps vector with C’s delivered vector and predict whether it is delivered or buffered before you press the next step.

A B C m1 [0,0,0] m1 m2 [1,0,0] m2 m3 [1,0,0] m3 m2 held needs [1,0,0], has [0,0,0] m1, m2 m3 delivered at C: [0,0,0] -> [1,0,0] -> [1,1,0] -> [2,1,0]
Figure 2.3. The causal broadcast algorithm (Kleppmann slide 85) on the lecture example, vectors in node order A, B, C. When m2 reaches C, its deps vector exceeds C's delivered vector, so C holds it; delivering m1 releases m2.
Example 2.9 (The causal algorithm on the lecture example).

Use the execution of Figure 1.3 (node order A, B, C), but let the copy of to C be slow, as in Figure 2.3.

  • A broadcasts : ; A delivers it to itself, so .
  • B delivers () and broadcasts with (its own entry is ).
  • A broadcasts with (its own entry is ; it has delivered nothing from B or C).
Arrival at CCheckDelivereddelivered at C
, deps : holdnothing
, deps ready; then ready
, deps

C delivers , respecting and . Node A delivers (it delivers its own at once, since ). The two orders differ on the concurrent pair, as causal broadcast allows.

Correctness of the causal algorithm

The proof has three small lemmas: per-sender deliveries form a prefix, dependency vectors grow along causal chains, and a vector “covers” every causal predecessor. Throughout, causal precedence is that of Definition 1.3, and ” is a step” means a chain of length one (program order, or delivery then broadcast).

Lemma 2.10 (Per-sender prefix).

At every node , at all times and for every sender , the messages from that has delivered are exactly those with , and delivered them in increasing order of .

Lemma 2.11 (Dependency vectors grow along steps).

If is a step, then and .

Lemma 2.12 (Dependency vectors cover causal predecessors).

If , then . Moreover and .

Theorem 2.13 (The causal algorithm is safe).

If the lower layer satisfies reliable broadcast, every execution of the causal algorithm satisfies integrity and causal order: if and a node delivers , then delivered before .

Safety is not enough: an algorithm that never delivers anything is trivially safe. Liveness needs the reliable layer.

Theorem 2.14 (The causal algorithm is live).

If the lower layer satisfies reliable broadcast, every execution of the causal algorithm satisfies validity and agreement. Together with Theorem 2.13 it implements causal broadcast (Definition 1.9).

Total order broadcast

Question. FIFO and causal order could be decided by each node alone. Total order requires every node to make the same choice about concurrent messages. Who chooses?

Lecture 04.05 and Kleppmann’s slide 86 give two answers: one designated node chooses (a sequencer), or a deterministic rule on timestamps chooses and everybody waits until the rule’s answer is certain. Recall that total order constrains only the agreement between nodes; the order itself may be arbitrary (Proposition 1.16, execution E4).

A single leader (sequencer)

// total order broadcast through a fixed sequencer L (lecture 04.05, slide 86)
on request to TO-broadcast m at node i:
    send Request(m) to L via reliable FIFO link
 
on receive Request(m) at L:
    FIFO-broadcast Ordered(m)                 // L handles requests one by one
 
on FIFO-deliver Ordered(m) at node i:
    TO-deliver m to the application
Theorem 2.15 (The sequencer gives FIFO-total order while the leader is up).

Assume the FIFO broadcast used by satisfies Definition 1.8 and links are reliable and FIFO. Then every execution satisfies integrity, total order (Definition 1.10) and FIFO order. If is correct, it also satisfies validity and agreement. If crashes, no message that has not yet FIFO-broadcast is ever delivered.

Try it: step through the run (the figure below lets A act as sequencer; here the sequencer is a separate node L, and nothing else changes). B and C send requests x and y; check that every node delivers them in L’s numbering, even A, which receives x first and must hold it. Then L crashes and A sends z: nothing more is delivered anywhere.

A B C sequencer x 1:x 1:x 1:x y 2:y 2:y 2:y A crashes z z is never ordered: no deliveries until a new sequencer
Figure 2.4. Total order through a sequencer (lecture 04.05, Kleppmann slide 86). Requests go to A, which numbers them and FIFO-broadcasts them; every node delivers 1:x then 2:y. After A crashes, nothing more is delivered.

Lecture 04.05 calls this the approach preferred in industry, and it is the shape of every leader-based system later in these notes. Its weakness is availability: after the leader crashes nothing is delivered (Figure 2.4). The obvious repair, “detect the crash and pick a new leader”, is where all the difficulty hides. A slow leader looks exactly like a crashed one, so two nodes may both act as sequencer and number messages differently; and the new leader must learn which messages the old one had already numbered and to whom. The lecture admits that changing the leader “safely while broadcasts are going on” is technically difficult and defers it; Raft and Paxos are the answer (Module 4 and onward).

Lamport timestamps

The leaderless alternative orders messages by Lamport timestamp. Each message gets a key : the sender’s Lamport clock value and the sender’s id , compared lexicographically, so keys are unique and totally ordered. Every node delivers messages in increasing key order. The difficulty, in Kleppmann’s words: how do you know that you have seen all messages with a smaller key?

// total order broadcast with Lamport timestamps at node i (slide 86)
// assumes reliable FIFO links between all pairs of nodes, including i to i
on initialisation:
    t := 0
    buffer := {}
    latest[q] := (0, q) for every node q     // largest key received from q
 
on request to broadcast m:
    t := t + 1
    send (t, i, m) to every node, including i
 
on receive (t', j, m) from node j:            // m = none for a heartbeat
    t := max(t, t')
    latest[j] := (t', j)
    if m != none: buffer := buffer + {(t', j, m)}
    while buffer is not empty:
        (T, k, m') := entry of buffer with the smallest key (T, k)
        if latest[q] >= (T, k) for every node q:
            deliver m' to the application
            buffer := buffer - {(T, k, m')}
        else:
            break                             // a smaller key may still come
 
on timeout (nothing sent for a while):
    t := t + 1
    send (t, i, none) to every node           // heartbeat: "nothing below t"
LineRole
t := t + 1 before every sendeach node’s keys strictly increase, so a FIFO link carries keys in increasing order
latest[q] >= (T, k) for all nothing with a smaller key can still arrive from
heartbeata node with nothing to say must still let the others advance
Theorem 2.16 (Lamport timestamps give FIFO-total order without crashes).

Assume reliable FIFO links. Every execution of this algorithm satisfies integrity, total order and FIFO order: every node delivers messages in increasing order of key, without gaps. If no node crashes and every node sends heartbeats, every message is eventually delivered by every node. If one node crashes, deliveries stop at every node as soon as their smallest buffered key exceeds the last key received from the crashed node.

Lecture clarification: why "dense timestamps"?

Lecture 04.05 says this approach “only works if you have very dense timestamps”, like 11, 12, 13, so that a receiver that holds 11 and 12 and then sees 14 knows that 13 is missing and waits for it. The underlying point is right: a node may deliver key only when it is certain that no smaller key is still on its way. But Lamport timestamps are not dense across nodes (two nodes can both be at 12, and a node’s clock can jump from 3 to 40 on receipt), so “detect the gap” cannot work by looking at the numbers. The working rule is the one on Kleppmann’s slide 86 and in the pseudocode: use FIFO links, and wait until every node has sent something with a larger key. Per sender, the keys then behave like the dense sequence numbers of FIFO broadcast.

Exam trap: "Lamport timestamps solve total order, so we do not need consensus"

Both algorithms of this section are correct only as long as nobody crashes (Lamport) or the leader does not crash (sequencer). Kleppmann’s verdict on slide 86: neither is fault tolerant, because the crash of a single node stops every other node from delivering. Waiting longer does not help: in an asynchronous system a crashed node and a slow one are indistinguishable. Fault-tolerant total order broadcast is equivalent to consensus (4), and consensus is the subject of Modules 4-9.

Summary: which algorithm gives what

AlgorithmGuaranteeMessages per broadcastSurvives crashes?
direct send over reliable linksbest-effortnot if the sender crashes mid-way
eager reliable broadcastreliableyes, any number
gossip, fanout reliable w.h.p.about per informed node per roundyes, probabilistic
FIFO algorithm over RBFIFORB cost, plus one counteras the RB layer
causal algorithm over RBcausalRB cost, plus an -vectoras the RB layer
sequencerFIFO-total1 request + one FIFO broadcastno: the leader is a single point of failure
Lamport timestampsFIFO-total per message + heartbeatsno: any crash blocks all
consensus (Raft, Multi-Paxos)FIFO-totala majority round per entryyes, if a majority is up

The last row is the destination. Raft’s leader is a sequencer; what Raft adds is a way to replace the leader that never lets two leaders number messages inconsistently, and a commit rule that only counts an entry as ordered once a majority stores it (Modules 5-7). Multi-Paxos achieves the same with ballots (Modules 8-9).

Worked example: one run through all the layers

We follow one broadcast of node B in a group of three, from the network up, to see every mechanism at once. The execution is the causal one of Figure 2.3; we look only at B’s message .

  1. Application. B’s application calls broadcast for after it has seen .
  2. Causal layer at B. and , so . B hands to reliable broadcast and sets .
  3. Reliable broadcast at B (eager). B gives the tuple the id , puts it in seen, sends it to A and C over reliable links, and delivers it to its own causal layer.
  4. Causal layer at B again. The tuple arrives at B’s buffer; , so B delivers to itself at once and sets .
  5. Reliable links. The copy to C is delayed and retransmitted (TCP); it arrives once.
  6. Reliable broadcast at C. First receipt of id : C relays it to A and B, then hands it to its causal layer. (B and A discard the relayed copies as duplicates.)
  7. Causal layer at C. , the check fails, is buffered. When arrives, C delivers and then , as in Example 2.9.

The reliable layer did not care about order, and the causal layer did not care about crashes: the two-layer split of lecture 04.04 in action.

What breaks

  • Crash-recovery. All the algorithms keep their state (seen, delivered, sendSeq, buffer, Lamport clock) in memory. A node that restarts with empty state reuses sequence numbers (receivers then discard or misorder its new messages) and re-delivers old messages. Recovery needs stable storage or a fresh incarnation id.
  • Uniform agreement. Eager relaying gives agreement only among correct nodes (Remark 2.3). If a node acts on a message and crashes, the survivors may never see it unless delivery waits for a majority.
  • Partitions. Reliable, FIFO and causal broadcast simply wait for the partition to heal; gossip may need many rounds afterwards. The total order algorithms block during the partition: the sequencer’s side keeps going, but the other side cannot deliver; the Lamport algorithm blocks everywhere.
  • Leader failure. The sequencer is a single point of failure; replacing it by timeout alone may create two sequencers. This is the problem that consensus solves.
  • Byzantine nodes. A lying node can send different messages under one id, forge a deps vector, or (as sequencer) give different numberings to different nodes. None of these algorithms tolerates that.

Why it matters

Reliable, FIFO and causal broadcast are cheap and fully fault tolerant because each node decides alone. Total order is not: both simple algorithms reduce it to “one node decides” or “wait for every node”, and either way one crash stops the system. Module 4 shows that this is no accident: fault-tolerant total order broadcast is equivalent to consensus. Module 5 starts repairing the sequencer with terms and majority votes, Raft (Modules 6-7) turns it into a replicated log, and Paxos (Modules 8-9) does the same with ballots.

Further reading

  • Kleppmann’s notes, Section 4.3, slides 81-86, for the original pseudocode of every algorithm here (1).
  • Hadzilacos and Toueg build every broadcast of Module 1 from reliable broadcast in modular steps, including uniform versions, and prove each layer (2).
  • Demers et al. introduced epidemic (gossip) algorithms for replicated databases and analyse rumour mongering and anti-entropy (3).
  • Chandra and Toueg show that total order broadcast and consensus are equivalent and solve both with failure detectors (4).

Homework

The problems for this module are in Homework 2.

1.
Kleppmann M. Distributed Systems: Lecture Notes [Internet]. University of Cambridge, Computer Science Tripos Part IB; 2021. Available from: https://www.cl.cam.ac.uk/teaching/2122/ConcDisSys/dist-sys-notes.pdf
2.
Hadzilacos V, Toueg S. A Modular Approach to Fault-Tolerant Broadcasts and Related Problems. Cornell University, Technical Report TR94-1425. 1994;
3.
Demers A, Greene D, Hauser C, Irish W, Larson J, Shenker S, et al. Epidemic Algorithms for Replicated Database Maintenance. In: Proceedings of the 6th ACM Symposium on Principles of Distributed Computing (PODC). 1987. p. 1–12.
4.
Chandra TD, Toueg S. Unreliable Failure Detectors for Reliable Distributed Systems. Journal of the ACM. 1996;43(2):225–67.