Design a Collaborative Editor (Google Docs / Figma)
Many people editing the same document in real time — why naive last-write-wins fails, operational transformation with a central server, CRDTs for decentralised merging, the document session service and routing all editors of a doc to one owner, cursors and presence, persistence with operation logs and snapshots, offline editing, comments, permissions and version history.
1. Requirements
Functional
- Multiple users edit a rich-text document simultaneously; see each other's edits in real time (< 200 ms).
- Live cursors/selections and presence avatars.
- Comments and suggestions; version history; offline editing that merges later.
- Sharing with view/comment/edit permissions.
Non-functional
- Convergence: all users end with the same document; no lost edits.
- Scale: 100M documents active monthly; typical doc has 1–10 concurrent editors, some have 100+ (viewers in thousands).
- Durable: acknowledged edits never lost.
2. The core problem
3. Operational transformation
def transform_insert_insert(a, b, a_wins_ties):
"""Adjust insert a so it applies after insert b has been applied."""
if a.pos < b.pos or (a.pos == b.pos and a_wins_ties):
return a
return Insert(a.pos + len(b.text), a.text)
def transform_insert_delete(a, d):
"""Adjust insert a so it applies after delete d (removes d.length chars at d.pos)."""
if a.pos <= d.pos:
return a
if a.pos >= d.pos + d.length:
return Insert(a.pos - d.length, a.text)
return Insert(d.pos, a.text) # insertion point was inside the deleted range
Server loop (per document): receive (op, base_rev, client_op_id) → transform op against ops base_rev+1 … head → apply → head += 1 → persist → ack the sender with the new revision → broadcast to others.
Client state machine: at most one op "in flight" awaiting ack + a buffer of local ops; incoming server ops are transformed against in-flight and buffered ops (the classic Jupiter/Google Wave algorithm).
4. CRDT alternative
Each character has an ID (client, counter) and a left origin:
insert "X" with id (alice, 17) after id (bob, 4)
delete marks id (carol, 9) as tombstone
Replicas integrate ops by IDs → same order everywhere, no transformation, no central sequencer.
Libraries: Yjs, Automerge, Loro, Diamond Types.
| OT | CRDT | |
|---|---|---|
| Server role | required sequencer | optional relay/storage |
| Offline / P2P | limited (long offline = big transforms) | natural |
| Metadata overhead | low | per-character IDs, tombstones (compressed) |
| Correctness | transform functions are tricky | algebraic guarantees |
| Used by | Google Docs and other server-centric editors | Yjs/Automerge-based editors, local-first apps; Figma uses CRDT-inspired per-property merging |
5. High-level design
6. Deep dives
Presence and cursors
Ephemeral — never persisted to the op log. Clients send cursor/selection updates (throttled ~10/s) to the owner, which broadcasts them; cursors are expressed relative to character IDs or transformed like ops so they stay attached to the right text.
Large audiences
Hundreds of editors: batch broadcasts (every 50 ms), compress, and limit cursor broadcasts to visible ones. Thousands of viewers: put viewers on a read-only fan-out tier (pub/sub) fed by the owner rather than direct connections to it.
Offline editing
Client stores ops locally (IndexedDB). On reconnect it sends them with the last known revision; with OT the server transforms a potentially long sequence; with CRDTs merge is automatic. Very long divergence may need a user-facing "review changes" UI.
Permissions and comments
Permission checks at connection time and on each op type (comment-only users can only send comment ops). Comments anchor to ranges by character IDs (or are transformed), stored in a separate service.
Version history
Group ops into versions by time/session ("Edited by Alice, 3:42 PM"); restore = new ops that transform the current doc to the old state (history remains append-only).
7. Trade-offs
| Decision | Choice |
|---|---|
| Algorithm | OT with central server (mature for text) vs CRDT (offline/P2P, simpler server) |
| Ownership | consistent hashing of doc → session owner with lease |
| Persistence | op log + periodic snapshots |
| Transport | WebSocket; ops batched |
| Presence | ephemeral, throttled, best effort |
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.