Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Distributed Architecture
Lesson 20 of 24

CRDTs and operational transformation

The two families of algorithms behind real-time collaboration and offline sync.

22 min read

The collaboration problem

Two people edit the same document. Alice inserts X at position 5 while Bob deletes the character at position 3. If each applies the other's operation naively, Alice's X lands one position off in Bob's copy and the documents diverge. Real-time editors need every replica to converge to the same text, preserve each user's intent, and respond to local typing instantly, with no waiting on a network round trip. Locking a paragraph while someone edits it avoids the problem but feels broken; users expect to type in the same sentence at once.

Operational transformation

OT transforms each incoming operation against concurrent operations already applied, adjusting positions so intent is preserved: Bob's delete at 3 shifts Alice's insert from 5 to 4. Google Docs uses OT with a central server that assigns a single order to operations, which keeps the transformation rules manageable. The server is the source of truth for ordering; clients apply local edits optimistically and rebase them as server acknowledgements arrive. OT works well online with a central server and is hard to get right peer to peer.

CRDTs

Conflict-free replicated data types are designed so that merging replicas is commutative, associative and idempotent: replicas that have seen the same set of updates hold the same state, in any order, with duplicates harmless.

  • G-Counter: one counter per replica, value is the sum, merge takes the per-replica max
  • OR-Set: adds carry unique tags, so a concurrent add and remove resolve predictably
  • Sequence CRDTs (RGA, as used in Yjs and Automerge) give every character a unique, ordered id, so inserts never collide
  • No central server is needed, so they suit offline-first and peer-to-peer sync

Costs and architecture

Sequence CRDTs keep metadata per character and tombstones for deletions, so a document's memory can be several times its text size; modern libraries compress this well. A practical architecture for a collaborative editor: clients connect through a websocket gateway to a document session service that holds the live document in memory, partitioned by document id with consistent hashing so all editors of a document meet on one node. Operations are appended to a durable log for replay and version history, and snapshots are written periodically to object storage so loading never replays millions of operations.

Presence and cursors

Cursors, selections and who-is-online are ephemeral: broadcast them over pub/sub at a throttled rate (for example 10 updates per second per user) and never persist them. Mixing presence traffic into the durable operation log wastes storage and slows replay. With 50 editors in one document, unthrottled cursor updates can outnumber real edits a hundred to one, so throttle and coalesce them at the client and drop stale ones at the gateway. If a presence message is lost, the next one corrects it a moment later, so no delivery guarantee is needed.

Key takeaways

  • OT transforms concurrent operations and usually relies on a central server for ordering
  • CRDTs converge by construction, with no central coordinator, at a metadata cost
  • Route all editors of a document to one session node, and log operations durably
  • Snapshot periodically for fast loads and keep presence ephemeral

Sign in to track lessons and earn badges.

Conflict resolutionMulti-region architecture

Challenge checkpoint

Course: 0/24 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 8/10Design a Collaborative Document EditorApply CRDTs or OT, per-document session routing and a durable operation log to make concurrent edits converge.MAJORHardPRO
All 10 challenges →