Skip to content

How to design Google Docs

The first sections are free. The deep dives, level expectations and quiz are part of Pro.

On this page
Asked at
Practice this question

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

AskWhy it mattersA 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.

The requirements menu

RequirementScopeWhy

Create a document

Core

The entry point. It produces the document id that routing, the log and snapshots are all keyed by.

Several users edit the same document at once

Core

The heart of the question: concurrent edits that have to be merged into one text.

Everyone sees the others’ changes in real time

Core

Forces a push channel and an owner that broadcasts each applied edit, rather than clients polling for a new copy.

See other users’ cursors and who is present

Core

Cheap once the connection exists, and a good test of what belongs in memory rather than in a database.

View the document’s history

Core

Decides what you store: an operation log and snapshots rather than just the latest text.

Keep typing while briefly offline, and merge the edits on reconnect

Optional

Worth taking: the non-functional requirement asks for it, and it is a natural follow-up to the merging deep dive. Say how pending operations are held and replayed.

Restore the document to a past version

Optional

Take it if history comes up: a restore is a new operation that replaces the current text with an old snapshot, so history stays linear. Leave it if time is short.

Undo your own edits while others keep typing

Optional

Interesting but deep: undo means inverting your own operation and transforming it against the edits since. Name it, and decline it unless asked.

Thousands of viewers on one document during an all-hands

Optional

The likely curveball. Park it until the owner-per-document design is on the board, then answer it with follower servers that serve viewers.

Export to PDF or Word

Out of scope

A batch job that builds the current text from the newest snapshot and the operations committed after it. Say where it reads and stop.

Full-text search across documents

Out of scope

Its own indexing pipeline, fed from snapshots. Name the feed and leave the index to another round.

Rich document structure: assume a plain text editor (Hello Interview)

Out of scope

Styles, images and tables add operation types without changing the merging mechanism.

Sharing and permission levels, unless the candidate reaches them

Out of scope

Mention an access check when the connection opens and a re-check when permissions change; the permission model itself is a separate design.

Comments and suggestions

Out of scope

Comments anchor to text positions and must move with edits, which is a whole second problem. Mention the anchoring and move on.

Non-functional requirements

RequirementWhat 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

QuantityWorkingResultWhat 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

EntityWhat 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

EndpointNotes

POST /docs { title } → { doc_id }

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.

GET /docs/{id} → { snapshot, version }

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.

WS /docs/{id}: client sends { op, base_version }, server sends { op, version } and cursor updates

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.

GET /docs/{id}/history?before= → versions

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

RESTmetadataWebSocketsaveEditorDocument serviceDocuments DBCollaborationserver

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.

RESTWebSocketmetadatalookupownerappendloadreadwriteEditorDocument serviceLoad balancerDocuments DBCoordinationserviceCollaborationserverOperation logSnapshotsCompactionworker
The whole design

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?

A concept that helps here: Operational transforms and CRDTsA concept that helps here: Conflict resolutionA concept that helps here: Optimistic updates and rollback

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?

A concept that helps here: Routing messages between stateful serversA concept that helps here: Consistent hashingA concept that helps here: Leader election and failover

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: Event sourcing and ledgersA concept that helps here: Queues and logs

Concepts this guide uses

Sources

  1. Google Drive Blog: What's different about the new Google Docs, making collaboration fast
  2. Wikipedia: Operational transformation
  3. Wikipedia: Conflict-free replicated data type
  4. Marijn Haverbeke: Collaborative editing in ProseMirror
  5. Liveblocks guide: Why you can't delete Yjs documents
  6. ShareDB documentation: Document history
  7. ShareDB documentation: Presence
  8. Apache ZooKeeper: Overview
Sign in to report a mistake