← All writing

Engineering· November 22, 2024 · 14 min

The Architecture Behind Real-Time Collaborative Apps

Exploring CRDTs, operational transforms, and WebRTC data channels — everything you need to build Google Docs-level collaboration from scratch.

Why Real-Time Collaboration Is Hard

Collaborative apps like Google Docs let multiple users edit the same document simultaneously. The challenge: what happens when two users edit the same text at the same time, on different connections, with different latencies?

Operational Transforms (OT)

OT was the original solution — pioneered by Google Docs. Every edit is an "operation" (insert, delete). When two operations conflict, a transform function adjusts them so they converge to the same result.

The problem: OT is notoriously complex to implement correctly. The number of transform functions grows quadratically with the number of operation types.

CRDTs: The Modern Approach

Conflict-free Replicated Data Types (CRDTs) are data structures that can be merged automatically without conflicts. For text, this typically means assigning a unique, stable ID to every character.

User A inserts 'H' at position 0  →  {id: 'a1', char: 'H', after: null}
User B inserts 'W' at position 0  →  {id: 'b1', char: 'W', after: null}
Merged result: deterministic order based on IDs

WebRTC Data Channels

For peer-to-peer collaboration without a relay server, WebRTC data channels let browsers communicate directly. Combined with a signaling server for the initial handshake, you get low-latency sync.

Practical Libraries

  • Yjs — mature CRDT library, works with many editors
  • Automerge — JSON-based CRDT
  • PartyKit — hosted real-time infrastructure

For most teams, Yjs with a WebSocket provider is the pragmatic path.