How to design Google Docs
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Merging concurrent edits
- Millions of connections
- Keeping storage under control
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
The problem
Google Docs lets several people type into the same document at the same moment, shows each of them the others' changes and cursors as they happen, and keeps a history they can scroll back through. The answer turns on one decision: a single owner server per document that puts concurrent edits into one sequence and adjusts the late ones so they still land where their author meant, with an append-only log of those edits underneath. The log gives you history, recovery when an owner dies, and, folded into snapshots, fast loads for a document edited for years.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
Is this plain text, or rich text with styles, images and tables? | Rich structure multiplies the kinds of operation that have to be transformed against each other. Plain text keeps it to insert and delete, which is enough to show the mechanism. | Plain text. Say that styles would be a third operation type transformed against the other two, and move on. |
How many documents are open at once, and how many people edit one document at the same time? | The number of open connections sizes the server fleet; the number of editors on one document decides how much fan-out a single owner has to do. | Millions of people with documents open. Most documents have a single editor or a handful; some have dozens typing at once. |
How quickly does an edit need to show up on the other screens? | A target of moments rather than seconds rules out save-and-reload polling and pushes you to a persistent connection and an in-memory owner. | Low latency: within moments. Each person's own typing should appear instantly, before the server confirms it. |
Does someone who loses their connection for a while keep what they typed? | Offline edits mean a client holds unconfirmed operations and the server has to merge a batch based on an old version. | Yes, short offline periods must not lose edits. Ask how long short is; a long gap is where the merging deep dive goes next. |
What does viewing history mean: any past version, or named checkpoints? | Replaying to an arbitrary version means keeping operations; checkpoints mean keeping snapshots. The choice drives storage. | People want to scroll back through past versions. Snapshots at intervals, with finer detail for recent edits, is an acceptable reading. |
Do I need sharing, permissions, comments or suggestions? | They are separate subsystems and would eat the time you need for merging and ownership. | No. Permissions are out unless you reach them; comments and suggestions are out. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
Scale to millions of open connections | Connections are long-lived WebSockets, so the fleet is sized by how many connections and in-memory documents one server can hold, and a router must send each editor of a document to the same server. |
Every copy of a document converges to the same text | One server owns each document and fixes the order of its operations; each operation carries the version its author had applied when typing it, so the owner can transform it; clients transform incoming operations against their own unconfirmed ones. |
Edits show up for others with low latency | The owner keeps the document in memory, applies and broadcasts each operation at once, and appends to the log on the way; clients show their own typing before the server confirms it. |
Tolerate short offline periods without losing edits | Clients keep unconfirmed operations with their base version and resend them on reconnect; the owner must still hold, or be able to read, the operations since that version to transform against. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Collaboration servers for the open documents | Open connections ÷ connections one server holds in a load test, where a connection's cost is its socket buffers plus its share of the in-memory document and presence state. Example: 10 million open connections ÷ 100,000 per server = 100 servers, plus headroom so one dead owner's documents fit on the survivors. | In the example about 100 servers; the real figure comes from your own measurement, not from this page | The collaboration fleet, and why connections and in-memory state are the first limit in the common case of a single editor or a handful per document. |
Messages one owner sends for its busiest document | Editors × operations a second each × recipients. Example: 50 editors typing 2 operations a second each = 100 inbound operations a second, each sent on to the other 49 editors ≈ 4,900 outbound messages a second for that one document. | Outbound fan-out is tens of times the inbound rate on a document with dozens of editors | Why the broadcast side of the owner grows before transformation does, and why thousands of viewers on one document need follower servers for viewers rather than a bigger owner. |
Raw operation log for a document edited for years | Operations a day × days × bytes an operation. Example: 2,000 operations a day × 365 days × 5 years = 3.65 million operations; at about 40 bytes each ≈ 146 MB of raw operations, against a plain-text snapshot of a few hundred KB at most. | The log outgrows the text it describes by a large factor | Why a load starts from a snapshot rather than the first operation, and why raw operations older than the oldest kept snapshot are trimmed. |
Transforms for the first operation sent after an hour offline | Operations others committed during the gap. Example: 2 other editors × 1 operation a second × 3,600 seconds = 7,200 committed operations to transform the returning client's first operation against. | Thousands of transforms for one operation, done once by the owner | The cost of a long offline gap under OT, and why the owner needs the operations since the client's base version at hand, from memory or from the log. |
Core entities
| Entity | What it holds |
|---|---|
Document | One row per document: who made it, what it is called, and where its newest text and version live. |
Operation | One insert or delete, committed at one version of one document, in the append-only log. |
Snapshot | The whole text of a document as of one version, written by the compaction worker into object storage. |
Presence | Who has the document open and where their cursor is, held in the owner's memory and gone when they disconnect. |
The API
| Endpoint | Notes |
|---|---|
| Handled by the document service: inserts the metadata row and an empty snapshot at version zero. The id it returns is what the router hashes to pick an owner. |
| Returns the newest snapshot text and its version, plus the committed operations after it, so the client can build the current text. The version is what the client then passes to the WebSocket as its starting point. |
| One persistent connection per open document. The client sends one unconfirmed operation at a time with the version it was built on; the owner answers with the committed version and broadcasts the transformed operation to the others. Cursor updates travel on the same socket with the version they refer to, and are not written anywhere. |
| Lists kept snapshots and their timestamps, paged backwards from a version. Opening one reads that snapshot; recent versions can be rebuilt from the previous snapshot plus the operations between, as long as those operations are still kept. |
The high-level design
Step 1 of 4
One server holds the document and orders the edits
Start with the smallest thing that works. A document service handles creating documents and their metadata in a database. One collaboration server holds each open document in memory, takes editors' operations over a WebSocket, applies them in the order they arrive, and broadcasts each one to the other editors on the same document. For now it saves the current text to the database when it changes. The single sequence of operations the server produces is the thing everything else will lean on.
What breaks first
The deep dives
Merging concurrent edits. Two editors insert text at the same position in the same instant. How does the system decide which goes first, adjust the one that arrives second, and leave both screens showing identical text, including what each editor sees before the server has confirmed their own keystroke?
Millions of connections. Millions of editors hold a connection open at once. How does an edit find the other editors of its document when they may have connected to different machines, what runs out first on those machines, and what happens to a live document when the machine holding it dies?
Keeping storage under control. A document has been edited daily for years, so its edit history runs to millions of operations. What do you store, how does a fresh load open it quickly, and how does the loader join the live stream with no gap and no duplicate?
A concept that helps here: Snapshots and log compaction
A concept that helps here: Event sourcing and ledgersA concept that helps here: Queues and logsConcepts this guide uses
Sources
- Google Drive Blog: What's different about the new Google Docs, making collaboration fast
- Wikipedia: Operational transformation
- Wikipedia: Conflict-free replicated data type
- Marijn Haverbeke: Collaborative editing in ProseMirror
- Liveblocks guide: Why you can't delete Yjs documents
- ShareDB documentation: Document history
- ShareDB documentation: Presence
- Apache ZooKeeper: Overview