Build a Multi-User Live Text Editing Platform
System DesignModel answerLast updated
Design a service where many people edit one document at once, see each other's changes quickly, keep history, show live cursors, and can work offline, all while converging to one state.
Problem
Design the backend and client sync strategy for a web-based rich-text editor in which several people work on the same document simultaneously. Walk through requirements, rough capacity numbers, architecture, and the hardest technical pieces. Plan for about 40 minutes of discussion.
What the product must do
- Many users can edit one document at the same moment.
- Other people's changes show up within roughly 1-2 seconds, ideally well under 500 ms.
- If two people touch the same region, the system merges their work sensibly instead of failing or discarding either edit.
- Users can browse earlier versions of a document and restore one.
- Users can see who else is present, where their cursors are, and what text they have selected.
- Users can keep editing while offline, and their changes merge in on reconnect.
Quality targets
- Up to 100 simultaneous editors in a single document.
- Remote edits visible in under 500 ms in the common case.
- Strong eventual consistency: clients may briefly differ but must always converge to an identical document.
- No loss of acknowledged edits.
Scale assumptions
- Around 2 million concurrent editing sessions at peak, each producing about 40 operations per minute.
- Each operation is small, roughly 50-200 bytes.
- About 500 million documents, averaging 50 KB each.
What to cover
- Clarify scope and derive capacity numbers from the assumptions above.
- Define the client/server API and the data model.
- Draw the high-level architecture and explain how editors of one document are routed.
- Explain how concurrent edits are reconciled, and compare operational transformation (OT) with CRDTs.
- Describe the client-server sync protocol, including optimistic local edits.
- Cover durability, failover, version history, presence, and offline reconciliation.
- Name the main trade-offs and likely failure modes.
Hints
Hint 1
Compute the load first: 2M sessions x 40 ops/min is about 1.3M ops/s. Notice it is many tiny messages rather than heavy bandwidth, and that work can be partitioned per document.
Hint 2
If every editor of a given document is routed to a single server that assigns a global order to operations, the conflict-resolution problem becomes much simpler. Think about how that affects what properties your transform function needs.
Hint 3
Treat ephemeral data (cursors, who is online) differently from durable data (operations, snapshots). Ask what must survive a crash, when you may acknowledge an edit, and how a reconnecting client proves which edits it already sent.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Core idea
The central question is how concurrent edits converge to one document. Storage, presence, and history are standard components. I would settle the convergence mechanism first and shape everything else around it.
1. Requirements and numbers
- Scope: rich text only. Up to 100 editors per document; additional viewers are read-only.
- Latency: local edits are instant (applied optimistically); remote edits arrive in under 500 ms at p99.
- Consistency: replicas that have applied the same set of operations are identical.
- Durability: once the server acknowledges an edit, it is never lost.
Estimates:
- Peak: 2M x 40 / 60 is about 1.3M ops/s.
- Inbound: 1.3M x ~150 B is about 200 MB/s. Fan-out multiplies egress by the average collaborator count, typically 2-3.
- Log growth: at an average of roughly 400K ops/s x 100 B, about 3.5 TB/day, so compaction is required.
- Document state: 500M x 50 KB is about 25 TB.
Conclusion: operation-heavy, bandwidth-light, and partitionable by document. No single document needs more than one machine, which drives the design.
2. API
REST for management: create a document, fetch the latest snapshot with its revision, list versions, restore a revision.
WebSocket for the live session:
- Client to server:
join {doc_id, last_known_rev}andop {client_id, seq, base_rev, ops}. - Server to client:
ack {seq, rev}andremote_op {rev, author, ops}. - Both ways:
presence {user_id, cursor, selection}.
The pair (client_id, seq) makes resubmission after a reconnect idempotent.
3. Data model
- Documents table (PostgreSQL): id, owner, title, head revision, last snapshot revision.
- Operation log (append-only, partitioned by doc):
(doc_id, rev), author, client_id, seq, payload, timestamp. - Snapshots: metadata in PostgreSQL, content in object storage.
- Permissions: document, principal, role.
- Presence: Redis keys with a ~30 s TTL.
Document state = snapshot at revision N plus operations N+1 through head.
4. Architecture
Clients hold the editor, a local transform engine, and an IndexedDB queue. A gateway routes REST calls to a document service and WebSockets to collaboration servers.
The key decision is one owner per document. Consistent hashing on doc_id, with leases in etcd or ZooKeeper, sends all editors of a document to one collaboration server. That server is the single sequencer: it assigns increasing revision numbers, giving one total order. The hot path stays in memory. With 1.3M ops/s spread over many documents per server, a few hundred servers suffice. A background worker consumes the log and writes snapshots roughly every 100 ops or 30 seconds.
5. Conflict resolution: OT vs CRDT
OT rewrites concurrent operations against each other to preserve intent. Example: the text is abc. User A inserts X at position 1; user B deletes the character at position 2 (c), both based on revision 5. The server orders A first, then transforms B's delete to position 3. Both end at aXb.
Because a central server fixes the order, you need only the TP1 property (applying a then transformed b equals applying b then transformed a). The much harder TP2 is needed only for peer-to-peer OT.
CRDT (Yjs, Automerge, RGA) gives each character a unique ID and inserts reference neighbours, so merges commute and no central ordering is needed. The costs are per-character metadata, tombstones, memory often several times the text size, and complex garbage collection.
Recommendation: server-sequenced OT for the online path (compact, proven). Choose CRDTs if offline-first or peer-to-peer is the dominant need. A clear, justified choice matters more than which one.
6. Sync protocol
Each client tracks a pending operation (at most one in flight), a buffer of composed local edits, and its base revision. Typing is applied locally at once.
Server steps on receiving an op:
- Transform it against all ops after its base revision.
- Assign head + 1.
- Persist to the log.
- Ack the sender and broadcast to everyone else.
When a remote op reaches a client, it is transformed against pending and buffer before being applied, and those are transformed against it. Batching keystrokes every 50-100 ms cuts op volume several-fold without hurting perceived latency.
7. Durability and failover
- Ack only after replication (for example Kafka
acks=all). "Every keystroke durable" really means every acknowledged op; unacknowledged ones remain in the client queue and are resent. - On server crash: the lease expires, a new owner loads the latest snapshot and replays the log tail, clients reconnect with
last_known_revand resend pending ops, and duplicates are dropped by(client_id, seq). - Prevent split brain with a fencing token (lease epoch) on log writes so a deposed owner cannot append.
8. Versions, presence, offline
- Version history: group snapshots into user-visible versions (per session or idle gap). Compact fine-grained ops after N days; keep coarse snapshots long term. Restore is a new forward operation that sets content to the old state, so history is never rewritten and concurrent editors receive it like any other edit.
- Presence: cursors and selections broadcast every 100-200 ms through Redis pub/sub, throttled, with a 30 s TTL. It is ephemeral, so it is never written to the log. Cursor positions must be transformed against incoming ops so they do not drift.
- Offline: edits queue in IndexedDB. On reconnect the client sends
joinwith its last known revision, receives missed ops, transforms its queue against them, and resubmits. Long offline sessions make this transform chain expensive, which is where CRDTs shine.
Trade-offs and common mistakes
- OT vs CRDT: memory and simplicity versus offline and peer-to-peer ease.
- Snapshot frequency: faster loads versus more storage and write cost.
- Log retention: audit and fine-grained history versus cost.
- Hot-document risk: the single owner bounds a document to one machine; 100 editors is well within that.
- Mistakes: skipping a central order and then needing TP2, acknowledging before persisting, storing presence durably, forgetting idempotency on resend, and implementing restore by overwriting history.