ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/gossiper/tasks/task-3-2-2-fanout-probability
TASK

Implementation

What fanout K guarantees that all N nodes receive a message with probability >= 0.99? The theory says that after R rounds of gossip with fanout K, the probability that a specific node has NOT received the message is approximately (1 - K/N)^R.

Your task is to:

  1. Implement the probability calculation: P(miss) = (1 - K/(N-1))^R
  2. Find minimum K for a given N and target probability
  3. Simulate gossip rounds to verify empirically

Implement a calc_fanout handler:

Request:  {"type": "calc_fanout", "msg_id": 1, "nodes": 25, "target_prob": 0.99, "rounds": 5}
Response: {"type": "calc_fanout_ok", "in_reply_to": 1, "min_fanout": 3, "miss_prob": 0.003}

And a simulate_gossip handler that runs a Monte Carlo simulation:

Request:  {"type": "simulate_gossip", "msg_id": 2, "nodes": 25, "fanout": 2, "rounds": 10, "trials": 100}
Response: {"type": "simulate_gossip_ok", "in_reply_to": 2, "delivery_rate": 0.98, "avg_rounds_to_all": 4.7}

Sample Test Cases

Calculate fanout for small clusterTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"calc_fanout","msg_id":2,"nodes":5,"target_prob":0.99,"rounds":5}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Simulate gossip returns delivery rateTimeout: 10000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"simulate_gossip","msg_id":2,"nodes":10,"fanout":3,"rounds":5,"trials":50}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}

Hints

Hint 1
Probability of a node NOT receiving a message in one round: (1 - K/N)^R where R is rounds
Hint 2
For 99% delivery across N nodes, solve for K given the number of gossip rounds
Hint 3
More rounds = lower K needed, but more latency
Hint 4
Log(N) rounds with fanout K=2 typically suffices for high probability delivery
Hint 5
Implement a simulation to verify the theoretical formula empirically
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

probabilitygossip reliabilityfanout analysisconvergence
main.py
python
Calculate Minimum Fanout for Reliable Delivery - The Gossiper | Build Distributed Systems