← back to distributed systems

CRDTs

Shapiro et al. 2011 · wpWikipedia

Conflict-free replicated data types guarantee eventual consistency without consensus. The trick: structure your data as a join-semilattice. Merging any two states produces a unique least upper bound. Merge is commutative, associative, and idempotent. No conflicts, no coordination, no locking.

Why CRDTs work

A join-semilattice is a partially ordered set where every pair of elements has a least upper bound (join). If your state is a semilattice and every update moves up in the lattice, then merging replicas always converges. Order of operations does not matter. Duplicate messages do not matter. This is the algebraic foundation of conflict-free replication.

initial A: +1 B: +2 +1 again +3 merge same result regardless of merge order

G-Counter (grow-only counter)

Each node maintains its own counter. The value is the sum of all counters. Merge takes the componentwise max. Only increments are allowed (no decrements). This is a CRDT because max is a join on the natural number lattice.

Scheme

PN-Counter

A PN-Counter supports both increments and decrements by maintaining two G-Counters: P (positive) and N (negative). The value is P - N. Merge each G-Counter independently. Decrements never cancel increments at the semilattice level; they accumulate separately.

Scheme

OR-Set (observed-remove set)

Each element gets a unique tag when added. Remove removes all currently observed tags for that element. If one replica adds and another concurrently removes, the add wins (add has a new tag the remove did not see). This gives intuitive "add wins" semantics without coordination.

Scheme
Neighbors

Cross-references