Skip to content

How to design a collaborative document editor (frontend)

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

On this page
Asked at
  • Meta
  • Adobe
  • Accolite
  • Tencent
Practice this question

The problem

The web client for a document that several people edit at once: your own typing shows at once, other people's arrives as they type, each person's cursor is drawn where they are, and the design works to keep typed work through a crossed edit or a short dropped connection, naming the limits where silent merging stops being safe. The answer turns on one choice: a keystroke is a tiny step against a document tree, stamped with the version number the client had seen, and the server puts steps into one order while each client moves its own unconfirmed steps over whatever arrived first. Presence rides the same connection but stays apart from the document, and unsent steps live in device storage, so a tunnel or a closed tab usually doesn't lose them.

Clarifying questions

AskWhy it mattersA typical answer

Plain text, or rich text with headings, lists and bold?

Plain text lets a position be a single number. Rich text needs a tree of blocks with marks, and each step addresses a place in that tree, which changes the document model from the first minute.

Rich text: a tree of blocks with inline marks. Images and tables inside the document are out of scope, as are comments and suggested edits.

How many people are in one document, and how many of them are typing at the same moment?

It sizes the presence layer (how many cursors to draw) and the editing path (how often steps cross on the wire and have to be moved over each other).

Up to 100 people with the document open, about 50 of them editing at the same moment. The figure follows a public cap of 100 on one shared file and a design range of 10 to 50 editing together.

Is there a server that can put edits in one order, or must this work peer to peer?

A central order makes versions a plain counter and the merge a matter of mapping positions. Without a server, the common choice is to give each character its own lasting id so changes combine in any order, at the cost of extra bytes on each character; transforming changes against each other with no referee is possible but harder to implement.

There is a server. Pick either approach, but say what you give up. A direct link from each editor to each other editor would be far more than this question needs.

How fast must my own keystroke appear, and how fast must it reach the others?

The first figure rules out waiting for the server before painting a character. The second tells you whether to batch steps or send each one.

Your own keystroke within 200 milliseconds, whatever the connection. No figure is given for reaching the other screens; say you'll send as soon as the connection allows and batch when steps pile up.

What does undo mean here: my own most recent edit, or whatever anyone did last?

A shared history means your undo reverses a colleague's work, which surprises both of you. Undoing your own edits means an inverted step has to be moved through the changes others made since.

Undo and redo your own edits. The reversal is mapped through the changes others made after it.

How long can someone be disconnected and still expect a clean merge?

Minutes in a tunnel and a week on a plane are different problems. Short gaps merge silently by mapping positions; long gaps can scramble a sentence both people reworked and need the person to choose.

Design for short gaps: a dropped connection, a closed laptop reopened at home. Treat a week offline as a follow-up question.

The requirements menu

RequirementScopeWhy

Open a document and edit its text with several other people at the same time

Core

The product. Everything else in the round hangs off how a pair of edits to one piece of text are reconciled.

See other people's edits appear as they type

Core

Remote steps must flow through the same path as local ones, so the view redraws for either without a reload.

See where each other person's cursor and selection are, with their name

Core

Presence is its own layer: it travels with the edits but is kept out of the saved document.

Keep typing through a dropped connection and have the work saved when it returns

Core

It decides where unconfirmed steps live (on the device) and what the client asks for on reconnect.

Undo and redo your own edits

Core

In a shared document a plain rollback to an earlier state would wipe other people's work, so undo has to be an inverted step mapped forward.

Show who has the document open, as a list of names and colors

Optional

Take it: it falls out of the presence layer for almost no extra work, and it is the fallback when cursors are too many to read.

Jump to where another person is editing by clicking their name

Optional

Take it if presence is already mapped through edits, since the position is then reliable. Leave it if the cursors layer is still raw positions.

Keep editing for days with no connection and merge afterwards

Optional

Leave it unless asked. Silent merging suits short gaps; a long gap needs conflicts shown to the person, which is a different design.

Paste rich content from other applications with its formatting

Optional

Take it if time remains, and not before: pasted markup must be parsed into the document tree and cleaned, which is its own deep dive.

Chat between the people in the document

Out of scope

A separate feature with its own storage; it shares the connection at most.

Comments and suggested edits

Out of scope

Both anchor to ranges that must survive edits, a real problem, but a second document model on top of the first.

Version history

Out of scope

The server's ordered log of steps makes it possible later; the client needs just the current version.

Sharing and permissions

Out of scope

Who may open or edit is decided before the editor loads; the client assumes it was allowed in.

Images and tables inside the document

Out of scope

Tables add nested blocks and images add uploads; both would eat the time the editing path needs.

