Skip to content

Design real-time collaboration — and what survives a disconnect

AdvancedAsked sometimesSystem designDesignArchitecture
Asked atGoogleFigmaNotionMicrosoft
#websockets#ot#crdt#redis#consistency

Company tags are compiled from public interview reports and are signals, not guarantees.

What interviewers are testing

Collaborative editing compresses the hardest realtime problems into one room: concurrency, ordering, fan-out, persistence, and offline merge. Interviewers listen for whether you know the hard part is not WebSockets but intention — position 4 meant the old document, and replaying it against the new one corrupts meaning without any bug in the plumbing. Strong candidates separate the durable document from the ephemeral presence layer and can explain recovery without hand-waving "the server broadcasts, so it is fine".

Mental model

A document is shared mutable state at character granularity, and the whole architecture hangs on one decision: who decides the final order and meaning of edits. OT delegates that to a central authority that transforms each operation against the ones it missed. CRDTs bake identity into every atom so arrival order stops mattering entirely. Gateways, pub/sub, snapshots, and resync are plumbing arranged around that choice — get the choice wrong and the plumbing cannot save you.

Step-by-step solution

  1. Step 1: Why concurrent edits corrupt: position is a lie after edits

    The naive model — operations are positions in a string, apply them in arrival order — dies immediately in collaboration. User A, briefly offline, computes "insert 'very ' at position 4" against version 1 of the document. Meanwhile B deletes positions 4 through 8 and commits version 2. When A reconnects and the server applies insert(4), position 4 in version 2 is somewhere else entirely — inside a different word, or past the end. Nobody made a typo: "4" was a statement about an old document, and the document moved. Watch the animation: every frame is individually correct, and the sequence is still wrong. This is the staging problem every collaborative system must solve — not connection management, not fan-out, but the fact that an edit's meaning is relative to the state it was authored against.

  2. Step 2: OT or CRDT: where conflict resolution lives

    Both families fix the stale-position problem, and the interview signal is knowing what each one really is. OT keeps operations relative and adds the missing ingredient: the server receives every op, orders them per document, and transforms each against everything it missed — insert(4) becomes insert(6) because the server knows v2 shifted it. It is what Google Docs runs, it keeps clients dumb, and it demands one authoritative order per document. CRDTs instead make every character or atom carry an identity, so operations reference atoms rather than offsets and merge is commutative — arrival order does not matter, ever. That buys offline-first and peer-to-peer, at the cost of metadata per atom and tombstones for deletions. The decision rule to say out loud: with a central server and short disconnects, OT is simpler; with long offline periods or no central authority, CRDTs pay for themselves.

  3. Step 3: Fan-out: one order per document, N gateway instances

    At 100,000 concurrent users the WebSocket tier cannot hold the truth — it holds sockets. Any gateway instance accepts a keystroke, wraps it with document id, client id, and the client’s last-seen version, and routes it to the document’s lane without interpreting it. The critical scalability insight: there is no global order — there is one order per document. A document actor (or a partitioned service keyed by document id) consumes that lane, transforms and sequences each op, and broadcasts the result on a per-document pub/sub channel. Redis fans the transformed op out to every gateway, each pushing to the sockets subscribed to that document. Watch the animation: 100k users, but a single writer per document — correctness comes from the per-document lane, scale comes from having 100k documents each with their own. Sticky sessions are an optimization for locality, never a correctness mechanism, because session migration would silently break ordering.

  4. Step 4: Persistence: append per op, snapshot on idle

    Writing PostgreSQL on every keystroke is the classic mistake: a fast typist generates tens of writes per second per user, and row-level updates of a live document serialize the hot path. The working state instead lives in the ordered op log — appends are cheap — and durability comes from periodic snapshots: after the document sits idle for a debounce window, or after every N operations, a snapshot of the versioned document is written to Postgres. Each snapshot carries its version number, making it a base that the op-log tail can replay onto. The failure question lands here: crash after broadcasting an update but before persisting? The op was already committed to the log with its version, and the clients that received it hold it too — recovery replays the log tail onto the last snapshot, and the worst case degrades to "the broadcast is ahead of storage", which resync repairs. Snapshotting also compacts: once durable, the log before the snapshot can be trimmed.

  5. Step 5: Reconnection: resume by version, converge by construction

    A disconnected user is not a lost user — they are a replica that fell behind, and the resync is a delta, never a full reload that would discard their local edits. The client reconnects announcing its last applied version (for OT) or its state vector (for CRDTs — a per-replica summary of what it has seen), and the server computes exactly the missing ops. OT transforms the user’s buffered offline operations through every op they missed, in order; CRDTs merge the buffered atoms directly, because identity makes the merge order-independent. Now the bonus scenario: two users replace the same paragraph while a third deletes it. OT resolves it by definition, not by accident — the server serializes the three operations and the transform rules decide precisely how replace-vs-delete interacts, so the outcome is deterministic and testable. CRDTs resolve it structurally: the paragraph’s atoms were deleted, and replacements targeting deleted atoms are absorbed or reinsert as defined — legitimate edits are not silently dropped, they are deterministically superseded and recoverable from the log. Watch the animation: announce, delta, apply, converge.

Edge cases & traps

  • Presence, cursors, and typing indicators are ephemeral and lossy by design: never persist them as document state or replay them on reconnect — dropping a cursor is free, dropping an edit is data loss.
  • A document is an ordering domain, not the system: forcing a global order across all documents is the scalability trap — one lane per document gives correctness and 100k documents give throughput.
  • Snapshotting must never block editing: the snapshot is taken at a specific version while later ops keep appending, so the stop-the-world copy of a live structure is a latent outage.
  • Undo/redo changes meaning under collaboration: per-user undo needs per-client operation history to "remove my last edit" from a merged state — a global undo stack will silently revert other people’s work.
  • Redis pub/sub is fire-and-forget: a gateway that was down misses updates, which is acceptable only because every client can recover via version-based resync — the day resync is broken, pub/sub losses become silent divergence.

Follow-up questions

Go deeper: Explore the System Design visualizer