Design a Live Multi-User Document Editor
System DesignModel answerLast updated
Design the client and server for a collaborative text editor where multiple users edit the same document simultaneously and see each other's changes in near real time.
Live Multi-User Document Editor
You are building a web-based document editor where several people can open the same file, type at the same time, and watch each other's changes appear within a second or two — think Google Docs in spirit.
The interview targets a frontend engineer, so your answer should cover both sides of the system: the client (how it models the document, handles local edits, and applies incoming changes from others) and the server (how it coordinates those edits and keeps everyone consistent).
What you need to address
- How the document is represented and how edits are modelled
- How a user's own keystrokes appear immediately without waiting for the server
- How two users editing the same spot at the same time are resolved so no edit is lost and all clients end up with identical text
- The sync protocol between client and server: transport choice, message ordering, acknowledgement, and reconnection
- Server responsibilities: authority over ordering, storage, and scaling across many active documents
- Presence indicators: live cursors, selections, or an active-users list
Constraints
- No edit from any connected user may be silently discarded; once editing stops, every client must hold the same document.
- Local edits must feel instant — no round-trip latency visible to the typist.
Good clarifying questions to raise
Before diving in, consider asking:
- Plain text only, or does formatting (bold, lists, embedded images) need to be supported?
- How many concurrent editors per document, and how many documents are live at once?
- Is offline editing with eventual merge-on-reconnect in scope?
- Which presence features are required?
- Is version history or point-in-time restore needed?
- What permission model applies (view / comment / edit)?
Hints
Hint 1
Start by choosing a conflict-resolution strategy — Operational Transformation (OT) or CRDTs — and be ready to walk through a concrete example of two users inserting text at the same position at the same time.
Hint 2
Think in three distinct client-side buckets: the committed document state, a queue of locally applied but unacknowledged operations, and incoming remote operations that need to be rebased against that queue before being applied.
Hint 3
Cursors are positions in the document, so any operation that inserts or deletes text before a cursor must shift that cursor's index — remote cursor updates need the same transformation logic as text operations.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Clarifying assumptions
For this answer: rich-ish text (bold, italic, lists) but no embedded objects; up to ~20 concurrent editors per document; offline mode is a stretch goal; presence includes live cursors and an active-user list; version history is out of scope for now.
Conflict resolution strategy
The core problem is concurrent edits: two users send operations derived from the same document state, and both must be applied without clobbering each other.
Two mainstream approaches exist:
Operational Transformation (OT) — Each edit is an operation (insert char at index 5, delete 3 chars at index 12). The server serialises all operations, and each client transforms incoming operations against any local operations it has already applied but the server has not yet seen. Google Docs uses this model. It is well-understood but the transformation functions grow complex with rich text.
CRDTs (Conflict-free Replicated Data Types) — Each character is given a globally unique, stable identity (typically a fractional position or a Lamport timestamp pair). Insert and delete operate on identities rather than integer indices, so two concurrent inserts never collide — they just end up adjacent in a deterministic order. Figma, Linear, and several open-source editors (Yjs, Automerge) use this model. CRDTs handle offline merges naturally; the trade-off is a larger in-memory document representation and tombstones for deleted characters.
For this design, choose OT (easier to explain in an interview; server is already the authority so total ordering is straightforward).
Worked example
State: "hello" (version 5).
- User A sends:
insert("!", index 5)→"hello!" - User B sends:
insert(" world", index 5)→"hello world"
Both were derived from version 5. The server receives A first (version 6), then must transform B's operation against A's before applying it: A inserted 1 char at index 5, so B's index shifts to 6 → insert(" world", index 6) → "hello! world". All clients converge to "hello! world".
Client architecture
Document model
Represent the document as a sequence of typed spans (text + marks like bold/italic). Internally use a flat operation log so any state can be reconstructed.
Three-bucket state machine
committed — the last server-acknowledged state
in-flight — one batched operation sent but not yet acked
pending — operations typed since the last send
When the user types, the change is applied locally (optimistic UI) and appended to pending. A short debounce (50–100 ms) batches pending ops into in-flight and sends them. When the server acks, in-flight is cleared and committed advances.
When a remote operation arrives:
- Transform it against
in-flight(if any) to getremote'. - Transform
remote'againstpendingto getremote''. - Apply
remote''to the visible document — the user never sees a flicker.
Optimistic apply / rollback
Never roll back committed ops. If the server rejects an op (permission error, document deleted), revert in-flight and pending and show an error banner.
Sync protocol
Transport: WebSocket — persistent, low latency, bidirectional. Fall back to long-polling for environments that block WebSockets.
Message shape:
{ "type": "op", "docId": "abc", "baseVersion": 5, "op": { ... }, "clientId": "u42" }
The server acks with { "type": "ack", "version": 6 } or broadcasts the transformed op to other clients as { "type": "op", "version": 6, "op": { ... } }.
Reconnection: On reconnect the client sends its last known version; the server replays the operation log from that point. For long disconnections the client may receive a full snapshot instead.
Server architecture
Per-document session (authority node)
Route all connections for a given document to a single session process (use consistent hashing or a registry like Redis). This process:
- Receives ops, applies OT against its in-memory operation log, assigns a monotonically increasing version, and broadcasts to all connected clients.
- Flushes ops to a PostgreSQL (or DynamoDB) operation log asynchronously and periodically writes snapshots so replay does not start from version 0.
Storage layout
documents(id, snapshot_blob, snapshot_version, updated_at)
operations(doc_id, version, op_json, client_id, created_at)
To load a document: fetch the latest snapshot, then replay operations with version > snapshot_version.
Scaling
A single document fits on one node; horizontal scale comes from distributing documents across nodes. Session state is ephemeral — if a node crashes, clients reconnect and a new session hydrates from storage. Target: thousands of active documents across a modest cluster of 10–20 nodes.
Presence
Cursors and selections are not part of the operation log — they are ephemeral. Broadcast them over the same WebSocket as { "type": "cursor", "userId": "u42", "index": 37, "color": "#e74" }. The server fan-outs these messages to other clients in the session without persisting them.
Cursor index adjustment: any op that inserts or deletes before a stored remote cursor must shift that cursor's index by the same amount — reuse the transform function.
Active-user list: maintain a set of connected client IDs per session; send join/leave events.
Common mistakes to avoid
- Sending absolute text instead of operations — diffing full document text on every keystroke is expensive and makes transformation impossible.
- Forgetting to transform against the pending queue — remote ops land on the visible state, which includes uncommitted local changes; skipping this step causes garbled text.
- Integer indices for cursors without adjustment — a remote insert at index 3 shifts every cursor at index ≥ 3 by the insert length.
- No back-pressure on the operation log — a document edited for months accumulates millions of tiny ops; periodic snapshotting keeps replay time bounded (target < 200 ms).
Complexity note
OT transformation functions are O(n) in the number of concurrent unacknowledged ops, which in practice is tiny (< 10 ops). Document load is O(ops since last snapshot), kept small by snapshotting every few hundred ops.