Skip to content
SD-37 · Back to the write-up

Design a Collaborative Documents

One server owns each open document: it orders every edit, saves it to the op log, and sends it to everyone else, while snapshots keep opening fast.

Follow a request

Clients
Entry
Sessions
Data
EditorBrowser or app
Doc APIOpen, share, list
WS gatewaysHold sockets
Session registryDoc → owner
Collab serversOne owner per doc
Snapshot workersBackground
Metadata DBDocs and shares
Op log16 shards by doc
Snapshot storeObject storage
1user → gateway · op base 41, seq 7Ana types; her editor shows it at once and sends the batch with base revision 41.1 / 5

The numbers, at a glance

Of everyone with a document open……only about 7 in 100 are typing
typing ≈ 105,000reading ≈ 1.4M
Batching keystrokesEdit messages a second at peak
What an open readsWith a snapshot every 1,000 edits
So opening a document stays fast however long its history is.

An edit, in order

Ben’s editor is on another gateway. Ana sees her own typing at once; the ack only clears her queue.

Ana’s editor
Gateway
Collab owner
Op log
Ben’s editor
1 op base 41, seq 7
2 forward by doc_id
3 append rev 43, epoch 12
4 ok (2 of 3 copies)
5 ack 43
6 ack 43
7 remote op rev 43
RequestResponseAsync, does not block

How concurrent edits merge

The decision this question is really about.

Last write wins
Loses work
Ana savesBen saves
PUT whole doc
Ana’s edit gone
+ Simple.− Overlapping saves throw away typing.
One owner orders (OT)
Our pick
Ana opBen op
owner: rev 42, transform → 43
op log, then fan out
+ One order, small edits, history for free.− Needs a live owner per document.
CRDT
Offline-first
Ana opBen op
merge on every copy
ids on every character
+ Merges in any order, anywhere.− Bigger documents; still needs a server for access.

How it grows

Add a box only when a number forces you to. Highlighted boxes are new at each stage.

Stage 1 · ≈ 100 saves/sMake it work
Editor
App server
Database
Save the whole document; the last save wins.
Stage 2 · ≈ 10,000 editorsEdit together
Editor
Doc APIWS gateway
Collab server
Metadata DBOp log
Small edits over a WebSocket to one server that orders them and logs each.
Stage 3 · ≈ 1.5M editorsScale the sessions
Editor
Doc APIWS gateways × 43Session registry
Collab servers × 30Snapshot workers
Metadata DBOp log × 16 shardsSnapshot store
One owner per document through leases, snapshots for fast opens, 16 op-log shards.
Stage 4 · ≈ 1.5M editors + searchFind and recover
Editor
Doc APIWS gatewaysSession registry
Collab serversSnapshot workers
Metadata DBOp logSnapshot storeSearch index
Snapshots feed a search index with reader lists; old snapshots are the history.
Was this walkthrough useful?Your vote tells us what to improve next.