Algorithms · Working together

Operational transformation

How Google Docs lets two people type in the same sentence. You insert a word at position 5 while a friend deletes at position 2. Whose position 5?

The problem

Two people open the same Google Doc. The sentence says the cat sat. At the same instant, one of them adds a word in the middle and the other deletes the start. Neither waits for the other, and both screens must end up showing the same text.

Google's 2010 Drive blog series on the new Google Docs explained how it handles this. The hard part is that the edits cross paths on the network, so each person's copy changes before the other person's edit arrives.

The obvious way

Send each edit as "insert this text at position 4" or "delete 4 characters at position 0", and have everyone apply every edit exactly as sent. It breaks because a position only means something for the text it was written against. Once a delete lands first, "position 4" points at different letters. Each machine applies the same edits in a different order, and the copies diverge.

The clever idea

Imagine you tell a friend "take the fourth door on the left", and before they arrive, someone removes the first four doors. Your directions are stale, so you have to rewrite them: "now it's the first door". Docs does this with every edit. Each edit is sent as an operation, and when two operations happen at the same time, each one is rewritten to account for the other.

That rewriting is called operational transformation (OT). A central server decides the order in which operations count. Each copy then transforms the incoming operation against the ones it didn't know about, for example shifting an insert's position if text was deleted before it, so every copy ends up identical.

How the code does it

The obvious way applies A and B as written, in both orders. A's "position 4" lands in different text once B has deleted the first four characters, so you get big cat sat one way and cat big sat the other. That is divergence.

The clever way sends the second edit through transform first. Here B removed 4 characters before A's position, so this line moves A from 4 to 0. In the other order, B's delete starts before A's insert, so it doesn't need to move. Both orders end at big cat sat.

Trade-offs

Transformation needs a rule for every pair of operation types, and every rule has edge cases, like the insert-inside-a-delete case this toy skips. It also leans on a central server to order operations. If you need simple data, such as a single settings value, or no server at all, a plain "last write wins" rule or a different approach may fit better.

Where else you'll see it

Any shared editor faces the same question: what does "position 4" mean when the text under it has changed? Collaborative notes, code editors and whiteboards all have to answer it, whether they use OT or a different technique.

B deleted "the " (positions 0 to 3). Why does transform move A's insert from position 4 to 0?

  • B's delete removed 4 characters before position 4
  • Inserts always go to the start
  • A and B have different site numbers
Show the answer

B's delete removed 4 characters before position 4. Four characters before A's spot disappeared, so the same place in the text is now 4 positions earlier. Position 4 becomes 0.

Change it and see
  • Change B.len to 2, predict results
  • Set A.pos to 8, rerun
  • Make B an insert at 4

The code

javascript, with what each line means beside it. It compares the clever way with applying everyone’s edits exactly as sent.

const start = "the cat sat";Start with the text 'the cat sat'
const A = { type: "ins", pos: 4, text: "big ", site: 1 };A inserts 'big ' at position 4
const B = { type: "del", pos: 0, len: 4, site: 2 };B deletes 4 characters from position 0
function apply(s, op) {Define how to apply one edit
if (op.type === "ins") return s.slice(0, op.pos) + op.text + s.slice(op.pos);If inserting, splice the text in
return s.slice(0, op.pos) + s.slice(op.pos + op.len);Otherwise, cut the characters out
}
function transform(a, b) {Define transform: adjust a for b's effect
const t = { ...a };Copy a so we can change it
if (a.type === "ins" && b.type === "ins") {Insert versus insert:
if (b.pos < a.pos || (b.pos === a.pos && b.site < a.site)) t.pos += b.text.length;Slide right if b inserted earlier
} else if (a.type === "ins" && b.type === "del") {Insert versus delete:
if (b.pos < a.pos) t.pos -= Math.min(b.len, a.pos - b.pos);Slide left by what b removed before
} else if (a.type === "del" && b.type === "ins") {Delete versus insert:
if (b.pos <= a.pos) t.pos += b.text.length;Slide right if b inserted earlier
}
return t;Hand back the adjusted edit
}
const naiveAB = apply(apply(start, A), B);Obvious way: A, then B as sent
const naiveBA = apply(apply(start, B), A);Obvious way: B, then A as sent
const smartAB = apply(apply(start, A), transform(B, A));Clever way: A, then B adjusted
const smartBA = apply(apply(start, B), transform(A, B));Clever way: B, then A adjusted
show([Show a table of results
{ how: "obvious", order: "A then B", result: naiveAB },Row: obvious, A first
{ how: "obvious", order: "B then A", result: naiveBA },Row: obvious, B first
{ how: "transformed", order: "A then B", result: smartAB },Row: transformed, A first
{ how: "transformed", order: "B then A", result: smartBA },Row: transformed, B first
], "Do the copies agree?");

Read more

Run it. Then change it.

The code runs right in the page and draws as it goes. Free, nothing to install.

More on working together