ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/gossiper/tasks/task-3-3-1-tree-broadcast
TASK

Implementation

Random gossip wastes messages because nodes may receive duplicates. A spanning tree ensures each node receives exactly one copy: the root broadcasts to its children, who forward to their children, etc.

Your task is to implement tree-based broadcast:

  1. Use the topology message to learn your tree neighbors
  2. On broadcast, forward to all tree neighbors except the source
  3. Track the forwarding path for debugging

Handle these message types:

  • topology: Store the neighbor list for this node
  • broadcast: Store value and forward to tree neighbors
  • read: Return all known values
  • tree_info: Return current tree structure for this node
Request:  {"type": "tree_info", "msg_id": 1}
Response: {"type": "tree_info_ok", "in_reply_to": 1, "neighbors": ["n2", "n3"], "message_count": 5}

Sample Test Cases

Topology stores neighborsTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1","n2","n3"]}}
{"src":"c1","dest":"n1","body":{"type":"topology","msg_id":2,"topology":{"n1":["n2","n3"],"n2":["n1"],"n3":["n1"]}}}
{"src":"c1","dest":"n1","body":{"type":"tree_info","msg_id":3}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "topology_ok", "in_reply_to": 2, "msg_id": 1}}
{"src": "n1", "dest": "c1", "body": {"type": "tree_info_ok", "neighbors": ["n2", "n3"], "message_count": 0, "in_reply_to": 3, "msg_id": 2}}
Broadcast stores and reads backTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"topology","msg_id":2,"topology":{"n1":[]}}}
{"src":"c1","dest":"n1","body":{"type":"broadcast","msg_id":3,"message":42}}
{"src":"c1","dest":"n1","body":{"type":"read","msg_id":4}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "topology_ok", "in_reply_to": 2, "msg_id": 1}}
{"src": "n1", "dest": "c1", "body": {"type": "broadcast_ok", "in_reply_to": 3, "msg_id": 2}}
{"src": "n1", "dest": "c1", "body": {"type": "read_ok", "messages": [42], "in_reply_to": 4, "msg_id": 3}}

Hints

Hint 1
Build a spanning tree from the topology provided by Maelstrom
Hint 2
Each node only forwards to its children in the tree
Hint 3
Tree broadcast has O(N-1) messages vs O(N*K) for random gossip
Hint 4
The root receives the broadcast and pushes down the tree
Hint 5
Use the topology message to learn your neighbors
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

spanning treetree broadcastoverlay networkmessage forwarding
main.py
python
Implement Tree-Based Broadcast Overlay - The Gossiper | Build Distributed Systems