The whole course ▾
System Design · Lesson 35 of 39 · Week 8
Design Google Docs: editing together in real time
Three people type into one sentence at once, and all end up seeing exactly the same text. Today: design Google Docs.
New System Design videos go up at 6 pm. Subscribe on YouTube so you don't miss it.
Got a doubt? Ask in the comments under the video.Subscribe
Two edits, one sentence
Welcome to the last week of lessons: senior level. We start with a system where the hard part isn't scale at all. It's two people changing the same thing, at the same moment, and both being right.
The requirements. Many people edit one document, live. Everyone sees each other's changes within a second. However the edits interleave, everyone ends up with exactly the same text. And there's a full history: any earlier version can be seen and restored. Plus the small touches, like seeing where everyone's cursor is.
Here's the problem in one word. The document says cat. Ben's browser inserts the word the, and a space, at position zero. At the same moment, Ana's browser adds an s at position three, at the end, to make cats. Ben's edit reaches the doc server first: the cat. Then Ana's edit is applied as sent, an s at position three, and the text becomes t-h-e-s, space, cat. Both edits arrived, and the result is wrong.
Why did it break? Ana's position three was correct in the version she saw, cat. It's wrong in the version the server has now, the cat. The fix isn't to lock the document, because people type a few characters a second, and nobody wants to wait their turn. The fix is to adjust positions. Ben added four characters before Ana's spot, so her s belongs at position seven: the cats.
Operational transformation
That adjusting has a name: operational transformation, usually called OT. It's what Google Docs has used since the beginning.
Every edit names the version it was made on: Ana's says, insert s at three, based on version ten. The server puts all edits into one order: Ben's became version eleven. When Ana's arrives, still based on version ten, it's transformed past every edit she hadn't seen: Ben inserted four characters before her position, so three becomes seven. The server sends everyone the same sequence of edits, and every browser ends with the same text.
There's a catch. OT works because one place decides the order of every edit to a document. Each browser also applies its own edits instantly, so typing never lags, and adjusts when the server's order comes back. The transformation rules for rich text, with styles, tables and comments, are notoriously hard to get right, which is why most teams use a proven library rather than writing their own.
CRDTs
There's a second family of solutions, newer, and built so that copies can merge without any central server at all.
OT relies on a central server to put edits in order. A CRDT, a conflict-free replicated data type, takes a different route: every character gets a unique, permanent ID, and inserts are placed relative to their neighbours' IDs, not by number. Then any two copies of the document, however they were edited, merge into exactly the same result, in any order. That makes CRDTs ideal for apps that work offline for hours, or sync directly between devices.
Which one should you choose? For a server-centred editor like Google Docs, OT is proven. For offline-first apps, CRDT libraries like Yjs and Automerge are the modern choice. Their price is extra metadata, an ID for every character, which they work hard to compress. In an interview, name both, choose one, and say why.
One server per document
Now the architecture. Whichever method you choose, each document needs a home while people are editing it.
Every editor's browser keeps a WebSocket open, from lesson eighteen. The load balancer routes by document ID, with consistent hashing from lesson thirteen, so every editor of document forty-two lands on the same session server, which holds that document in memory and orders its edits. Each edit is appended to an edit log before it's acknowledged and broadcast, so nothing is lost. And every few hundred edits, a snapshot of the whole document is saved.
Opening a document is three steps. Load the latest snapshot. Apply the few edits made since it was saved. Then join the live session, and receive edits as they happen. And the version history comes free: any past version is a snapshot plus the edits after it, so restoring version one hundred is just replaying the log up to that point.
Cursors, gaps and failures
Three more details make it feel like magic, instead of a demo.
Cursors and selections are sent live through the session server, and never stored: if one is lost, the next update replaces it a moment later. Edits made offline, in a train tunnel, are queued in the browser, and sent on reconnect, where they're transformed like any late edit. And if a session server dies, the document moves to another server, which loads the latest snapshot and the edit log, and editors reconnect. Nothing acknowledged is lost.
Notice the two kinds of data. Edits are precious: written to the log before anyone is told they happened. Presence, cursors and who's online, is throwaway: it's sent and forgotten. Treating them differently keeps the system both safe and fast, and it's a distinction senior engineers make without thinking.
In the interview
Design Google Docs is a senior favourite, because there's a real algorithm inside, not just boxes.
Say it like this: editors connect over WebSockets; every document is routed to one session server, which orders edits with operational transformation, or merges them with a CRDT; each edit is appended to a log before it's acknowledged and broadcast; snapshots make loading fast and give version history; and presence travels live, without being stored.
Then expect these. Why not lock the paragraph being edited? People type a few characters a second, and nobody wants to wait their turn. OT or CRDT? Server-centred, OT; offline-first, CRDT. And a thousand editors on one document? One session server can still order the edits, but broadcasting every cursor to everyone gets expensive, so cursors are sent less often, or only to people looking at that part.
The common mistake: treating the document as one value, saved with last write wins, or a version check. Every save would erase someone's typing. Collaborative editing needs edits merged, not documents replaced.
Quick check
The text is "cat". Ben inserts "the " at 0, then Ana's "s" at 3 arrives. Where does the s go?
Show the answer
Position 7: shifted by Ben's 4 characters, giving "the cats".
Why route every editor of one document to the same server?
Show the answer
One place orders that document's edits, and keeps it in memory.
Remember: concurrent edits need their positions adjusted, not just applied. Use OT with a central server, or CRDTs that merge anywhere. And keep an edit log plus snapshots, while presence is sent and thrown away. Tomorrow: running millions of scheduled jobs, on time.
Lessons on weekdays, the week's revision on Saturday, a mock interview on Sunday.