Media and real time · Design 21

Design Google Docs

Design Google Docs in a 45-minute interview: operational transformation vs CRDTs explained honestly, one session server per document, presence, snapshots, versions and offline edits.

AdvancedInterview priority: High60 min
On this page
  1. How to use this page
  2. 1. Clarify the requirements
  3. 2. Back-of-the-envelope estimates
  4. 3. API
  5. 4. Data model
  6. 5. High-level design
  7. 6. Deep dives
  8. 7. Bottlenecks, failures and monitoring
  9. 8. Follow-up questions
  10. Quiz

How to use this page

This page follows the order of a real system design interview. At each step there is a Your turn box: write your own answer first, then open ours and compare. Most of the boxes (gateway, database, cache) are familiar. The question that decides the interview is new to most people: two people type in the same sentence at the same moment; how does everyone end up with the same text?

PhaseTime in a 45-min interviewWhat you produce
1. Clarify requirements5 minCo-editing, cursors, history; what's out of scope
2. Estimate5 minConcurrent editors, operations/s, log size, servers
3. API5 minLoad a snapshot, a live session, the operation messages
4. Data model5 minDocuments and permissions, operation log, snapshots, routing
5. High-level design10 minRouter → one session server per document → log → snapshots
6. Deep dives10–15 minOT vs CRDTs, ownership, presence, snapshots, offline
7. Wrap up2–3 minBottlenecks, failures, monitoring

Users create documents, share them with others, and edit together in real time, seeing each other’s cursors. Changes are saved automatically with version history. Skip comments, suggestions mode, offline editing and spreadsheets.

1. Clarify the requirements

Your turn: scope it

The interviewer says: "Design Google Docs." Write the questions you'd ask, then 4–5 functional and 4–5 non-functional requirements with targets, and what you'd leave out.

Compare with our answer

Useful questions: How many people edit one document at once? Plain text or rich text? Must it work offline? Do we need full version history? Comments and suggestions?

A strong answer mentions: real-time co-editing with live cursors · autosave and version history · sharing with roles · convergence as the defining requirement · local typing instant, others' edits within ~200 ms · no acknowledged edit lost · comments and offline as stretch goals.

Functional: what it must do

  • Create and open documents
  • Share with view or edit permission
  • Real-time co-editing: Everyone sees changes live
  • Live cursors and names: Who is where
  • Autosave and version history: See or restore any earlier version

Out of scope

Comments and suggestionsOffline editingSpreadsheets and slides

Non-functional: how well it must work

QualityTargetWhy
LatencyLocal typing instant; others see it in < 200 msLag makes collaboration feel broken
ConsistencyAll editors converge to the same textEven when edits happen at the same time
DurabilityNo acknowledged edit ever lostPeople write their theses here
Availability99.9%+
ScaleMany docs; up to ~100 concurrent editors per docMost docs have 1–5 editors at once

The key word is convergence: after everyone stops typing and all messages have arrived, every copy of the document is identical. That's harder than it sounds, because each person's own keystrokes appear on their screen instantly (nobody will accept waiting for a server round trip per key), so different screens apply edits in different orders.

2. Back-of-the-envelope estimates

Your turn: the numbers

Assume 100 M daily users, 10% of them with a document open at any moment, about one operation per second per active editor (typing is batched every ~100 ms), 100 bytes per operation, and editors idle 70% of the time. Work out concurrent editors, operations/s, operation log per day, and session servers at 50 K connections each.

Compare with our answer
  • Concurrent editors: 100 M × 10% = 10 M.
  • Operations: ~1 op/s each ≈ 10 M ops/s in total, but spread across millions of documents, each seeing only a few ops per second.
  • Log: 10 M × 100 B × 86,400 × 30% active ≈ 26 TB/day. Compacting old operations into snapshots keeps this from growing forever.
  • Session servers: 10 M ÷ 50 K ≈ 200, plus headroom.

A strong answer mentions: total ops/s large but per-document rate tiny · documents as independent units to spread across servers · log growth and compaction · connections sizing the session fleet · batching keystrokes.

Assumptions: daily active users 100 M; docs open at once (share of dau) 10; operations per active editor per second 1; size of one operation 100 B.

