Design a Shared Document Editor (like Google Docs)
Design an editor where many people change one document at the same time. Clashing edits must be merged, and edits made offline must sync later.
Think of Google Docs like two painters working on the same canvas from far apart. When painter A adds a blue stroke and painter B adds a red stroke, an orchestrator blends the two strokes without smudging either one.
- Clients
- Editor A
- Editor B
- Entry
- WebSocket gateway
- Services
- Document service
- Snapshot worker
- Queues, caches
- Sync channel
- Data, outside
- Document store
1. Requirements and scope
- Several people edit the same document at once, and each sees where the others are typing.
- Clashing edits are merged by fixed rules, so once all edits arrive, everyone sees the same text.
- Support document revision history and offline editing.
- Sub-50ms keystroke broadcast latency.
- High availability and partition tolerance.
- Zero data loss or character transposition.
2. Back-of-the-envelope numbers
- Writes: 50,000 keystrokes a second (33%)
- Reads: 100,000 document reads a second (67%)
Balanced: reads and writes are close. Both paths need care, so neither can be slow.
3. API
4. Data model and storage
DOC_OPERATIONS: (doc_id, op_id, user_id, op_type, position, character, vector_clock)
5. The design, in three versions
Version 1: Document Lock (Pessimistic Editing)
When one user edits, the entire document is locked for all other users.
Shared Document Editor (like Google Docs): Version 1: Document Lock (Pessimistic Editing) Version 1 of 3
The picture follows one request through the parts of this version, in order. Under it, switch a part off to see what the request does without it.
One request, step by step
Each numbered arrow is one step. Press "Simulate Request Flow" to follow them one at a time.
- The order of the steps
- A request or its reply
The parts: switch one off
1. Clients
2. Services
3. Data, outside
The steps in words0 of 3 steps shown
Editor A to Document service
Editor A asks for the lock on the document.
Document service to Document store
A's edit is saved.
Document service to Editor B
Editor B must wait for the lock, then loads the new text.
Write path, read path and failures
Each path follows one request through the final design (version 3), one hop at a time.
Write pathType a character
- Client
- Local copy
- WebSocket gateway
- Operation log
- Sync channel
- Other clients
Client to Local copy
Asha types "x". Her app applies it to its own copy at once, so typing never waits on the network.
Client to WebSocket gateway
The app sends the change as a small operation: insert "x" after character id 17.
WebSocket gateway to Operation log
The server appends the operation to the document's log.
WebSocket gateway to Sync channel
It publishes the operation to every server that holds a connection for this document.
Sync channel to Other clients
Each server pushes it to its editors. Their apps merge it, and every copy ends with the same text.
Read pathOpen a document
- Client
- Document API
- Snapshot storage
- Operation log
- WebSocket gateway
Client to Document API
Ben opens the document.
Document API to Snapshot storage
The API loads the latest saved snapshot of the whole document.
Document API to Operation log
It reads the operations made since that snapshot and applies them.
Client to WebSocket gateway
Ben's app connects for live changes, starting from the last operation it has.
What breaks, and what happens then
A user goes offline while editing
- What users see
- Their changes cannot reach the server.
- What the design does
- The app keeps the operations and sends them on reconnect. They merge without overwriting anyone's work.
Two people type at the same place
- What users see
- The copies could end in a different order.
- What the design does
- Each character has a unique id, and ties are broken by a fixed rule. Every copy applies the same rule.
A WebSocket server crashes
- What users see
- Its editors are disconnected.
- What the design does
- They reconnect to another server and ask for the operations after the last one they saw.
The operation log grows very long
- What users see
- Opening the document gets slow.
- What the design does
- A worker saves a new snapshot every few minutes, so only recent operations need replaying.
6. Deep dives
7. Follow-up questions
- How do you manage CRDT memory bloat for large 500-page books? (Answer: Periodic garbage collection compaction into immutable S3 snapshot blocks with tombstones deleted).
What it costs
Open connections and memory are the cost: each open document lives in memory on a server. The change log grows by about 20 TB a year. Save a snapshot now and then, so old changes can be dropped.
Another way to design it
Operational transformation with one central server is the other well-known design. The server puts all edits in one order, so clients stay simple. A CRDT fits better when people edit offline for long periods.
8. How answers are judged
A passing answer at Mid level. A complete design with numbers: about 50,000 keystroke operations a second across all documents. It explains how the server orders conflicting edits and goes deep on one part, such as syncing or storage.
| What is judged | Junior | Mid | Senior | Staff |
|---|---|---|---|---|
| BreadthHow much of the system does the answer cover? | Names the core parts: clients, servers, a database and a cache. | Covers the whole main flow, from the request to the stored data and back. | Builds a complete design without being led through it. | Covers the system, the systems around it and who owns each part. |
| DepthHow far does it go into the hard parts? | Explains what each part does and why it is there. | Goes deep on one part, such as the database or the cache. | Goes deep on the two or three riskiest parts. | Goes deepest where the risk is largest, and explains why the risk is there. |
| DrivingWho leads the conversation? | Follows the interviewer, step by step. | Moves forward alone, with an occasional nudge. | Leads the conversation from start to finish. | Changes the problem when the question asked is not the right one. |
| Trade-offsAre choices compared and justified? | Names more than one option. | Says why one option was picked over another. | Compares options in detail, with what each one costs. | Ties each choice to cost, speed and risk for the business. |
| Unclear questionsWhat happens when the question is vague? | Answers the questions that are asked. | Asks questions to make the problem clear. | Sets the scope and says what is left out. | Questions the assumptions behind the problem itself. |
| ScopeWhat does it consider beyond the normal path? | Covers the normal path, when everything works. | Adds what happens when a part fails. | Adds growth: what breaks at ten times the load, and the fix. | Adds abuse, cost and the daily work of running the system. |
Requirements and scope
Before I design, I want to check the scope. Every document syncs edits between up to 50 concurrent editors, and offline edits must merge instead of overwriting. Does every keystroke need to reach others in under 50 milliseconds, or only the ones a user is actively watching? I will treat cursor positions as separate, lossy traffic from the edits themselves.
Multiple users open the same document and their keystrokes broadcast to each other live over a socket. Since everyone is online and editing together, I will not worry about offline support and start on the protocol.
Shows: Unclear questionsThe difference: The problem includes offline editing and merging as a requirement. Assuming everyone is always online skips a case the design must handle.
Estimates
50,000 keystroke operations a second is the write load, with reads about twice that, at 100,000 document loads a second. Each operation is about 100 bytes with its position, character and vector clock, so a year of raw log is about 160 terabytes. Snapshots that let old operations drop keep real storage near the stated 20 terabytes a year.
50,000 keystroke operations a second is the write load, so I would size the WebSocket fan-out for that same 50,000 messages a second going out.
Shows: DepthThe difference: Each keystroke broadcasts to up to 50 other editors on that document. Outbound traffic runs closer to 50 times the inbound rate, not the same as it.
Core design
Each client sends its edits over a WebSocket to the document's own server, the single source of truth for that document. The server places incoming edits in order and transforms each one against edits it has not seen yet. It then broadcasts the transformed edit to every other client. Every edit also appends to an operation log.
Each client sends its edits straight to the other connected clients over the socket. Every client applies incoming edits to its own copy of the text in the order they arrive.
Shows: DepthThe difference: Edits can reach each client in a different order, so their text differs. OT orders every operation through a central server; CRDTs use globally unique ids instead. Either fixes it.
Deep dive
The hard part is keeping every copy of the document identical without one lucky ordering. With OT, a central server orders every operation and transforms each one against what came before. I would only reach for CRDTs if offline-first editing across days mattered enough. Each character then gets a globally unique id, at the cost of far higher memory use.
When a device reconnects after being offline, I would replay its queued edits in the order it made them. Each one applies to whatever the document looks like now.
Shows: DepthThe difference: Positions recorded offline no longer match the document once others have edited it, so a replayed edit can land in the wrong place. OT or CRDTs avoid that instead.
Failures and trade-offs
If the network drops packets, cursor and presence updates stay lossy, on their channel apart from the text stream. A missed cursor position costs nothing. If a document's server crashes, a new server rebuilds its state from the last snapshot plus the operation log since. Reconnecting clients wait a short, random delay, so they do not all resync at once.
If a document's server crashes and restarts, every connected client reconnects right away and asks it to resync the whole document. All of this happens at the same moment the server is still cold.
Shows: ScopeThe difference: That reconnect storm can overload the server right as it restarts, since nothing spreads the resyncs out. A short random delay before reconnecting avoids that.
We wrote this guide in our own words, informed by public interview guides such as AlgoMaster and Hello Interview. We are not connected to either site. Interviewers differ. The guide and the example answers show the direction each level takes, not a checklist.
10. Check your design with your own AI
Write your design first. Then copy this prompt into the AI chat you already use, and add your design at the end. It asks for a score in each area, one fix for each, and two follow-up questions.
The site sends nothing anywhere. An AI review can be wrong, so check what it says against sections 8 and 9.
See the prompt
You are a system design interviewer. Review my design for the interview problem below. Problem: Design a Shared Document Editor (like Google Docs) Design an editor where many people change one document at the same time. Clashing edits must be merged, and edits made offline must sync later. What users must be able to do: - Several people edit the same document at once, and each sees where the others are typing. - Clashing edits are merged by fixed rules, so once all edits arrive, everyone sees the same text. - Support document revision history and offline editing. How well it must work: - Sub-50ms keystroke broadcast latency. - High availability and partition tolerance. - Zero data loss or character transposition. Numbers to assume: - 50 Million active documents, up to 50 concurrent editors per document. Judge my design at the Mid level. A passing answer at this level: A complete design with numbers: about 50,000 keystroke operations a second across all documents. It explains how the server orders conflicting edits and goes deep on one part, such as syncing or storage. For each area below, say whether my design is strong, fine or weak. Give one reason and one fix for each: 1. Requirements and scope 2. Estimates 3. Core design 4. Deep dive 5. Failures and trade-offs Then ask me the two follow-up questions an interviewer would ask next. Do not write a full model answer for me. My design: [Describe your parts, how one request moves through them, and why you chose each one.]