Skip to main content
← PROBLEMS / SYSTEM-DESIGN
An end-to-end domain learning journey

Design a Real-Time Collaborative Document
Editor (CRDTs & WebSockets).

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.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

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.

Full walkthrough · 5 stages
01
STAGE 01 · REQUIREMENTS & MATHL5 · APPLIED DESIGN

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.

02
STAGE 02 · OT VS. CRDTL5 · APPLIED DESIGN

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.

03
STAGE 03 · ENTERPRISE ARCHITECTUREL6 · SCALE & TRADE-OFFS

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.

Sync & Persistence Path

CRDT Delta Sync & Snapshot Compaction Pipeline

Local CRDT TreeIndexedDB offlineWS Room ShardHash(doc_id) fanoutAppend Op LogIdempotent deltasSnapshot CompactorPrune tombstones

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"]))
04
STAGE 04 · SCALE & MEMORY BOUNDSL6 · SCALE & TRADE-OFFS

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.

05
STAGE 05 · INTERVIEW FOLLOW-UPSL7 · STAFF / PRINCIPAL

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 · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why do sequence CRDTs (RGA / Yjs) anchor character insertions to neighbor IDs like (lamport_ts, client_id) instead of integer array indexes?

Recommended Learning Flow