The note of order #12 reads shirts by hand. Mrs. Rini adds , skip fabric softener at the end of that note from the front counter. In the same second, Eko adds 2 at the front from the tablet by the washers. Both additions must stay. This is the problem of collaborative editing (editing together): several people change the same text from different places, and in the end every screen must show the same text with no typing lost. At Mrs. Rini's Laundry, every order has an order note field like this one.
Think of each of them holding a photocopy of the same note. They write on their own copy, then send each other what they wrote. Each photocopy is the copy of the note on one screen. The trouble: "letter 14" on Mrs. Rini's copy can be letter 16 on Eko's, because Eko has already added 2 letters at the front. A CRDT solves it by giving every letter a permanent name. So people say "after letter 14@rini" (the 14th letter Mrs. Rini typed), not "at position 14".
Last-write-wins: one edit disappears
The easiest way: whenever someone finishes typing, the browser sends the whole field, and the server keeps whatever arrives last. This is called last-write-wins (whoever writes last wins), LWW for short:
// browser
input.addEventListener('change', () => socket.emit('note:save', { id: 12, text: input.value }))
// server
socket.on('note:save', ({ id, text }) => { notes.set(id, text); io.to('staff').emit('note', { id, text }) })
Guess first: with this code, what does the note say once Mrs. Rini and Eko have both saved?
| Time | Who | Sends the whole text | On the server |
|---|---|---|---|
| 09:41:02 | Mrs. Rini | shirts by hand, skip fabric softener |
Mrs. Rini's version |
| 09:41:03 | Eko | 2 shirts by hand |
Eko's version |
The result is 2 shirts by hand: Eko's text overwrites Mrs. Rini's. Mrs. Rini's words are gone with no error message, and Mr. Darto's laundry may get fabric softener after all. LWW is only safe for values that really are replaced whole, such as a phone number or an order status.
Send operations, not the whole text
A second way is cheaper and overwrites nothing: send operations, only what changed. Mrs. Rini sends insert(14, ", skip fabric softener"), which means: insert that text after the first 14 letters. Eko sends insert(0, "2 "), at the front. But look at what happens if each screen applies those positions as they are:
| Screen | Before | Applies | Result |
|---|---|---|---|
| Mrs. Rini | shirts by hand, skip fabric softener |
insert(0, "2 ") |
2 shirts by hand, skip fabric softener |
| Eko | 2 shirts by hand |
insert(14, ", skip ...") |
2 shirts by ha, skip fabric softenernd |
On Eko's tablet, the 2 in front moves every letter 2 places to the right, so position 14 falls in the middle of by hand. The two screens differ, and one of them is broken.
OT: the server shifts the position
Operational Transformation (OT) fixes an operation's position before applying it. A central server (one server that every operation passes through) decides the order: insert(0, "2 ") goes first. Mrs. Rini's operation comes later, so the server transforms it (changes its position): "2 letters were inserted before position 14, so shift to 16". It becomes insert(16, ...). Google Docs takes this approach.
The downsides: OT needs a central server that orders every operation. OT also needs a transform function for every pair of operations (insert against delete, delete against delete). Those functions are known to be hard to write correctly. OT without a central server exists in research papers, but in practice there is almost always a server in the middle.
CRDT: every letter has a name
A CRDT (Conflict-free Replicated Data Type) is a data structure (a way to store data) whose copies may each be changed on their own. The copies are then merged in any order, and the final result is guaranteed to be the same on every copy. The problem a CRDT solves: conflicts, when several users change the same data at the same time. For text, the idea:
- Every letter has a permanent id:
number@site. A site is the copy where the letter was typed (rinioreko). Mrs. Rini typedshirts by hand, so its letters' ids are1@rinito14@rini. A new letter's number is one more than the largest number that site has ever seen (a Lamport clock). - An insert names its anchor, the id of the letter right to the left of the new one: "the letter
,with id15@rini, after14@rini". Ids never change, so no position ever shifts. - When two letters are inserted after the same anchor, the larger id goes on the left (the number first; if equal, the site name that comes later in the alphabet). This deciding rule is called the tie-break. Every copy uses the same rule, so the order is the same.
- A delete does not throw the letter away, it only marks it as a tombstone (a sign that the letter was deleted). The letter stays as an anchor for other people's inserts.
This is a small text CRDT made by hand. Each letter in the array doc holds an id (c the number, s the site), ch (the letter) and deleted. An insert carries after (its anchor), and a delete carries target:
const key = (id) => `${id.c}@${id.s}` // "15@rini"
const isLarger = (a, b) => (a.c !== b.c ? a.c > b.c : a.s > b.s)
function applyOp(doc, op) { // false means: not yet, wait
if (op.type === 'delete') {
const el = doc.find((e) => key(e.id) === op.target)
if (!el) return false // the letter to delete has not arrived
el.deleted = true // tombstone
return true
}
if (doc.some((e) => key(e.id) === key(op.id))) return true // seen before
let i = 0
if (op.after) {
const a = doc.findIndex((e) => key(e.id) === op.after)
if (a < 0) return false // its anchor has not arrived
i = a + 1
}
while (i < doc.length && isLarger(doc[i].id, op.id)) i++ // tie-break
doc.splice(i, 0, { id: op.id, ch: op.ch, deleted: false })
return true
}
function receive(replica, op) { // a cause-and-effect queue
replica.queue.push(op)
for (let progress = true; progress; ) {
progress = false
for (const o of [...replica.queue]) {
if (applyOp(replica.doc, o)) { replica.queue.splice(replica.queue.indexOf(o), 1); progress = true }
}
}
}
const text = (doc) => doc.filter((e) => !e.deleted).map((e) => e.ch).join('')
applyOpreturnsfalsewhen an operation's anchor, or the letter it deletes, has not arrived yet.receivethen keeps that operation in the queue (replica.queue) and tries it again every time another operation is applied.- The
whileloop skips letters with a larger id after the anchor, so they stay on the left: that is the tie-break. - The same operation arriving twice adds nothing.
Arrival order may be random
The example in the Lab: Mrs. Rini types , skip fabric softener, which is 22 insert operations, 15@rini to 36@rini. Eko types 2 at the front (15@eko, 16@eko), then deletes by hand with 7 delete operations. The two copies exchange those 31 operations and both become 2 shirts , skip fabric softener. Eko has deleted the letter 14@rini (the d at the end of by hand), but Mrs. Rini's insert still lands after it, because that letter's tombstone is still there.
Tried: those 31 operations were sent to a third copy, Mr. Darto's phone, in 2,000 random orders. The result was the same 2,000 times, and the queue was always empty at the end. Also: a CRDT guarantees the result is the same on every copy, not that the sentence reads well. The space before the comma still needs a human to tidy it.
Tombstones and their cost
In the hand-made CRDT above, the 31-letter note is stored as 38 elements in doc, 7 of them tombstones. Type and delete one letter 1,000 times, and a 14-letter document carries 1,014 elements. Real libraries keep this cost down: Yjs (a CRDT library) throws away the content of deleted letters and merges runs of tombstones. Measured with Yjs 13.6.33 and a random clientID (each copy's number), as in real use: shirts by hand = 28 bytes. 22 letters added then deleted = 44 bytes, or 66 bytes with gc: false (the content of deleted letters is kept). 1,000 rounds of type and delete = 46 bytes.
Yjs and Automerge: the tools people really use
Do not write your own CRDT for production (an app real users use). Versions checked on 11 October 2026: yjs 13.6.33 (with y-websocket 3.1.0 to keep copies the same over WebSocket) and @automerge/automerge 3.5.0. This is the order #12 note in Yjs. A Y.Doc is one copy, and getText('note') is the text inside it:
import * as Y from 'yjs'
const rini = new Y.Doc(), eko = new Y.Doc()
rini.getText('note').insert(0, 'shirts by hand')
Y.applyUpdate(eko, Y.encodeStateAsUpdate(rini)) // eko copies the starting text
const before = Y.encodeStateVector(rini)
rini.getText('note').insert(14, ', skip fabric softener')
eko.getText('note').insert(0, '2 ')
Y.applyUpdate(eko, Y.encodeStateAsUpdate(rini, before)) // 39 bytes
Y.applyUpdate(rini, Y.encodeStateAsUpdate(eko))
console.log(rini.getText('note').toString()) // 2 shirts by hand, skip fabric softener
Y.encodeStateAsUpdateturns the content of a copy into an update (bytes that can be sent), andY.applyUpdateapplies it to another copy.beforerecords what Mrs. Rini's copy held before she typed, soY.encodeStateAsUpdate(rini, before)holds only her new typing.- Tried: both copies give
2 shirts by hand, skip fabric softener. An update that arrives before the update it builds on is kept aside (pending) until that one arrives. - In Yjs, inserts at the same spot at the same time are ordered by
clientID: the smaller one goes on the left. - In Automerge 3.5.0, text is edited with
Automerge.splice(d, ['note'], 14, 0, ', skip fabric softener')insideAutomerge.change, thenAutomerge.merge. Same result. For a plain value (such as astatus), Automerge picks one winner, and the losing value can still be read withgetConflicts. - A CRDT needs no server to merge, so it works peer-to-peer (devices send to each other directly) and offline. OT needs a central server that orders operations. That is the main difference between a CRDT and OT.
⚠️ Typing that gets lost or lands in the wrong place
- Saving the whole text with last-write-wins. Someone else's typing disappears with no error. Only fit for values replaced whole.
- Sending number positions without a transform. As soon as something is inserted in front, every later position is off.
- Throwing deleted letters away. An insert anchored on that letter loses its place. Delete means mark.
- Writing your own CRDT for production. Tie-breaks, queues, tombstones and data size are already handled by Yjs and Automerge.
TL;DR: Last-write-wins (the server keeps the last whole text that arrives) quietly drops other people's typing. Sending operations (only what changed) with number positions goes wrong as soon as something is inserted in front. OT fixes that by shifting positions, but needs a central server that orders every operation (Google Docs). A CRDT gives every letter a permanent id (15@rini: a number and the name of the copy where the letter was typed). An insert names its anchor (the id of the letter to its left). When two inserts want the same place, the larger id goes on the left. A delete only marks the letter as deleted (a tombstone). So operations may arrive in any order (2,000 random orders, 1 result) with no server, which suits peer-to-peer (devices send to each other directly) and offline use. Tombstones take space. The CRDT libraries Yjs (13.6.33) and Automerge (3.5.0) keep that down, so use them, not a hand-made one.
⚡ Last-write-wins: one edit disappears
Guess first: Mrs. Rini and Eko edit the order #12 note at the same time. The server uses last-write-wins (LWW): it keeps the whole text that arrives last. Mrs. Rini sends shirts by hand, skip fabric softener, and a second later Eko sends 2 shirts by hand. What does the note say?
2 shirts by hand. Mrs. Rini's words are gone with no error message.
Meaning: LWW is only safe for values that really are replaced whole, such as a phone number or an order status.
⚡ Number positions shift
Meaning: Sending operations (only what changed), such as insert(14, ...) from Mrs. Rini, is cheaper than sending the whole text. But on Eko's tablet the note already starts with 2 , so every letter has moved 2 places and insert(14, ...) lands in the middle of a word. OT (Operational Transformation) fixes that by shifting the position, and it needs a central server that orders operations.
⚡ CRDT: every letter has a name
Meaning: A CRDT is a data structure whose copies may each be changed on their own, then merged. For text, every letter has a permanent id, such as 15@rini (a number and the name of the copy where the letter was typed). An insert names its anchor (the id of the letter to its left). When two inserts want the same place, the larger id goes on the left. A delete only marks the letter as deleted (a tombstone), and the letter is still stored. So operations may arrive in any order, and the result is still the same.
⚡ Use Yjs, no central server needed
Write:
rini.getText('note').insert(14, ', skip fabric softener')
eko.getText('note').insert(0, '2 ')
Meaning: Yjs is a CRDT library. rini and eko are two copies of the note, and each inserts its own text. After they exchange changes, both hold 2 shirts by hand, skip fabric softener. A CRDT needs no server to merge, so it works peer-to-peer (devices send to each other directly) and offline. OT needs a central server that orders operations. That is the main difference between a CRDT and OT.