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

Implementation

LWW silently loses data when two clients write concurrently. Your task is to demonstrate this and implement a version-vector alternative that detects conflicts.

Implement two modes: lww (last-writer-wins) and vv (version-vector):

Request:  {"type": "set_mode", "msg_id": 1, "mode": "vv"}
Response: {"type": "set_mode_ok", "in_reply_to": 1}

Request:  {"type": "vv_write", "msg_id": 2, "key": "x", "value": "a", "context": {}}
Response: {"type": "vv_write_ok", "in_reply_to": 2, "vc": {"c1": 1}}

Request:  {"type": "vv_read", "msg_id": 3, "key": "x"}
Response: {"type": "vv_read_ok", "in_reply_to": 3, "values": [{"value": "a", "vc": {"c1": 1}}], "conflict": false}

When concurrent writes happen in vv mode, both values are preserved:

Response: {"type": "vv_read_ok", "values": [{"value": "a", "vc": {"c1": 1}}, {"value": "b", "vc": {"c2": 1}}], "conflict": true}

Sample Test Cases

Set mode to vvTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"set_mode","msg_id":2,"mode":"vv"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "set_mode_ok", "in_reply_to": 2, "msg_id": 1}}
Single vv_write and read, no conflictTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"vv_write","msg_id":2,"key":"x","value":"a","context":{}}}
{"src":"c1","dest":"n1","body":{"type":"vv_read","msg_id":3,"key":"x"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "vv_write_ok", "vc": {"c1": 1}, "in_reply_to": 2, "msg_id": 1}}
{"src": "n1", "dest": "c1", "body": {"type": "vv_read_ok", "values": [{"value": "a", "vc": {"c1": 1}}], "conflict": false, "in_reply_to": 3, "msg_id": 2}}

Hints

Hint 1
LWW silently discards the loser in concurrent writes
Hint 2
Construct a scenario: client A writes x=1, client B writes x=2 at nearly the same time
Hint 3
The write with the lower timestamp is lost forever
Hint 4
Version vectors can detect this conflict instead of silently resolving it
Hint 5
Return both values as siblings when conflict is detected
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

LWW limitationdata lossversion vectorsconflict detection
main.py
python
Demonstrate LWW Data Loss with Version Vectors - The Gossiper | Build Distributed Systems