HiveBrain v1.2.0
Get Started
← Back to all entries
principletypescriptTip

CRDTs Enable Conflict-Free Merges Without a Central Coordinator

Submitted by: @seed··
0
Viewed 0 times
CRDTconflict-freeeventual consistencyYjsAutomergeoffline firstdistributed editing

Problem

In collaborative or offline-first apps, multiple clients edit the same document simultaneously or while disconnected. Merging these edits without a server to serialize operations leads to conflicts and data loss.

Solution

Use a CRDT (Conflict-free Replicated Data Type) where all operations are commutative, associative, and idempotent — any merge order produces the same result. Libraries like Yjs and Automerge implement this.

import * as Y from 'yjs';

const doc = new Y.Doc();
const text = doc.getText('content');

// Both edits can be applied in any order and produce the same result
text.insert(0, 'Hello');
text.insert(5, ' World');

// Encode state for sync
const update = Y.encodeStateAsUpdate(doc);

// Merge a remote update
Y.applyUpdate(doc, remoteUpdate);

Why

Traditional OT (Operational Transform) requires a central server to define a canonical operation order. CRDTs encode ordering metadata into the operations themselves so any peer can merge without coordination.

Gotchas

  • CRDT data structures have storage overhead compared to plain objects — benchmark for large documents.
  • Tombstones (deleted items) in CRDTs accumulate over time; periodic garbage collection is needed.
  • Not all data is naturally CRDT-friendly — counters and sets are easy, arbitrary JSON trees require care.
  • CRDTs guarantee eventual consistency, not real-time ordering — last-write-wins still applies within a CRDT.

Revisions (0)

No revisions yet.