Design a Google Docs / Notion-style collaborative editor supporting 10M DAU, 100 concurrent editors per document, <80ms keystroke propagation, and seamless offline editing with automatic conflict-free merge on reconnect.
From patterns to decisions.
Building an offline-capable collaborative editor requires replacing central Operational Transformation (OT) locks with sequence CRDTs (RGA / Yjs) over sticky-routed WebSocket gateways, backed by an append-only operation log and periodic snapshot compaction.
Clarify Concurrency, Offline Sync & Latency Math.
With 10M DAU and ~500K concurrent active sockets, each typing editor emits ~3–5 keystrokes/sec (~100 bytes per binary CRDT delta). Broadcasting to 100 peers in a room requires <80 ms p99 fanout, while offline editors must buffer thousands of local edits and merge cleanly upon reconnect without losing cursor intent.
Why Operational Transformation (OT) Needs a Central Arbiter.
Classic OT transforms integer offsets (insert(5, 'a')) on a single authoritative server per document. Supporting multi-hour offline editing or multi-region active-active rooms in OT becomes notoriously bug-prone (TP2 puzzle). Sequence CRDTs (RGA / Yjs) anchor every insert to its left/right neighbor's immutable ID, making merges associative, commutative, and idempotent.
RGA/Yjs Delta Sync + Sticky WebSocket Shards + Op-Log Compaction.
Consistent-hash doc_id to a WebSocket Room Shard (with Redis Pub/Sub cross-pod fanout for failover). Clients exchange state vectors ({clientId: maxClock}) on reconnect to fetch only missing binary deltas, while a background compactor merges the append-only op log into a single base snapshot and garbage-collects deleted tombstones once all active replicas acknowledge the epoch.
CRDT Delta Sync & Snapshot Compaction Pipeline
1.State-Vector Sync: Reconnects exchange compact version vectors to stream only missing deltas.
2.Run-Length Blocks: Yjs merges contiguous typing by one author into 1 block, cutting RAM 10x.
3.Safe Compaction: Squashes op logs into a base snapshot periodically for fast cold loads.
Caption: Sticky document rooms broadcast tiny binary CRDT deltas in <80ms while background compaction bounds op-log and tombstone growth.
def compute_missing_deltas(local_clock: dict[str, int], remote_ops: list[dict]) -> list[dict]:
"""Filter operations unseen by a reconnecting client using its state vector."""
missing = []
for op in remote_ops:
cid = op["client_id"]
seq = op["seq"]
if seq > local_clock.get(cid, 0):
missing.append(op)
return sorted(missing, key=lambda o: (o["lamport"], o["client_id"]))
Taming Tombstone Bloat & Ephemeral Cursor Presence.
Never persist live cursor/selection coordinates in the CRDT op log—broadcast awareness/presence as ephemeral volatile messages over WebSockets with a 30s TTL. For deleted text, compress consecutive deletions into run-length tombstone ranges and run periodic snapshot compaction.
Permissions Revocation, Undo/Redo & Large Document Splitting.
On permission downgrade, immediately close the WebSocket and reject delta writes at the room gateway; implement local per-user undo using inverse operations scoped strictly to client_id so Ctrl+Z never undoes a collaborator's edits.
Practice: Make the trade-off yourself.
Why do sequence CRDTs (RGA / Yjs) anchor character insertions to neighbor IDs like (lamport_ts, client_id) instead of integer array indexes?