CRDTs — Conflict-Free Replicated Data Types
Data structures that merge automatically regardless of order, the maths that makes it work, the practical types, and their real costs.
#The problem they solve
Two replicas accept writes concurrently. When they sync, which wins?
The usual answers are all bad. Last-write-wins silently discards data and depends on clocks you can't trust. Surface both versions pushes the problem onto the user. Manual merge requires application logic for every data type and every conflict shape.
A CRDT — conflict-free replicated data type — offers a fourth answer: design the data structure so that concurrent updates cannot conflict in the first place. Merging is always possible, always automatic, and always lands on the same result no matter what order the updates arrive in.
That last clause is the magic. Replicas that receive the same set of updates in any order end in the same state, with no coordination at all.
#The maths
Convergence follows from three algebraic properties of the merge function:
Commutative: merge(a, b) = merge(b, a)
→ order of arrival doesn't matter
Associative: merge(merge(a,b), c) = merge(a, merge(b,c))
→ grouping doesn't matter
Idempotent: merge(a, a) = a
→ duplicate delivery doesn't matterThose three properties are exactly what "order doesn't matter, grouping doesn't matter, repeats don't matter" means formally. (The mathematical name for a structure with them is a join-semilattice, and merging is its least upper bound — but the three lines above are the whole content.)
The consequence: merging only ever moves a replica forwards, never backwards (in the counter below, each number only ever goes up), so given the same updates every replica arrives at the same place.
The practical payoff: because merge is idempotent and commutative, you can use an unreliable, unordered, at-least-once transport. Duplicates are harmless, reordering is harmless, retries are harmless. This is exactly what idempotency buys you, generalized to a whole data structure.
#Two flavours
State-based — each replica periodically sends its whole current state, and the receiver merges it in. Simple and extremely tolerant of a bad network: any subset of those messages, in any order, still converges. The cost is bandwidth when the state is large, which delta variants fix by sending only what recently changed.
Operation-based — replicas broadcast the individual operations ("x was added"). Far less data on the wire, but now the network must deliver every operation exactly once and in causal order — if operation B was made after seeing operation A, every replica must receive A before B (you can't apply "remove x" before the "add x" it refers to). Building a transport that guarantees this is real infrastructure work.
Most production systems use state-based or delta-based, because the transport requirements are so much weaker.
#The practical types
#G-Counter (grow-only)
Each replica has its own slot and only ever increments its own.
Replica A: {A:5, B:3, C:0}
Replica B: {A:2, B:7, C:1}
merge = element-wise MAX = {A:5, B:7, C:1}
value = sum = 13Element-wise max is commutative, associative, and idempotent. Increments are never lost — which is precisely what a naive counter++ on two replicas fails to guarantee. With a single shared number, both replicas start at 10 and each increments to 11. When they sync, they see two values of 11 and have no way to tell that the right answer is 12.
#PN-Counter (increment and decrement)
Two G-Counters: one for increments, one for decrements. Value = sum(P) − sum(N). You can't just decrement a G-Counter, because merging takes the max: if your slot drops from 5 to 4, the next merge sees another replica's stale copy saying 5, keeps the 5, and your decrement vanishes.
Use for: like counts, view counts, inventory estimates, vote tallies.
#G-Set / 2P-Set
A grow-only set merges by union — trivially convergent. Adding removal is where it gets interesting: a 2P-Set ("two-phase set") keeps a second grow-only set of removed elements, called tombstones — markers that say "this was deleted". An element is present if it's in the add-set and not in the tombstone set. The catch: the tombstone set can only grow too, so once an element is removed it stays removed forever and can never be re-added.
#LWW-Element-Set
Each element carries a timestamp for its add and remove. The later one wins. Simple, and inherits every problem of clock skew — so pair it with logical clocks, not wall clocks.
#OR-Set (observed-remove)
The one that actually behaves intuitively, and worth knowing by name.
Each add stamps the element with a unique tag, so "x added by A" and "x added by B" are distinguishable events rather than the same fact. A remove deletes only the tags it has actually seen — so it can't accidentally erase an add it never knew about.
A: add("x") → {x:[t1]} B: add("x") → {x:[t2]}
sync → {x:[t1,t2]}
A removes x (sees t1, t2) → removes both → {}
Meanwhile B adds x again → {x:[t3]}
merge → {x:[t3]} ← the concurrent re-add SURVIVES ✓The rule "add wins over a concurrent remove" matches what users expect: if you re-added something while someone else was deleting it, your intent shouldn't be silently erased.
#Registers
LWW-Register — a single value with a timestamp. MV-Register (multi-value) — keeps all concurrent values as siblings and lets the application decide, which is honest rather than lossy.
#Sequence CRDTs (RGA, Logoot, YATA)
The hard case: collaborative text editing, where concurrent inserts at the same position must produce a sensible order without a coordinator.
The trick is to give each character a globally unique, densely orderable identifier — typically a fractional position plus a replica ID. "Densely orderable" means there's always room for another identifier between any two. If "a" sits at position 1 and "b" at position 2, a character typed between them gets 1.5, the next one 1.75, and so on — without renumbering anything. The replica ID breaks ties when two users pick the same position at the same moment.
This is what powers modern collaborative editors built on Yjs and Automerge.
#The real costs
State them — this is where interviews go deep, and enthusiasm without cost-awareness reads as naive.
1. Metadata growth. Tombstones and per-element tags accumulate. A document edited for years carries the ghosts of every deleted character. Garbage collection requires knowing every replica has seen a change — which needs coordination, the very thing CRDTs were avoiding.
2. They converge, but not necessarily to what you wanted. Convergence is a technical guarantee, not a semantic one. Two users concurrently editing the same sentence produce a merged result that is consistent everywhere and possibly nonsense. CRDTs guarantee agreement, not intent preservation.
3. Invariants across items are impossible. This is the big one. A CRDT counter cannot enforce "balance ≥ 0" or "no more than 100 tickets sold," because each replica accepts writes without seeing the others. Any global invariant requires coordination — that's not a CRDT limitation, it's a theorem.
4. Not everything has a CRDT. Counters, sets, maps, registers, and sequences do. Arbitrary business logic with cross-entity constraints doesn't.
#Where they're used
- Collaborative editing — Figma, Linear, and editors built on Yjs/Automerge.
- Offline-first mobile apps — local writes that merge on reconnect without user-visible conflicts.
- Distributed caches and databases — Redis CRDTs (active-active), Riak data types, Azure Cosmos DB conflict resolution.
- Shopping carts — the classic Dynamo example: merging carts by union means a network partition can resurrect a removed item, but never loses an added one. That asymmetry is a deliberate business decision (a spurious extra item is a minor annoyance; a lost purchase is lost revenue), which is a great illustration that consistency choices are product choices.
- Multiplayer presence and cursors — small, high-churn, conflict-prone state.
#When to propose one
The test is a single question: is the data type mergeable, and are there no cross-item invariants?
| Requirement | CRDT? |
|---|---|
| Like/view counters across regions | ✓ PN-Counter |
| Collaborative document | ✓ Sequence CRDT |
| Offline-capable notes/tasks | ✓ OR-Set / map |
| Presence, cursors, ephemeral state | ✓ LWW-Register |
| Shopping cart | ✓ OR-Set |
| Account balance with no overdraft | ✗ needs coordination |
| Limited inventory / seat booking | ✗ needs consensus |
| Unique username assignment | ✗ needs linearizability |
The pattern in the ✗ rows: a global invariant that no single replica can verify alone. When you see one, CRDTs are the wrong tool and you need consensus or a single-writer design instead. Recognizing that boundary quickly matters more than knowing every CRDT variant.
#Check yourself
What three properties make a CRDT's merge converge?Show answer
Why can CRDTs use an unreliable, unordered network?Show answer
How does a G-Counter avoid losing increments?Show answer
Why does a PN-Counter need two G-Counters?Show answer
How does an OR-Set let a concurrent re-add survive a remove?Show answer
CRDTs versus operational transformation for collaborative editing?Show answer
Name three real costs of CRDTs.Show answer
What one question decides whether a CRDT fits?Show answer
#What to carry forward
- CRDTs make concurrent updates unable to conflict: merges are commutative, associative and idempotent.
- They tolerate unreliable, unordered, duplicated delivery.
- Know the workhorses: G- and PN-Counters, OR-Sets, registers, and sequence CRDTs for text.
- Costs: growing metadata, convergence that may not match intent, and no cross-item invariants.
- Use them for counters, carts, offline and collaborative data — never for invariants like balances or seats.