The whole course ▾

System Design · Lesson 35 of 39 · Week 8

Design Google Docs: editing together in real time

Updated 22 Oct 2026 · By Akshay, software engineer, India · 6 min read

Three people type into one sentence at once, and all end up seeing exactly the same text. Today: design Google Docs.

The video for this lesson is coming soon.

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

Two edits, one sentence
Ana's browser
insert "s" at 3
Ben's browser
insert "the " at 0
Doc server
text: cat
first
1
a moment later
2
"thes cat": wrong
What it must do
Many people edit one document, live
Everyone sees changes within a
second
Everyone ends with the same text
Full history; restore any version
WHY IT BREAKS
Eacheditwasmadeagainst
anolderversion:its
positionsmustbeadjusted,
notappliedassent.

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

Operational transformation
How OT works
1
Each edit names the version it saw
2
The server puts all edits in one order
3
Late edits are transformed past earlier ones
4
Everyone applies the same sequence
THE CATCH
OTneedsoneplacethat
orderseveryedit:a
centralserverper
document.

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

CRDTs
OT: a server orders edits
Needs a central server
Positions shift as edits arrive
Hard to use fully offline
CRDT: merge anywhere
Every character has a unique ID
Any two copies merge identically
Great offline and peer-to-peer
WHICH ONE?
Aserver-centrededitor:
OTisproven.
Offline-first:CRDTs,like
YjsorAutomerge.

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

One server per document
Editors'
browsers
Load
balancer
routes by doc
ID
Session
server
doc 42 in
memory
Edit log
every edit, in
order
Snapshots
every few
hundred edits
WebSocket
doc 42
append
edit
1
save
snapshot
Opening a document
1
Load the latest snapshot
2
Apply the edits made since
3
Join the live session

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

Cursors, gaps and failures
Details
->
Cursors: sent live, never stored
->
Offline edits queued, sent on reconnect
->
A session server dies: reload from the log
TWO KINDS OF DATA
Editsareprecious:logged
beforethey'reacknowledged.
Presenceisthrowaway:sent,
neversaved.

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

In the interview
SAY THIS IN THE INTERVIEW
OT(oraCRDT),onesession
serverperdocument,edits
loggedbeforebroadcast,
snapshotsforloading.
Follow-ups to expect
->
Why not lock the paragraph?
->
OT or CRDT?
->
A thousand editors on one
doc?
THE COMMON MISTAKE
"Lastwritewins"on
ashareddocument:
charactersvanish.

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

Quick check
?
The text is "cat". Ben inserts
"the " at 0, then Ana's "s" at 3
arrives. Where does the s go?
Position 7: shifted by Ben's 4
characters, giving "the cats".
?
Why route every editor of
one document to the same
server?
One place orders that
document's edits, and keeps it in
memory.
Remember
Concurrent edits need
adjusted positions
OT with a server, or CRDTs
anywhere
Edit log plus snapshots;
presence is throwaway
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".

Quick check

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.

Practise with flashcardsYour progress stays on this device.
New System Design videos at 6 pm

Lessons on weekdays, the week's revision on Saturday, a mock interview on Sunday.