← Lessons

quiz vs the machine

Platinum1780

Concurrency

CRDT Counters and Sets

Data types that merge automatically and converge without coordination or conflicts.

6 min read · advanced · beat Platinum to climb

Convergence without coordination

A conflict free replicated data type, or CRDT, is a structure designed so that concurrent replicas always merge into the same value, no matter the order updates arrive. There is no need for locks or a coordinator.

A grow only counter

A simple counter that only increases keeps one sub counter per replica. Each replica increments only its own entry. The value is the sum of all entries. Merging two replicas takes the element wise maximum of their entries. To allow decrements, a second grow only counter tracks decrements and the value is the difference.

A grow only set

A grow only set lets replicas add elements. Merging is simply the union of the two sets. Order does not matter and duplicates are harmless, so any merge order converges.

Why merges converge

CRDT merges are designed to be commutative, associative, and idempotent. Those three properties mean merges can be applied in any order, any number of times, and still reach the same state. Removal is harder and needs tombstones or unique tags.

Key idea

CRDTs like grow only counters and sets merge by maximum or union, which is commutative associative and idempotent, so replicas converge without any coordination.

Check yourself

Answer to earn rating on the learn ladder.

1. How does a grow only CRDT counter merge two replicas?

2. Why do CRDT merges always converge?

3. Why is element removal harder than addition in CRDTs?