NumberEstimateWhy it matters
Editors connected at once10 M
Operations per second10 M / sEach keystroke (batched) is an operation.
Operation log per day25.9 TBCompact old ops into snapshots to keep this manageable.
Session servers (≈ 50 K editors each)200
  • Every document has one owner session server, so its editors must be routed to the same place.
  • Typing is batched every ~100 ms into one operation to keep traffic down.

The estimate's lesson is shape, not size: 10 M ops/s sounds frightening, but no single document sees more than a handful per second. One server can own thousands of documents, so the design is "partition by document and keep each document's logic on one machine".

3. API

Your turn: the API

Write the calls and the real-time messages. What must every edit message carry so the server can merge it correctly, and what must it carry so a retry doesn't apply it twice?

Compare with our answer

A strong answer mentions: a load call returning a snapshot and its version · a WebSocket per open document · each operation carrying its base version (the version it was typed against) and a client operation id · an ack with the new version · broadcasts of other people's operations · presence messages · a revisions list.

CallRequestResponseNotes
POST /v1/docs{ title }201 { docId }
GET /v1/docs/{id}–{ snapshot, version, permissions }Initial load: latest snapshot + version number
POST /v1/docs/{id}/share{ email, role: viewer | editor }204
WS connect /v1/docs/{id}/sessionauth, versionstream of opsOne live session per open document tab
GET /v1/docs/{id}/revisions–{ revisions[] }Version history

Real-time events

DirectionEventPayload
client → serverop{ baseVersion, op: insert(pos, "abc") | delete(pos, len), clientOpId }
server → clientack{ clientOpId, version }
server → clientsop (transformed){ version, op, author }
both wayspresence{ userId, cursor, selection, colour }

An operation is a small edit description such as insert(5, "hi") or delete(2, 3) (delete 3 characters starting at position 2), not the whole document. The base version tells the server what the client saw when it made the edit; without it, the server couldn't tell which other edits it raced with.

4. Data model

Your turn: the storage

How is a document stored: as its current text, as a list of edits, or both? Where do permissions and cursor positions live?

Compare with our answer

A strong answer mentions: metadata and ACLs in a relational store · an append-only operation log keyed by (doc_id, version) as the source of truth · periodic snapshots for fast loading and history · routing from doc_id to its session server · presence in memory only.

documents Relational (Spanner / PostgreSQL)

  • doc_id, owner_id, title
  • acl: [user_id, role]
  • latest_version, latest_snapshot_id

Metadata and permissions: transactional and relational.

operation log Append-only log (Bigtable / Cassandra), keyed by doc + version

  • doc_id (partition)
  • version (clustering)
  • op, author_id, timestamp

Every change in order: the source of truth for history and recovery.

snapshots Object storage / Bigtable

  • doc_id, version → full document content

Loading a doc by replaying a million ops is too slow, so save a full copy every N versions.

session routing Coordination service (Zookeeper / etcd) or consistent hashing

  • doc_id → session_server

Every editor of a document must reach the same session server.

Comments on the data:

  • The operation log is the truth: every change, in the order the server accepted it, keyed by (doc_id, version). Appending version 1201 only if 1201 doesn't exist yet (a conditional insert) also protects against two servers both thinking they own the document (see below).
  • Snapshots are a cache of the log: the full document at version N. Loading = latest snapshot + replay of the few ops after it. This is the same idea as a database checkpoint plus its write-ahead log.
  • Cursors are not stored. They're useful for a few seconds and worthless afterwards.

5. High-level design

Your turn: draw it

Sketch what happens when Ana opens a document, and when Ana and Ben type at the same time. Where does the order of operations get decided?

Compare with our answer

Compare with the figures below. The order is decided in exactly one place: the session server that owns the document. People often put a stateless load balancer in front of interchangeable servers, which spreads one document's editors across machines with no single place to order their edits.

The architecture, then opening a document

  1. The architecture: each open document is owned by exactly one session server, chosen by a router. That server orders every incoming operation, transforms concurrent ones, appends them to an operation log, and broadcasts them. A background snapshotter saves full copies so documents load fast.
  2. Opening: the gateway checks permissions, loads the latest snapshot and any newer operations, and the router connects the tab to the one session server that owns this document.
