Collaborative Editor (Google Docs)
Requirements: many users edit the same document concurrently, see each other's changes in ~100 ms, offline edits merge cleanly, version history, cursors/presence.
clients ==WebSocket==> collab servers (one "owner" per document, via consistent hashing on doc_id)
| apply op -> transform/merge -> assign revision -> broadcast
v
op log (append-only, per doc) ---> snapshotter (periodic snapshots) -> blob store
presence (cursors) in memory / Redis pub-sub
Deep dives
- Concurrency control: OT (operational transformation) — the server transforms concurrent ops against each other (
insert(5,"a")vsdelete(3)→ shift index) and imposes a total order; or CRDTs (e.g. RGA, Yjs/Automerge): each character has a unique, ordered id, so replicas merge without a central server (great for offline/P2P, more metadata). - Single writer per document (route all sessions of a doc to one server) simplifies ordering; fail over by replaying the op log from the last snapshot.
- Storage: op log + periodic snapshots; history = replay; compaction of old ops.
- Client keeps pending ops, acks with server revision, rebases local changes (that's OT client logic).