Design a Collaborative Editor (Google Docs)
hardA collaborative editor like Google Docs, Figma, Notion, Office 365 lets multiple users edit one document simultaneously, with each user's changes appearing in others' views within tens of milliseconds, intent preserved, offline-friendly, with history.
Key Concepts
High-level design
Client opens WebSocket to doc owner.
Client sends ops with revision number.
Owner orders ops (OT) or merges (CRDT), persists, broadcasts to others.
Snapshot + op log durable.
Presence on separate channel (Redis pub/sub).
Permission checked on join.
Reconnect: replay missed ops.
Components
- WebSocket gateway with doc → owner routing.
- Doc owner service (one per doc; sharded by doc_id).
- Op log + snapshot store (Postgres / KV).
- Presence service (Redis pub/sub).
- Permission service (ACL on doc).
- Export / print pipeline (PDF, Word).
- Search indexer (async).
OT in depth
Operations: insert(pos, char), delete(pos, len).
Transform function: T(op1, op2) returns op1' that has same effect as op1 if applied after op2.
Server-mediated (Jupiter algorithm): clients send ops with revision; server transforms and broadcasts.
Convergence: all clients end up at same state.
Intent preservation: insert goes where user meant.
Hard edge cases: nested transforms, attribute conflicts.
CRDT in depth
Unique IDs per element (typically (author, counter)).
Concurrent inserts merge by ID order.
Concurrent deletes (tombstones) idempotent.
Associative + commutative merge → P2P friendly.
Cost: metadata per character; delta encoding mitigates.
Yjs (Y-CRDT), Automerge are widely-used libraries.
Trade-offs
OT: well-understood, smaller payload; hard to implement correctly.
CRDT: simpler reasoning, P2P, larger payload, harder to add later.
Single owner per doc: simple ordering; hot shard for big docs.
Distributed owners (CRDT): scales but coordination across owners needed for snapshot/sync.
Snapshot + log: bounded log size; snapshot timing matters.
Large docs: section-level CRDTs or chunked owner per section.
Real-world systems
- Google Docs: server-mediated OT (Jupiter algorithm). Battle-tested for 15+ years.
- Microsoft Word collaborative: hybrid OT-like.
- Figma: in-house CRDT-like, optimized for vector graphics; documented design talks.
- Notion: CRDT-based for blocks; uses Yjs internally for some surfaces.
- Linear, Excalidraw: CRDT (Yjs).
- Replit's multiplayer: CRDT-based for code collaboration.