ComponentWhat it does
GatewayAuth, permissions, WebSocket upgrade
Doc routerMaps each document to its single owning session server
Session serverOrders ops for its documents, broadcasts to editors
OT engineTransforms concurrent operations so everyone converges
Operation logAppend-only history: the source of truth
SnapshotterPeriodically writes full snapshots for fast loading and history
PresenceCursors and selections, in memory only

The edit flow, one hop at a time:

  1. Ben types; his editor applies delete(2,3) locally at once and sends it with baseVersion: 1200.
  2. The session server sees the document is at 1200, so there's nothing to transform. It appends the op to the log as 1201 and, once the write is durable, acks Ben and broadcasts to other editors.
  3. Ana's insert(5,"hi") arrives, also with base 1200. The server transforms it against 1201 (Ben's delete), getting insert(2,"hi"), appends it as 1202, acks Ana and broadcasts.

Two people type at once: one server orders and transforms

  1. Ana inserts text at position 5 while Ben deletes at position 2, both based on version 1200. The session server applies Ben’s op first, then transforms Ana’s: her position shifts because Ben’s delete came before it. Both receive the same result.

6. Deep dives

Your turn: the hard parts

Write two or three sentences on each before reading: How do concurrent edits converge? Why one server per document, and what if two servers both think they own it? How do cursors work? How does a document with 2 M edits open fast? What changes if editing must work offline?

Compare with our answer

A strong answer mentions:

  • OT with a central server that orders and transforms ops, or CRDTs with unique ids per character; their real costs.
  • A single owner per document via consistent hashing or a lease, with fencing so an old owner can't write.
  • Presence in memory, throttled, and cursor positions transformed like edits.
  • Snapshots every N ops plus a log tail; restore as a new edit.
  • Offline: a queue of local ops merged on reconnect; long divergence favours CRDTs.

Concurrent edits: why naive merging fails

Start with the failure. Both editors begin from abcdefgh. Positions count from 0.

Without coordination, the same two edits produce two documents

Ana's screen applies Ben's delete(2,3) after her own insert and removes cde; Ben's screen applies insert(5,"XY") after his delete, and position 5 now points somewhere else. The positions in an operation only mean something relative to the version it was made on. Every solution is a way of fixing that.

OptionHowCatch
Last write winsKeep whichever version of the document saved lastThrows away someone's typing
LockingOnly one person may edit a paragraph at a timeSafe, but kills live collaboration
Operational transformation (OT)A server orders ops; each op is adjusted against the ops it raced withTransform functions are subtle; in practice needs a central server
CRDTEvery character gets a unique id; ops refer to ids, so they merge in any orderExtra memory per character; some odd merges

Operational transformation: what Google Docs does

OT: the server picks one order and shifts the later operation

OT (operational transformation) keeps operations position-based and fixes the positions. A transform function takes two concurrent operations and adjusts one so it has the same intended effect after the other has been applied. For insert vs delete: if the delete removed characters entirely before the insert's position, shift the insert left by the number deleted (5 − 3 = 2); if it was after, leave it alone. There are rules for every pair (insert/insert, insert/delete, delete/delete, plus formatting).

Google Docs uses a client-server form of OT descended from the Jupiter system (as did Google Wave). The server is the single source of order:

  • The server receives an op with base version v, transforms it against every op in the log after v, appends the result as the next version, and broadcasts it.
  • Each client keeps at most one operation in flight (sent, not yet acked) and buffers new typing into a second pending op. When someone else's op arrives, the client transforms it against its own unacknowledged ops before applying it, so its screen stays consistent.

The honest catch: writing transform functions that are correct for every interleaving is notoriously hard. Several published peer-to-peer OT algorithms were later shown not to converge in some cases. A central server removes the hardest cases, because only two "histories" ever need reconciling (the server's and one client's). That's why practical OT systems have a server.

CRDTs: the alternative

CRDT: edits name character ids, so arrival order doesn't matter

