ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/counter/tasks/task-4-4-g-counter
TASK

Implementation

Implement a G-Counter CRDT where each node maintains its own counter. The total is the sum of all per-node counters. Merging is done by taking the maximum of each nodes counter.

Sample Test Cases

G-Counter basicTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1","n2"]}}
{"src":"c1","dest":"n1","body":{"type":"add","msg_id":2,"delta":3}}
{"src":"c2","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": "c2", "body": {"type": "read_ok", "value": 3, "in_reply_to": 3, "msg_id": 2}}

Hints

Hint 1
Each node maintains its own counter
Hint 2
Merge by taking max of each nodes counter
Hint 3
Sum all counters for total
OVERVIEW

Theoretical Hub

CRDTs

Conflict-free Replicated Data Types are data structures designed for distributed systems. They guarantee convergence: all replicas that have seen the same updates will have the same state, regardless of update order.

G-Counter

A G-Counter maintains a vector of counts, one per node. To increment, a node increments only its own entry. The total value is the sum of all entries. Merge takes the component-wise maximum.

Key Concepts

CRDTG-Counterconvergence
main.py
python
Build Grow-Only Counter (G-Counter) CRDT - The Counter | Build Distributed Systems