Real-Time Collaborative Text Editing System Design
System DesignModel answerLast updated
Design a multi-user document editor with concurrent edits, convergence, offline reconnect, version history, permissions, and live presence.
You are asked to design a service that lets multiple people edit the same document simultaneously and watch each other's edits appear live. The interviewer wants an end-to-end design, not just an algorithm name.
Scope to nail down before designing
- Document model: assume plain UTF-8 text with per-character or per-run styling added later as an extension. Say explicitly what you are deferring.
- Concurrency semantics: two users type at (nearly) the same offset — what does everyone converge to?
- Offline: a client goes dark for minutes, makes edits, then reconnects. Must those edits merge, and how?
- Permissions: owner / editor / commenter / viewer, plus share links.
- History: what can be viewed, restored, or audited, and for how long?
- Presence: cursors, selections, "who's online" — is this durable or ephemeral?
What to produce
- Clarifying questions you would ask and the assumptions you would state.
- The core data model for a document and for a single edit operation.
- A convergence strategy (OT or CRDT), with a justification and its failure modes.
- The client–server synchronization protocol, including reconnection and offline replay.
- Snapshots, durable history, and recovery.
- Access control enforcement points.
- Presence handling as a separate, non-durable channel.
- Capacity estimates, bottlenecks, and what you would monitor.
Hints
Hint 1
Before choosing OT or CRDT, force the question of whether offline edits must merge — that single answer changes the whole architecture.
Hint 2
Model an edit as a small, invertible, causally-tagged operation against a known base version; then convergence becomes 'apply ops in a total order everyone agrees on'.
Hint 3
Keep presence on a separate websocket/pub-sub path with no durability; mixing cursor updates into the durable op log will dominate your write volume.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Clarify, then commit to assumptions
Questions worth asking: Do edits made offline have to merge, or can we reject stale clients? Is the document plain text or rich text with structure (tables, embeds)? Do we need true undo across users? What is the history retention window and audit requirement? What's the expected concurrent-editor count per document (5? 50? 500)? Region/branch granularity for permissions?
Assumptions I'd state: plain text, up to ~50 concurrent editors per document, offline merging required, history retained 90 days with named version snapshots kept forever, permissions at document level (sharing a single paragraph is an extension).
2. Data model
A document is a sequence of characters with a version number (or a logical clock). An operation is small, invertible, and tagged:
insert(pos, char, opId, parentVersion)delete(pos, char, opId, parentVersion)opId=(siteId, lamportCounter)— globally unique and totally orderedparentVersion= the document version the author saw when generating the op
Operations are the source of truth; the document is a deterministic fold over them.
3. Convergence: OT vs CRDT
Operational Transformation (OT): the server is the single serialization point. Clients send ops against a base version; the server transforms incoming ops against all ops committed since that base. Centralized OT (Jupiter / ot.js style) is simple to reason about and cheap in storage, but requires a stateful, sticky server per document, transformation functions must be proven correct pairwise (the TP1 property), and long offline gaps produce large transform chains.
CRDT (e.g. RGA / Yjs-style sequence CRDT): every character carries a unique id and a pointer to a predecessor; insertion after a deleted character is still well-defined, so convergence is guaranteed without transformation. No central ordering authority is required — good for offline and peer-to-peer.
I would choose a sequence CRDT because offline merging is a hard requirement and it removes the transform-chain cost on reconnect. The trade-offs I'd name: more metadata per character (id + origin ≈ tens of bytes, so use block/run compression), garbage collection of tombstones is required for long-lived documents, and interleaving anomalies exist in naive designs (mitigate with run-splitting rules as in Yjs).
4. Sync protocol
- Client keeps a local replica and an unsent op buffer.
- On edit, apply locally (optimistic), push op with
opIdandparentVersionto the server. - Server appends to the document's op log, broadcasts to subscribers, and acks with the server's current version.
- Reconnect: client sends its last known version + its pending buffer; server replies with all ops since that version. CRDT merge makes replay idempotent — drop ops whose
opIdis already in the log. - Transport: WebSocket per document with binary-encoded op batches; fall back to long-poll. Snapshot the document at intervals (every N ops or M seconds) so a cold client loads a snapshot plus a short tail rather than the full log.
Undo/redo is per-user (a stack of inverse ops), which the CRDT's invertibility gives us for free.
5. History and recovery
- Append-only op log in durable storage (e.g. Kafka → object store), partitioned by document id.
- Periodic snapshots; a version is
(snapshotId, offset). - Named versions and audit trail are pointers into the log.
- Restore = load snapshot, replay forward; "revert to version V" creates a new op that rewrites the document, preserving history rather than deleting it.
6. Access control
Enforcement at the API gateway and again at the document service — never trust the client. A per-document ACL (userId → role) with roles owner/editor/commenter/viewer; share links carry signed, scoped, expiring tokens. Read permission is checked on snapshot fetch and on WebSocket subscribe; write permission on every op batch. Revocation fans out to active sockets via a control channel and forces re-auth.
7. Presence
Cursors, selections, and typing indicators go on a separate ephemeral channel: in-memory pub/sub keyed by document, throttled to ~10 updates/sec per user, TTL-expired after ~30s of silence, never persisted. Presence must never enter the durable op log — at 50 editors it would be orders of magnitude more traffic than edits.
8. Scale, numbers, monitoring
10k documents actively edited at peak × 20 editors = 200k concurrent sockets → dozens of websocket nodes with consistent-hash routing by document id, so all editors of a document hit one node. Op throughput is modest (edits are small); the real cost is fan-out and storage of the log. Monitor: op-apply latency p99, snapshot lag, reconnect merge time, socket churn, tombstone ratio (a sign GC is falling behind), and ACL denial rates.
Common mistakes: naming CRDT/OT without connecting it to offline requirements; conflating presence with document state; forgetting that a single node must own the op ordering; no snapshot/GC strategy; and treating permissions as a client concern.