A CRDT (conflict-free replicated data type) is a data structure designed so that replicas which have received the same set of operations are in the same state, whatever order they arrived in. For text, every character gets a unique id (such as (replica id, counter)); an insert says "put X after character id 5", and a delete marks ids as removed. Deleted characters stay as tombstones (hidden markers), because a concurrent insert might still refer to them.

OT (client-server)CRDT
Who decides orderThe serverNobody needs to: merges commute
Needs a server?Yes, in practiceNo: works peer-to-peer and offline, though most products still use one for storage and access control
MemoryPlain textIds and tombstones per character; modern libraries (Yjs, Automerge) compress this to a small overhead
Hard partCorrect transform functionsEfficient data structures, garbage-collecting tombstones
Long offline editsPainful: transform long op queuesNatural: merge whenever you reconnect

The choice for this design: OT with a session server per document, because we already have a server, it keeps the stored format plain and compact, and it's what Google Docs does. Say that a CRDT would be the better choice if offline editing or peer-to-peer sync becomes a core requirement.

One owner per document

The problem. If Ana's and Ben's connections land on different servers, both servers might call their op "version 1201".

OptionHowCatch
Any server, distributed lock per opEach server takes a lock on the document for each keystrokeA network round trip to a lock service per op; slow and fragile
Single owner (chosen)A router maps doc_id to one session server (consistent hashing or a lease in ZooKeeper/etcd); every editor of that doc connects thereHot documents can't be split; failover needs care

The owner holds the document in memory, orders ops, writes them to the log and broadcasts. Spreading load happens across documents, not within one.

When an owner dies, clients notice the dropped WebSocket and reconnect through the router. The new owner loads the snapshot plus log tail, and clients resend their unacknowledged ops with their base versions and client op ids. Ops that did reach the log before the crash are recognised by client op id and not applied twice.

Presence and cursors

Cursors and selections are presence: frequent, ephemeral, never stored

  1. Cursor positions are presence data: sent often, never stored, and simply dropped if a newer one arrives.

Cursors are presence data: Ana's position and selection, her name and colour. They change constantly and matter only for a moment, so they are kept in the session server's memory, sent at most every ~50–100 ms per user, and dropped if a newer update arrives first.

One detail candidates miss: cursor positions must be transformed too. If Ana's cursor is at position 120 and Ben inserts 10 characters at position 40, Ana's cursor is now at 130. Clients transform remote cursors against incoming ops exactly like edits (or, with a CRDT, anchor cursors to character ids, which move with the text automatically).

For a document with 500 viewers in a lecture, sending every cursor to everyone is 250,000 updates per round. Show only editors' cursors, throttle harder, and send presence only for the part of the document each client is looking at.

Snapshots and version history

The problem. A document edited daily for two years has 2 M operations. Replaying them all to open it takes seconds or more.

A snapshotter writes the full document every N operations (say 1,000) or after a pause in editing. Opening a document = latest snapshot + the ops since it, usually a few hundred at most.

Version history is built from the same data. The fine-grained log is grouped into revisions by author and time ("Ana, 10:42–10:58"), which is what the history panel shows. Viewing an old version = nearest snapshot + replay to that point. Restoring an old version is not a rewrite of history: it's a new operation (or set of operations) that turns the current text into the old text, appended like any other edit, so collaborators and the log stay consistent.

Keeping history affordable

At ~26 TB of ops a day, keeping every keystroke forever is expensive. A common policy: keep every op for recent weeks, then compact older history into snapshots at revision boundaries (one per editing session), and keep only those. The user still sees "every version" at the granularity they care about. Snapshots are immutable, so they go to cheap object storage; the recent log tail stays in the faster store.

Offline edits

The brief put offline out of scope, so treat it as "the requirement changes". With short disconnections OT already copes: the client keeps its unacknowledged ops, reconnects, sends them with their base version, and the server transforms them against everything that happened meanwhile.

Long offline sessions are different. A user edits on a plane for 3 hours (2,000 ops) while colleagues make 5,000 ops online. With OT, the server must transform 2,000 ops against 5,000: workable but slow, and the merged result can surprise people. CRDTs handle it naturally: the device sends its ops, the server sends the others, and both sides merge in any order. Whatever the algorithm, merging after long divergence can produce text that reads oddly, so products show who changed what and make it easy to compare versions.

