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

Implementation

Full state sync wastes bandwidth when nodes are mostly in sync. Anti-entropy optimizes this: first exchange compact digests. Only transfer full state if digests differ.

Implement digest-based anti-entropy:

  1. digest handler returns a hash of the current message set
  2. digest_sync handler compares digests and only transfers if different
  3. Track bandwidth savings
Request:  {"type": "digest", "msg_id": 1}
Response: {"type": "digest_ok", "in_reply_to": 1, "digest": "abc123", "count": 5}
Request:  {"type": "digest_sync", "msg_id": 2, "remote_digest": "abc123", "remote_messages": null}
Response: {"type": "digest_sync_ok", "in_reply_to": 2, "match": true, "transferred": 0}

If digests differ, the remote sends its messages:

Request:  {"type": "digest_sync", "msg_id": 3, "remote_digest": "xyz789", "remote_messages": [1,2,3]}
Response: {"type": "digest_sync_ok", "in_reply_to": 3, "match": false, "transferred": 2, "local_messages": [1,2,3,4,5]}

Sample Test Cases

Digest of empty setTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"digest","msg_id":2}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Digest sync with matching digestTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"broadcast","msg_id":2,"message":42}}
{"src":"c1","dest":"n1","body":{"type":"digest","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": "broadcast_ok", "in_reply_to": 2, "msg_id": 1}}

Hints

Hint 1
A digest is a compact summary of your state (e.g., sorted hash of all message IDs)
Hint 2
Compare digests first; only transfer full state if they differ
Hint 3
This saves bandwidth when nodes are mostly in sync
Hint 4
Use a simple hash of sorted message IDs as the digest
Hint 5
Track bytes saved by using digests vs full sync
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

anti-entropydigestset reconciliationbandwidth optimization
main.py
python
Implement Anti-Entropy with Digest Comparison - The Gossiper | Build Distributed Systems