Setting the stage…
Design a collaborative editor: two people, one sentence
Two people edit the same sentence at the same instant. This story watches what happens, breaks three designs on purpose, and builds the one that works, one idea at a time. Scroll to begin.
How a collaborative editor addresses text: positions
This is the whole system, small enough to hold in your head. Alice and Bob have the same sentence open, and a server sits between them. The sentence has a typo: THE CATT.
Every letter has an address: its position. The second T of CATT lives at position 7. Positions are how an editor says which letter an edit is about.
Alice deletes that T. The edit travels as a tiny message, delete position 7, to the server and on to Bob, whose copy changes the same way. While one person types at a time, positions are enough.
What goes wrong when two people edit at the same time
Now both act in the same instant. Alice deletes the typo at position 7. Bob adds SEE at the very start. The server relays each edit untouched. Time is slowed about twenty times here: a real hop takes a few dozen milliseconds, and two people typing land inside that window all the time.
Look at Bob's screen. Alice's message said position 7, and it meant the typo. By the time it arrived, Bob's insert had moved everything four places along, so position 7 was the space. The address was right when Alice wrote it and wrong when it landed.
Nothing crashed and nothing was logged. Two screens now disagree and neither one knows. This is the worst kind of failure in a replicated system: no error, two truths.
Fix one: take turns. Only the person holding a lock may type. It works, and it is miserable: every key press waits for a round trip before the letter appears. On a slow link that is the difference between typing and filing a request.
Fix two: send the whole document, and let the last copy to arrive win. Everyone agrees, which feels like success. But Alice's fix is gone and the typo is back.
Operational transformation: rewriting an edit's position
The fix starts with a server that numbers every edit in the order it handles them. Bob's reached it first, so it becomes revision 1. Alice's waits its turn. One line of order, for this one document.
Alice wrote delete position 7 for revision 0, but the server is already at revision 1: an edit she has not seen came first. So it rewrites her edit. Bob inserted four letters before position 7, so her delete becomes delete position 11. Same intention, new address.
Both screens now read SEE THE CAT. The rewrite is the whole trick: operational transformation is a rebase for edits, the same move as git rebase. Replay an edit's intention on top of whatever happened since it was written. Clients do the same to their own unsent edits when news arrives.
One more rule. If both people insert at the same spot, someone has to go first. The server's order decides: Bob's ? was numbered first, so it keeps the left spot and Alice's ! is rewritten to land after it. Every screen applies the same rule, so every screen agrees.
Why does it always converge? Draw the two orders as the two sides of a diamond. Apply Alice's edit and then Bob's rewritten one, or Bob's edit and then Alice's rewritten one. Both roads end at the same text. That is the one property every transformation function has to keep, and it is checked by test, on thousands of random pairs.
CRDTs: giving every character a permanent name
OT rewrites addresses. A CRDT removes the problem instead: stop using addresses that move. The moment a letter is typed it gets a permanent name. These eight were named o1 to o8 when the document was created, and they keep those names forever.
Edits now point at names. Alice deletes o8. Bob inserts SEE after nothing, at the start. Neither edit mentions a position, so neither can be invalidated by the other. They can arrive in any order and land in the same place. Try both orders.
A deleted letter keeps its name. Its cell stays in the document as a tombstone: invisible, but still there. It has to be. Here Bob types an S right after o8 while Alice deletes o8. If Alice's copy had thrown o8 away, it could not tell where the S goes.
Two people insert at the same spot? The names break the tie: the larger name goes first. Every copy compares the same two names and makes the same choice, with no server involved. That is why a CRDT does not need a server to be correct.
How do you know any of this is right? You do not, until you try to break it. The control below runs 5,000 random collisions through each design: two people, one to three edits each, random delivery. Count how often the screens end up different.
Operational transformation versus CRDT: what each one costs
Neither design is free. Where does order live? OT keeps it in a server, one per document. A CRDT keeps it in the names, so any copy can merge with any other. What must you remember? OT remembers the text and a short log. A CRDT remembers every letter ever typed, with its name, until it is sure everyone has seen the deletes.
Offline is where they part. An OT client that was away for a day holds a long queue that must be rewritten against everything that happened, one edit at a time. A CRDT just merges. Memory is where the CRDT pays: typing left to right can be stored as runs, but edits scattered through a document leave a tombstone behind every deletion.
So choose by constraint, not by taste. A server per document, mostly online: order from a server is simpler, and it is what Google Docs does. Offline-first or peer-to-peer: names, which is why local-first tools use CRDT libraries. Structured data like a design file: one value per property with the server picking the last write, which is how Figma describes its own system.
One room per document: how a collaborative editor scales
Zoom out. Everything you just watched is one room: one document, one point of order. A million documents are a million independent rooms, and no room ever needs to talk to another.
So scaling means placing rooms. Hash the document id to pick a host. When a host joins, only a few rooms move, which is consistent hashing again. A small directory with leases makes sure a document never has two rooms, because two sequencers would be two truths.
The limit is one very hot room. One document's order cannot be sharded, so products cap the number of editors (on the order of a hundred) and fan the stream out to thousands of viewers through a broadcast tier that never orders anything.
Two more streams, and they are opposites. Edits are written to a log before anyone is told they happened, with a snapshot every few hundred edits so a restart replays a handful, not a million. Cursors are ten times the traffic, lossy, and never stored: if one is dropped, the next replaces it.
Retries, crashes and lost acknowledgements in collaborative editing
A reply gets lost. Alice never learns that her edit was accepted, so she sends it again, which is what any reliable client does. A server that forgets what it has seen applies it twice, and the screens stop agreeing.
The fix is boring on purpose. Every edit carries an id, and the server remembers the ids it has applied. A repeat gets the same acknowledgement and changes nothing. Delivery is at least once; the effect is exactly once.
The room crashes right after it acknowledged an edit, but before it saved it. Both screens show the new letter and the record does not have it. The next person to open the document will not see it.
So save first, acknowledge second. A crash before the save means Alice was never told, so she retries and nothing is lost. A crash after the save means the edit is safe. This ordering is the write-ahead log, and it is the same rule in a database, a queue and a payment system.
The collaborative editor design in one minute
That is the whole design. Before you scroll on, say it back in your own words: positions move, so replicas silently diverge; OT rewrites edits against a server's order; a CRDT names every letter; each has a price; one room per document is how it scales; save before you acknowledge.
Say it back
- 01Positions are addresses, and addresses move when someone builds in front of you.
- 02Two screens that disagree without any error are the failure to design against: they must agree, and each edit must mean what its author meant.
- 03Operational transformation rewrites each edit past the edits it had not seen, in an order that a server decides.
- 04A CRDT gives every letter a permanent name, so edits commute and no server is needed, at the cost of tombstones.
- 05Choose by constraint: a server per document, offline or peer-to-peer, or structured data.
- 06A room per document is the unit of scale, with a log, snapshots and a broadcast tier around it.
- 07Ids make retries safe, and saving before acknowledging makes crashes safe.
Now say it out loud
Why can't two editors simply send each other positions?
A position is an address that shifts whenever someone inserts or deletes before it. The edit was right when it was written and points at the wrong letter when it lands, and nothing reports the error.
What does a server that orders edits do to an edit written against an older revision?
It rewrites the edit's position past every edit committed since that revision, so the intention survives, and then it applies and numbers the edit.
Why does a CRDT keep tombstones?
Later or concurrent edits can name a deleted letter as their anchor. Every replica has to remember where it sat until it is certain that no such edit can still arrive.
When a room accepts an edit, what comes first: the acknowledgement or the save?
The save. An acknowledgement is a promise, and a crash between the two would otherwise lose an edit that the client was told was safe.
Keep going
- Rehearse it as an interviewA 45-minute clock, four phases, and the interviewer’s pushback face down until you turn it over.
- Case study: design a collaborative editorThe written case study: requirements, estimates, the design, tested C# and the follow-up questions.
- Case study: design a chat systemA senior-level walkthrough of a messenger: connections as the unit of capacity, one writer per conversation, idempotent sends and gap repair, receipts as watermarks, group fan-out by size, reconnect storms and storage, with tested C#.
- Replication and shardingHow a database scales past one machine. Replicas for reads and failover, shards for writes and size, and the shard-key decisions that are very hard to undo.
- Consistency and availabilityWhat the CAP theorem actually says, the consistency models you can choose between, how quorums work, and how to decide which trade-off fits which feature.
Addresses. Alice's screen above and Bob's screen below both show the sentence THE CATT. A server sits between them.