7. Bottlenecks, failures and monitoring

Scaling bottlenecks

BottleneckFix
Hot document (500 viewers in a lecture)Viewers get batched broadcasts; edits still ordered by one server; read-only viewers can fan out via a pub/sub layer
Session server failureLease expires; another server loads snapshot + log and takes over
Op log growthCompact into snapshots; keep fine-grained history for recent revisions, coarser for old
Many small docsConsistent hashing spreads docs evenly; rebalance by moving idle docs

When things fail

FailureWhat happens
Session server crashesClients reconnect; new owner rebuilds state from snapshot + log; unacked client ops are resent
Client offline brieflyPending ops queued locally and transformed on reconnect
Op log write failsThe op isn’t acked, so the client keeps and retries it

What to monitor

Op round-trip latencyOps per second per docReconnect rateTransform conflictsSession server loadSnapshot age per doc

8. Follow-up questions

Your turn: the interviewer pushes

Answer in order before opening ours. 1) The owner server for a busy document crashes mid-sentence. Walk through what each editor experiences. 2) Ana removes Ben's edit access while Ben is typing. What must happen? 3) Product wants offline editing on phones as a core feature. What do you change?

Compare with our answer

A strong answer mentions:

  1. A brief "reconnecting" state; the router assigns a new owner (lease expiry, fenced); it loads snapshot + log; clients reconnect, resend unacked ops with base versions and client op ids; nothing acked is lost, duplicates are ignored.
  2. The ACL change is an event the session server subscribes to; it drops Ben to read-only or disconnects him and rejects any op from him with a later base version; ops already acked stay.
  3. Move to a CRDT document model (Yjs/Automerge-style) with local storage on the device; the server stores and relays updates and enforces access; tombstone garbage collection; UI for reviewing merged changes.
How would you add offline editing?

CRDTs make this much easier: edits merge whenever the device reconnects, in any order. With OT you’d need to transform long queues of offline ops.

How do suggestions or comments work?

They’re anchored to positions or character ids and transformed like other ops so they stay attached to the right text.

How do you show “Ana is editing” on a 1,000-viewer doc?

Throttle presence, show a sample of cursors, and only send presence to clients viewing that part of the doc.

Google Docs on one page
  • Numbers: ~10 M concurrent editors, ~10 M ops/s total but a few per document; ~26 TB of ops/day before compaction; ~200 session servers at 50 K connections.
  • Ops: insert(pos, text), delete(pos, len), each with a base version and client op id; local apply first.
  • Ownership: one session server per document via consistent hashing or lease; fencing with conditional log appends and epochs.
  • OT: server orders; transform each op against the ops since its base version; client keeps one op in flight; needs a server in practice.
  • CRDT: unique id per character, tombstones; merges in any order; better for offline and peer-to-peer; costs memory.
  • Limits: convergence ≠ intent (duplicate fixes, interleaving, rich-text edge cases).
  • Storage: op log is the truth; snapshot every ~1,000 ops; history = grouped revisions; restore = a new op.
  • Presence: in memory, throttled, never stored; transform cursor positions like edits.
Mock interviewPractise it as a mock interview: concurrent edits and ownershipOpens in the ByteAtlas lab. Your progress is saved in this browser.
Check what stuck

1. Both start from abcdefgh. Ben's delete(2,3) is ordered first; Ana's concurrent insert(5,"XY") arrives with the same base version. What does the server store for Ana?

2. A team routes each editor to any session server through a round-robin load balancer. What goes wrong?

3. Server 7 resumes after a long pause, still believing it owns doc 42, which server 9 now owns. What prevents two different version 1201s?

4. When is a CRDT clearly a better fit than server-based OT?

5. Where should live cursor positions be kept?

6. Ben's cursor is at position 120. Ana inserts 10 characters at position 40. What should Ana's screen do with Ben's cursor?

7. A document has 2 M operations in its log and takes 8 seconds to open. What is the standard fix?

Score 60% or more and the lesson is marked complete.

Passing the quiz also marks it complete. Progress is saved in this browser only.