What is a CRDT?
CRDT stands for Conflict-free Replicated Data Type - a data structure that can be replicated across multiple peers and automatically resolves conflicts without coordination. GUN uses CRDTs to ensure that when multiple peers modify data simultaneously (especially while offline), they can eventually converge to the same state without manual conflict resolution.CRDTs enable strong eventual consistency - all replicas that have received the same updates will have the same state.
Why CRDTs Matter
The Problem: Distributed Conflicts
In distributed systems, conflicts are inevitable:Traditional Approaches (Don’t Work)
Last Write Wins
❌ Unreliable - clock skew
❌ Loses data arbitrarily
Manual Merge
❌ Requires user intervention
❌ Doesn’t scale
Locking
❌ Requires coordination
❌ Doesn’t work offline
CRDTs
✅ Automatic resolution
✅ Works offline
✅ Mathematically proven
GUN’s CRDT Implementation
GUN implements state-based CRDTs using vector clocks (timestamps). The implementation is insrc/state.js:
State Vector Structure
Fromsrc/state.js:18:
How Conflict Resolution Works
Rule: Higher Timestamp Wins
GUN uses Last-Write-Wins (LWW) semantics based on state vectors:Per-Field Resolution
Conflicts are resolved per field, not per document:This per-field CRDT allows concurrent updates to different fields without conflicts.
Deduplication
Fromsrc/state.js:13, GUN checks states before accepting updates:
State Synchronization
When peers sync, they exchange state vectors:Sync Protocol
Merge Algorithm
From the source, merging is field-by-field:High-Resolution Timestamps
Fromsrc/state.js:5-9, GUN generates sub-millisecond timestamps:
GUN can handle 999 writes per millisecond on a single peer without timestamp collisions.
Clock Skew and Drift
The Problem
Different devices have different clocks:GUN’s Solution: Drift Adjustment
CRDT Guarantees
GUN’s CRDT implementation provides:1. Eventual Consistency
2. Commutativity
3. Associativity
4. Idempotence
Limitations
1. Last-Write-Wins Semantics
GUN uses LWW-Register CRDT, which means:2. Tombstones
Deleting data requires tombstones:3. Causal Consistency
GUN doesn’t guarantee causal order:Advanced: Custom CRDTs
You can implement custom CRDT logic:Counter CRDT
Set CRDT
Best Practices
1
Design for eventual consistency
Don’t assume immediate consistency. Design UIs that show “syncing” states.
2
Use per-field updates
Update fields individually to minimize conflicts.
3
Avoid counters with LWW
Use custom counter CRDT or operational transforms for counters.
4
Handle clock skew
Ensure devices have synchronized clocks (NTP).
Debugging CRDTs
Inspect State Vectors
Track Conflicts
Performance
- State overhead: ~16 bytes per field (timestamp)
- Merge cost: O(fields) per node
- Memory: Minimal, states stored with data
- Network: States sent with every update (+~16 bytes/field)
GUN’s CRDT overhead is minimal. The trade-off for automatic conflict resolution is worth it in most distributed applications.
Further Reading
- CRDTs: Consistency without concurrency control (Shapiro et al.)
- GUN’s Conflict Resolution (gun.eco)
- State-based CRDTs
Next Steps
Graph Database
Back to graph fundamentals
API Reference
Explore GUN’s API