Positions Are Ids, Not Integers

Two replicas, Hello! and Hello there, merging to Hello there!
Replica A appended an exclamation. Replica B inserted a word. A correct merge keeps both, in either arrival order.

Two people had the same note open. Both typed. We stored the last write, and the sentence the other person had just finished was gone. Last-write-wins is a reasonable rule for a cursor. It is a bad rule for the document.

Operational transformation, the approach behind the early collaborative editors, fixes the clash by sending every keystroke through one server that rewrites operations so they still mean the right thing. That works. The server is then required. Two laptops on a train, syncing when the tunnel ends, do not have that server in the middle of the tunnel.

A conflict-free replicated data type takes the other deal. Each replica holds a state. Merge is a join. If the join obeys three laws, replicas that have seen the same updates land on the same document with nobody elected to break ties.

merge(x, y) = merge(y, x)

merge(x, merge(y, z)) = merge(merge(x, y), z)

merge(x, x) = x

Commutative, associative, idempotent. The third one is the one we forgot on the first try. A retried update applied the same insert twice, and a word appeared twice. If merge cannot be repeated safely, it is not finished.

The bug, in one sentence

Start from Hello. Person A appends !. Person B inserts there after the fifth character. If those positions are integers, B's edit says "at index 5". A's edit moves every index after it. Apply B first and you get Hello there!. Apply A first, without rewriting B, and the exclamation sits in the middle of the word B just added, or the index falls off the end. The result depends on which packet arrived first. That is the whole bug.

The fix is to stop using the index as identity. Each character gets an id that does not move when someone else types. "Insert after id h5" means the same thing after the other person has typed. The index is a number you compute when you paint the screen. It is not the thing you replicate.

What we shipped

Yjs already assigns those ids. We deleted our list.

import * as Y from 'yjs'

const doc = new Y.Doc()
const text = doc.getText('body')
text.insert(0, 'Hello')
// the other peer, once it has seen "Hello"
text.insert(5, ' there')

The integer in insert is a convenience at the call site. The stored structure is ids. The WebSocket relay (y-websocket in our case) does not parse the text. It forwards updates. A relay that does not understand the document is replaceable. A relay that runs the merge is a second editor you now have to debug.

Cursors and "Ada is here" do not belong in the text. They are presence. Last-write-wins on a small map is the right tool for a caret, and the awareness channel is that map. Put the caret in the document and a blink replicates forever.

We snapshot the document to disk every 30 seconds. On restart we load the snapshot, then the updates that came after it. Without the snapshot, Monday morning replays every keystroke since the note was created, and the first person to open it waits.

Permissions stay on the server. The merge will accept an update from anyone who possesses the bytes. "Anyone who possesses the bytes" is not an access policy. Check the participant, then accept the update into that room's document.

The test

Two browsers. One sentence each. Airplane mode on both. Then the network comes back. If either sentence is missing, the merge is still an integer underneath. If both sentences are present, and a retry did not duplicate a word, the three laws hold for the case you actually have. Add a third browser only after that test is boring.