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

Implementation

A 2P-Set (two-phase set) supports both add and remove by maintaining two G-Sets: the add-set and the remove-set (tombstones). An element is in the set if it is in the add-set but NOT in the remove-set.

Request:  {"type": "add", "msg_id": 1, "element": "x"}
Response: {"type": "add_ok", "in_reply_to": 1}

Request:  {"type": "remove", "msg_id": 2, "element": "x"}
Response: {"type": "remove_ok", "in_reply_to": 2}

Request:  {"type": "read", "msg_id": 3}
Response: {"type": "read_ok", "in_reply_to": 3, "elements": []}

Request:  {"type": "merge", "msg_id": 4, "add_set": ["a","b"], "remove_set": ["b"]}
Response: {"type": "merge_ok", "in_reply_to": 4}

Sample Test Cases

Add then readTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"add","msg_id":2,"element":"x"}}
{"src":"c1","dest":"n1","body":{"type":"read","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": "add_ok", "in_reply_to": 2, "msg_id": 1}}
{"src": "n1", "dest": "c1", "body": {"type": "read_ok", "elements": ["x"], "in_reply_to": 3, "msg_id": 2}}
Add then remove then readTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"add","msg_id":2,"element":"y"}}
{"src":"c1","dest":"n1","body":{"type":"remove","msg_id":3,"element":"y"}}
{"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": "add_ok", "in_reply_to": 2, "msg_id": 1}}
{"src": "n1", "dest": "c1", "body": {"type": "remove_ok", "in_reply_to": 3, "msg_id": 2}}
{"src": "n1", "dest": "c1", "body": {"type": "read_ok", "elements": [], "in_reply_to": 4, "msg_id": 3}}

Hints

Hint 1
A 2P-Set has two internal G-Sets: add-set and remove-set
Hint 2
To add: insert into add-set. To remove: insert into remove-set
Hint 3
Value = add-set - remove-set
Hint 4
Once removed, an element cannot be re-added (tombstone is permanent)
Hint 5
Merge both G-Sets independently via union
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

2P-SetCRDTtombstone setadd-remove semantics
main.py
python
Implement Two-Phase Set (2P-Set) - The Gossiper | Build Distributed Systems