Non-functional requirements

RequirementWhat it forces

Your own keystroke shows within 200 milliseconds, whatever the connection

Each step is applied to the local document tree and painted before anything is sent. The network is kept off the path between a key press and the character on screen, which is why a pending list of unconfirmed steps exists at all.

Everyone ends with the same document once edits stop

One order for steps, or a merge that gives the same result in any order. In this design the server orders steps by version, and a client behind the server moves its own steps over the newer ones instead of applying them where they were.

No edit is lost, whether made at the same moment as another or during a dropped connection

Steps rather than whole-document saves, so a late copy can't wipe an earlier one; and unconfirmed steps written to device storage, so a tab that closes or reloads still has them to send.

Up to 100 people can have a document open at once

Presence for up to 100 people drawn in a layer over the text, each with a name and a color, and kept out of the document so cursor moves don't become document versions.

Core entities

EntityWhat it holds

Document

The shared text as the client holds it: a tree of blocks with marks, plus the version it has caught up to.

Edit

One batch of steps from one person, tagged with the document version it started from.

Pending edits

Steps applied locally that the server hasn't confirmed; they move over newer remote steps and live in device storage.

Presence

Where one person is right now: cursor, selection, name and color. Shared live, not saved.

Connection state

What the client knows about its link to the server.

The API

EndpointNotes

GET /documents/{id} → { content, version }

The initial load: the full block tree and the version it represents. The client opens the live connection with that version so the first edits are based on something the server knows.

Live connection, client to server: edit { based_on_version, steps }

Sent as soon as there are unconfirmed steps and nothing is in flight. If based_on_version is behind the server, the edit is rejected; the client applies the newer steps, rebases its own on top and sends again.

Live connection, server to client: edits from others, each with the new version; a confirmation of your own

One ordered stream. Remote steps go through the same apply path as local ones; a confirmation removes the matching steps from the pending list.

Live connection, both ways: presence { cursor, selection, name }, dropped when a person disconnects

Sent on each selection change, coalesced when several happen in one frame. Not part of the document and not stored; a client that stops refreshing is removed from each other client's presence.

Live connection, client to server: resume { last_version } → the edits missed since

Sent on reconnect before any pending steps. The missed steps bring the local document to the server's version; after that the pending steps are rebased and sent.

The high-level design

steporderedchangeremoteEditor viewDocumentConnectionServer

Step 1 of 4

An editor view, a document tree and a server

Start with the editor view bound to a local document tree: blocks with inline marks, kept apart from the page's markup. Each keystroke becomes a step applied to that tree, and the view redraws from the tree. The connection sends each change to the server, which relays it to the other clients, and remote changes come back down the same path. It already fails the moment two edits cross on the wire: a step made against one version of the text is applied to another, and the two screens drift apart.

stepselectionown stepsinverted stepunconfirmedbased_on_versionwritten as madecursor, nameothers presenceeditordered stepsnewer stepsrebaseddrawmap positionsofflineresumelast_versionEditor viewDocument treePresence stateUndo historyPending stepsConnectionDevice storageServerRemote cursorsOffline banner
The whole design

What breaks first

The deep dives

Two people typing in the same sentence. Two people type into one sentence within the same second: one adds a word at its start while the other fixes a typo near its end. Walk through how both screens come to show the same text, and where the typo fix lands.

A concept that helps here: Operational transforms and CRDTsA concept that helps here: Conflict resolution on the clientA concept that helps here: Rich text document models

Everyone's cursors. A class of 30 students opens the same document and starts typing, and a few laptops lose their connection. How does each screen show where the others are, what happens to a cursor when text is inserted before it, and what do the others see when a laptop drops off?

A concept that helps here: Heartbeats and presenceA concept that helps here: Where state lives

Typing through a dropped connection. Someone keeps writing on a train through two tunnels, closes the laptop, and opens it at home after a colleague has rewritten the same section. What happens to their words at each point?

A concept that helps here: Reconnect and resumeA concept that helps here: Offline-first storageA concept that helps here: Sync queues and client idempotency

Concepts this guide uses

Sources

  1. Marijn Haverbeke: Collaborative editing in ProseMirror
  2. ProseMirror Guide: Collaborative editing
  3. techinterview.org: Frontend system design, build a Google Docs real-time editor
  4. Yjs docs: Awareness and presence
  5. Yjs docs: Offline support with y-indexeddb
  6. Yjs protocols: sync and awareness
  7. Google Docs Editors Help: Document sharing basics
  8. MDN: Navigator.onLine
  9. web.dev: Web Vitals
Sign in to report a mistake