Gossip protocols & failure detection

In one sentence

Every node periodically swaps what it knows with a few random peers, so membership and failure news reach the whole cluster in O(log N) rounds with no coordinator.

Analogy: Office rumors: each person tells a few colleagues at the coffee machine, who tell a few more. By the afternoon everyone knows, and nobody sent a company-wide email.

A SWIM probe: a failed ping is retried through other members, and the target is suspected, not buried, until a timeout confirms it.

How it works

  • Each node gossips with a few random peers every interval, exchanging heartbeats and what it heard about others.
  • News spreads like an epidemic: everyone knows in O(log N) rounds, with constant load per node.
  • SWIM pings a random member, retries through k other nodes, then marks it suspect before declaring it dead.
  • Phi accrual replaces a fixed timeout with a suspicion level computed from past heartbeat arrivals.
  • Views are eventually consistent: fine for membership, wrong for decisions that need one answer.

Check yourself

Answer to mark it learned; your answers aren't scored.

In your 500-node cluster, healthy nodes are marked dead during GC pauses and rejoin seconds later. Every flap triggers data rebalancing. The detector is a fixed 2 s heartbeat timeout. Best fix?

Go deeper

Full explanation

A cluster needs every node to know who is in it and who is alive. If all N nodes heartbeat each other, messages grow as N²; a central registry is a single point of failure. Gossip (epidemic) protocols avoid both.

Dissemination. Every interval, each node picks a few random peers and exchanges state: its heartbeat counter plus what it heard about others. The receiver keeps the newer version of each entry. Informed nodes multiply every round, so news reaches everyone in O(log N) rounds while each node's load stays constant.

Failure detection. A peer whose heartbeat stops rising becomes suspect. Two refinements:

  • SWIM: ping one random member per period. No ack? Ask k other members to ping it (indirect probe). Still silent: gossip it as suspect, and declare it dead only if it doesn't refute in time.
  • Phi accrual: replace the fixed timeout with a suspicion level φ computed from past heartbeat arrival times, so the detector adapts to the network.

The view is eventually consistent: two nodes can briefly disagree. Use gossip for membership; decisions that need one answer, like electing a leader, need Consensus (Raft).

Code Toy push gossip: rounds grow with log N
pythonToy push gossip: rounds grow with log N
import random

def rounds_to_spread(n: int, fanout: int = 3, seed: int = 0) -> int:
    """Push gossip: each informed node tells `fanout` random peers per round."""
    rnd = random.Random(seed)
    informed = {0}                 # node 0 learns that a node joined
    rounds = 0
    while len(informed) < n:
        informed |= {rnd.randrange(n) for _ in informed for _ in range(fanout)}
        rounds += 1
    return rounds

for n in (1_000, 10_000, 100_000):
    print(n, rounds_to_spread(n))  # 8, 10, 13: 100x the nodes, 5 more rounds

def merge(mine: dict, theirs: dict) -> None:
    # entry = (generation, heartbeat): the higher tuple is newer, so it wins.
    # No coordinator: any two nodes that talk end up with the same view.
    for node, state in theirs.items():
        if state > mine.get(node, (0, -1)):
            mine[node] = state
Real example

A 1,000-node cluster gossips to 3 random peers every 200 ms (the LAN defaults of HashiCorp memberlist). A node joins. In a toy simulation of push gossip with fanout 3, all 1,000 nodes know after ~8 rounds, about 1.6 s. With 10,000 nodes it takes 10–11 rounds: 10× the nodes, 2–3 more rounds.

Each node still sends only 3 small messages per interval, whatever the cluster size. All-to-all heartbeats would need ~1,000,000 messages per interval.

Trade-offs +3 · −3

+ You gain

  • No coordinator and no single point of failure: a new node joins through any member.
  • Constant load per node, so it scales to thousands of nodes.
  • Tolerates lost messages and dead nodes: the next round goes around them.

− You pay

  • Only eventually consistent: for a moment, nodes disagree on who is alive.
  • Detection is probabilistic: a slow or overloaded node can be declared dead (a false positive).
  • Hard to debug: no single place holds the truth about the cluster.

Use it when

Membership, failure detection and slowly changing metadata (ring tokens, schema versions) in large clusters without a leader: Dynamo-style stores and service discovery.

Avoid it when

Anything that needs one agreed answer or an order: leader election, locks, configuration that must change atomically. Use Consensus (Raft). A small fixed cluster can simply heartbeat a coordinator.

Numbers to know 5
  • Cassandra: every second each node gossips with one random node, plus sometimes an unreachable node or a seed; it convicts peers with a phi accrual variant (phi_convict_threshold 8 by default).
  • HashiCorp memberlist LAN defaults (checked Nov 2026): gossip to 3 nodes every 200 ms, probe one node every 1 s, 3 indirect probes.
  • Dynamo paper (2007): each node contacts a random peer every second; gossiping the full membership works for a couple of hundred nodes.
  • SWIM (2002): detection time and message load per member do not grow with group size; all-to-all heartbeats grow as N².
  • Toy simulation, fanout 3: ~8 rounds for 1,000 nodes, 10–11 for 10,000, ~13 for 100,000.
Common pitfalls 4
  • Deciding from the gossip view what needs agreement: two nodes each believe they are the leader (split brain).
  • Tight fixed timeouts: a GC pause or a network blip marks healthy nodes dead and triggers needless rebalancing.
  • Forgetting that a degraded node suspects everyone else; Lifeguard makes a node that misses acks slow its own detector.
  • Treating seed nodes as leaders: they only help new nodes find the cluster.
Interview questions 3

Answer out loud first, then reveal.

Why gossip instead of a central registry with heartbeats?Show answer

A registry is simple, and for tens of nodes I'd use one. But it is a single point of failure whose load grows with the cluster, and all-to-all heartbeats grow as N squared. With gossip each node talks to a few random peers per round, so load per node is constant and news still reaches everyone in O(log N) rounds. The price is an eventually consistent view, so I never take exclusive decisions from it.

How does SWIM avoid declaring a healthy node dead?Show answer

Three mechanisms. First, indirect probes: if my ping fails, I ask k other nodes to ping the target, which rules out a problem on my own link. Second, suspicion: the node is gossiped as suspect and declared dead only after a timeout. Third, refutation: the suspect hears the rumor and answers with a higher incarnation number, which overrides it. HashiCorp's Lifeguard adds that a node missing acks slows its own detector.

Gossip or consensus: where does each belong in one system?Show answer

Both, for different data. Consul is the usual example: Serf gossip tracks which nodes exist and are alive, cheaply and without a leader, while its Raft servers hold the data that must be consistent. My rule: gossip for facts that may be briefly stale, like membership or load; consensus for decisions that must be unique, like who is leader or who holds a lock.