Skip to main content

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 in src/state.js:
Every write gets a state vector - a high-resolution timestamp:

State Vector Structure

From src/state.js:18:
Each field gets its own timestamp, not the whole document:

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

From src/state.js:13, GUN checks states before accepting updates:
If incoming state ≤ current state, the update is ignored:

State Synchronization

When peers sync, they exchange state vectors:

Sync Protocol

Merge Algorithm

From the source, merging is field-by-field:

High-Resolution Timestamps

From src/state.js:5-9, GUN generates sub-millisecond timestamps:
This allows multiple writes per millisecond with deterministic ordering:
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

GUN gradually adjusts timestamps to converge peer clocks.
Large clock skew (hours/days) can cause issues. Ensure devices have reasonably synchronized clocks (NTP recommended).

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:
For counters, use custom CRDT logic or operational transforms. LWW-Register may lose data.

2. Tombstones

Deleting data requires tombstones:

3. Causal Consistency

GUN doesn’t guarantee causal order:
For causal consistency, use version vectors (not implemented in GUN core).

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

Next Steps

Graph Database

Back to graph fundamentals

API Reference

Explore GUN’s API