AlgoViz

Design Google Docs

hard 55 min

Many people type into one document at once and all of them must end with the same text.

Video lesson · Chapter 1 of 6

Follow one keystroke

0:00 / 6:07

Theory

Read or listen first. About 1 min.

What it is

A collaborative editor is a document that several people change at the same time. Each person sees their own typing at once, and everyone else's a moment later. A service like Google Docs, Notion or Word on the web does this.

The hard promise is convergence: after all edits have travelled, every screen shows the same text, and nobody's words are lost, even when two people typed in the same place at the same moment.

Why it exists

Mailing copies of a file around makes many versions of the truth. Locking the file lets only one person work. Editing together removes both problems, but only if the machine can merge edits without asking.

The same ideas return in whiteboards, design tools, spreadsheets and code editors: give edits an order, or make them mergeable in any order.

A short history

  1. 1968

    Douglas Engelbart shows people working on one screen from two places in the Mother of All Demos.

  2. 1989

    Ellis and Gibbs publish operational transformation with the GROVE outline editor.

  3. 2006

    Google Docs appears, based on Writely, a web word processor from 2005.

  4. 2010

    Google's blog explains its new editor: operational transformation, with one unconfirmed change per client at a time.

Words to know

Operation
A small change, such as insert a word at position 5.
Revision
The number of edits the server has put in order so far.
Base revision
The revision a person was looking at when they typed.
Operational transformation (OT)
Rewriting an edit so it has the same meaning after other edits.
CRDT
A data structure whose copies can be merged in any order with the same result.
Convergence
All copies become identical once they have seen the same edits.
Presence
Who is here, and where their cursor is.
Snapshot
The whole document at one revision, saved so that it opens quickly.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Type. Shown locally at once, sent with a base revision

Understand it

Two friends, one scarf
123456One scarf, two knitters

Nobody had to stop knitting. They only had to agree which stitch came first.

Scope

What are we building?

A text editor in the browser, a service like Google Docs. Up to 100 people type in one document, see each other's cursors, share it with roles, go back to older versions and keep working without a network.

  • Edit together

    Others see a change in under 100 ms inside a region.

  • Never lose words

    An edit we confirmed is stored on several machines.

  • Go back

    See who changed what, and restore any version.

Similar applications

ServiceKnown for
NotionPages made of blocks
ConfluenceCompany wikis
Word on the webOffice documents edited together
Dropbox PaperSimple shared documents
CodaDocuments that work like apps
QuipDocuments beside sales data

The hard part

Two edits, one truth

Alice and Bob both look at Hello. Alice inserts X at position 2 and Bob inserts Y at position 4. Applied as written, one after the other, Y lands one letter too early. Shifting Bob's edit to 5 gives HeXllYo, which is what both wanted. Data versioning and causal IDs cover the general problem of concurrent change.

The finished system

Where the design ends up

The last version, not the first. The Design tab begins with one service and one database and shows what forces each extra box.

The whole system

Editors and readers reach an edge. Typing goes through the connection edge to the one owner of the document, which writes the edit log and keeps a lease; cursors go to presence, copied to Redis. Opening and sharing go to the document API, which reads roles from the metadata database and snapshots and images from object storage. The log feeds an event stream for search and for workers that send mail and push messages.
Blue is opening and sharing, pink is typing, orange is work done later.

Where each idea lives

IdeaRead
Long-lived connectionsReal-time APIs, and group chat for connections at scale
One owner, and proving the old one is goneFailure detection
Safe retriesIdempotency
Optimistic conflicts in databasesConcurrency control
How strict the order must beConsistency models
Placing documents on serversConsistent hashing
The interview, tab by tabApproach, Scope, Numbers, API, Design, Deep dives, Choices, Levels, Mock

Summary

The whole topic in one minute.

A collaborative editor sends small operations instead of documents. One owner per document orders them, transforms the late ones and stores each before confirming it. Clients show their own typing at once and use the same rules, so every screen ends equal.

Around that core sit cheaper promises: presence may be lost, readers may be a second late, search may be a minute late. Text may not.

  • Send operations with a base revision, never whole documents.
  • One owner per document gives every edit a place in one order.
  • Transform late edits so each lands where its author meant.
  • Store an edit on several machines before confirming it.
  • Cursors are disposable. Text is not.

Sources