System design case study C15 · CRDT · Real-time

Design a Collaborative Document Editor (Google Docs-style)

Many people type into the same paragraph at the same time, and everyone ends up with the same document. The magic is a merge algorithm plus a careful real-time architecture.

What you'll be able to explain
  • Explain operational transformation (OT) and CRDTs
  • Route all editors of a document to one session server
  • Persist edits as an operation log with snapshots
  • Show presence and cursors cheaply
  • Enforce sharing permissions

Scope

Functional

  • Create/edit rich-text documents; multiple users editing concurrently; live cursors.
  • Comments, version history, sharing (view/comment/edit), offline editing with later merge.

Non-functional

  • Local typing feels instant (< 50 ms); remote edits appear in < 300 ms.
  • All replicas converge to the same document. No lost edits.

Size

  • 50M DAU; ~5M documents open concurrently; most have 1 editor, some have 100+.
  • Edit operations: ~10/s per active typist, batched.

The convergence problem

Alice and Bob both see "CAT". Alice inserts "S" at position 3 ("CATS"); Bob deletes position 0 ("AT"). If each simply applies the other's raw operation, Alice ends up with "ATS" but Bob inserts "S" at position 3 of "AT", which is out of range or in the wrong place. Positions must be adjusted for concurrent edits.

Operational transformation (OT) CRDTs
Idea Transform an incoming op against concurrent ops (shift positions) Give every character a unique, ordered ID; merges are commutative
Needs a central server? Practically yes (server orders ops) No: peer-to-peer and offline-friendly
Metadata overhead Low Higher (IDs, tombstones), much reduced in modern libraries
Used by Google Docs Figma-like apps, Notion-style editors, Yjs and Automerge based apps

Choice for this design: a server-ordered model (OT-style, or a CRDT with a server as the hub). The server gives a single total order, and clients rebase their pending ops on it.

The architecture

CLIENTEditor ACLIENTEditor BNETWORKWebSocket gatewayCACHEDoc routerdoc_id → sessionSERVICESession serverowns doc 42DATABASEOp logappend-onlyOBJECT STORESnapshotsSERVICEPermissionswho owns doc 42?append opssnapshot every N opscan edit?
All editors of a document connect to the same session server, which orders ops and persists them.

The 30-second summary

Say it in the interview

"Each open document is owned by one session server, found through a router. Clients apply edits locally, send ops with their base revision, and the server orders, transforms and appends them to an op log before broadcasting. Documents load from snapshot plus log. Cursors are ephemeral and throttled. On failover, a new owner rebuilds from storage and clients resend unacknowledged ops."

Follow-up questions the interviewer may ask

  1. OT or CRDTs?
  2. How do you scale to many documents?
  3. How do you handle a document with 200 simultaneous editors?
  4. How is version history stored?

Model answers to each of these are in the book.