Full answer
Requirements
Functional:
- Several users edit one document at the same time; everyone sees changes and cursors live.
- Edits made offline merge when the user reconnects.
- Version history and restore.
Non-functional:
- Changes appear for others in under about 500 ms.
- All copies converge to the same text; no edit is silently lost.
- Documents are durable once the server acknowledges an edit.
Capacity estimates
Assumptions: 1M documents being edited at peak, 1 operation per second per active document on average (typing is bursty but small), 100 bytes per operation with metadata.
- Operations: 1M per second at peak.
- Operation log: 1M x 86,400 x 100 bytes = about 8.64 TB per day if every document were busy all day; in practice far less, and snapshots let old operations be compacted.
- Connections: a few million WebSockets across gateway servers (around 50,000 to 100,000 per server).
API
GET /v1/docs/{id} -> {"snapshot": "...", "version": 1842}
WS /v1/docs/{id}/session
client -> {"base_version": 1842, "ops": [{"insert": "Hello", "pos": 120}], "client_op_id": "c7-55"}
server -> {"ack": "c7-55", "version": 1843}
server -> {"remote_ops": [...], "version": 1844, "author": "u_9"}
GET /v1/docs/{id}/history?from=..
Data model
documents: doc_id, owner, title, latest_snapshot_version.snapshots: doc_id, version, content (object storage for large docs).operations: doc_id, version (monotonic), author, ops, created_at. Append only.permissions: doc_id, principal, role.
High-level design
flowchart LR
C1[Editor A] -- "WebSocket" --> GW[Gateways]
C2[Editor B] -- "WebSocket" --> GW
GW --> RTR[Doc router]
RTR --> DS[Doc session server owning the doc]
DS --> OL[(Operation log)]
DS --> SN[(Snapshots)]
SNP[Snapshot worker] --> OL
SNP --> SNOrdering and conflict resolution
Each document has one session server at a time (chosen by consistent hashing or a lease in a coordination service). It assigns a version number to every accepted operation, which gives one global order per document.
With operational transformation (OT), a client sends operations against the version it last saw. If other operations were accepted since, the server transforms the new operation against them. Example: A inserts "X" at position 5 while B deletes position 2. After B's delete, A's position becomes 4. The server then broadcasts the transformed operation and clients apply the same transforms to their pending local edits.
With a CRDT, each character has a unique, ordered identifier, so inserts and deletes commute and any order of delivery converges. CRDTs suit offline and peer-to-peer editing but carry more metadata per character.
Bottlenecks and trade-offs
- Single owner per document makes ordering simple but limits a single document's write rate; that is fine, since even 100 editors produce a small stream.
- Session server failure: a new owner loads the latest snapshot plus later operations from the log; clients resend unacknowledged operations, and
client_op_idmakes that safe. - Snapshots every few hundred operations keep load time short; history views replay operations from a snapshot.
- Cursors and presence are sent over the same socket but never stored, since they are temporary.
- OT versus CRDT: OT needs a central server and careful transform functions; CRDTs avoid the central ordering but cost memory and make some operations (rich text formatting) harder.
By level
Fresher: Explain that each keystroke becomes a small operation sent to the server and broadcast to others. A fresher should see that two people editing at once can conflict.
Mid: Describe per-document ordering, a simple OT example and the operation log with snapshots. Handle reconnects with client operation IDs.
Senior: Compare OT and CRDTs in depth, cover owner failover, offline merges, rich text and comments anchoring. Expect a view on when to choose each method.
Key points
- One session server owns each document and orders its operations
- OT transforms concurrent operations; CRDTs make operations commute
- Append operations to a log and compact with snapshots
- client_op_id makes resending after reconnect safe
- Presence is temporary and not stored
Common mistakes
- Last-write-wins on whole documents, which loses edits
- Locking paragraphs so only one person can type
- Sending the whole document on every keystroke
- Forgetting to transform pending local operations on the client
Follow-ups
- How would you support offline editing for hours?
- How do comments stay attached to text that moves?
- How would you move a document to a new session server without a pause?
Tips
- Give a small OT example with positions
- Explain why one owner per document is enough
- Mention snapshots for load time
Evaluation rubric
- Per-document ordering design
- Correct explanation of OT or CRDT
- Durability with logs and snapshots
- Handles failure and reconnect