CRDT (Conflict-Free Replicated Data Types)
What CRDTs Solveβ
CRDTs allow multiple nodes to update shared data concurrently without coordination, locks, or a master node. They guarantee Strong Eventual Consistency (SEC) even with:
- Offline edits
- Network delays
- Duplicates
- Outβofβorder messages
- Partitions
Useful for offlineβfirst apps, collaborative editing, distributed counters, edge systems, IoT, etc.
CRDT Mathematical Foundationsβ
CRDTs rely on three properties so all replicas converge without needing a global order.
Commutativeβ
Order of operations doesnβt matter.
a β b = b β a
Associativeβ
Grouping doesnβt matter.
(a β b) β c = a β (b β c)
Idempotentβ
Applying same update multiple times gives same result.
a β a = a
These guarantee deterministic convergence despite unreliable networks.
Types of CRDTsβ
State-basedβ
- Replicas periodically send entire state (or delta state)
- Merge = deterministic function (e.g., max)
- Needs eventual delivery but tolerant to message loss
- Examples: G-Counter, PN-Counter, OR-Set
Operation-basedβ
- Replicas send operations: insert, delete, increment
- Requires reliable causal-broadcast (or deduplication)
- More compact than state-based
CRDT Countersβ
G-Counter (Grow Only Counter)β
Used when only increments happen.
Structureβ
Each replica stores a number in a vector:
Replica A: [5, 0, 0]
Replica B: [0, 3, 0]
Replica C: [0, 0, 2]
Merge (max element-wise)β
merge(A, B) = [5, 3, 0]
merge(result, C) = [5, 3, 2]
Final valueβ
sum = 5 + 3 + 2 = 10
PN-Counter (Positive-Negative Counter)β
Supports increments + decrements.
Structureβ
Two G-Counters:
- P: increments
- N: decrements
Exampleβ
P = [5, 1] // total increments
N = [2, 0] // total decrements
Final valueβ
value = sum(P) - sum(N) = 6 - 2 = 4
- Used in: Likes, IoT counts, quotas, rate limits
Counter Correctnessβ
- Counters are eventually correct if all updates eventually sync.
- During partitions, temporary differences are normal.
Text CRDTsβ
Text CRDTs model documents as sequences of items. Each insertion produces a unique identifier so replicas can converge.
Imagine 3 users editing the same text:
Initial text:
H E L L O
Two concurrent inserts:
- User A inserts "X" after position 2
- User B inserts "Y" after position 2
Each insert given a unique ID:β
A inserts X@idA
B inserts Y@idB
Merge rule:β
Sort by unique ID (timestamp + replica ID)
Example ordering:
idA < idB
Final text:
H E X Y L L O
All replicas converge.
Yjs Internals (Most Practical Text CRDT Today)β
Item Structureβ
Each inserted character has:
id: { clientID, clock }leftreferencerightreferencecontent
Insertβ
Stores new item with references to neighbors. Sorting by ID resolves conflicts.
Deleteβ
Marks item as deleted (tombstone), not removed.
Merge / Syncβ
Operations are lightweight binary deltas. Replicas converge deterministically.
Used Inβ
- Figma
- JupyterLab
- Notion-like apps
- VS Code collab plugins
- Next.js Live
Real-World Use Cases of CRDTsβ
Likes / Reactions (Facebook, Instagram)β
- Distributed βLikeβ updates converge without double counting.
IoT Countersβ
- Sensors increment local counters and sync later.
Edge Rate Limitingβ
- Cloudflare-like distributed environments maintain usage totals globally.
Collaborative Text Editingβ
- Google Docs-style editing without a central lock.
Distributed Quotas (API calls, storage limits)β
- Global usage sums converge even across regions.