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
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Two people typing in the same sentence
- Everyone's cursors
- Typing through a dropped connection
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
- Meta
- Adobe
- Accolite
- Tencent
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
| Ask | Why it matters | A 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. |
Non-functional requirements
| Requirement | What 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
| Entity | What 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
| Endpoint | Notes |
|---|---|
| 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. |
| Sent as soon as there are unconfirmed steps and nothing is in flight. If |
| One ordered stream. Remote steps go through the same apply path as local ones; a confirmation removes the matching steps from the pending list. |
| 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. |
| 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
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.
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.
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?
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?
Concepts this guide uses
Sources
- Marijn Haverbeke: Collaborative editing in ProseMirror
- ProseMirror Guide: Collaborative editing
- techinterview.org: Frontend system design, build a Google Docs real-time editor
- Yjs docs: Awareness and presence
- Yjs docs: Offline support with y-indexeddb
- Yjs protocols: sync and awareness
- Google Docs Editors Help: Document sharing basics
- MDN: Navigator.onLine
- web.dev: Web Vitals