How collaborative editors like Google Docs merge edits made at the same time.
Operational Transformation (OT) is the technique that lets several people edit the same document at the same time and still end up with the same text. It works by rewriting each edit so it still makes sense after other edits have already been applied.
Two people open the word "cake". Alice inserts an X at the start. At the same moment Bob inserts a Y at the end. Each person sees their own edit instantly. Then the edits cross over the network.
If Alice simply applies Bob's operation "insert Y at position 4" to her text "Xcake", Y lands in the wrong place (before the final "e"), because her text got one letter longer. Bob has the same problem in the other direction, so the two documents diverge.
OT fixes this with a transform function. Before applying a remote operation, it adjusts the operation against the local ones that happened concurrently. Alice turns "insert Y at 4" into "insert Y at 5", so both end up with "XcakeY".
| Incoming op | Concurrent op already applied | What the transform does |
|---|---|---|
| Insert at p | Insert at q, with q before p | Shift p right by one |
| Insert at p | Insert at the same spot | Use a tie-break rule (such as user ID) so everyone picks the same order |
| Insert at p | Delete at q, with q before p | Shift p left by one |
| Delete at p | Insert at q, with q at or before p | Shift p right by one |
| Delete at p | Delete at the same spot | Do nothing: that character is already gone |
| Delete at p | Delete at q, with q before p | Shift p left by one |
| Design | How it orders edits | Good and bad |
|---|---|---|
| Client-server OT | A central server decides one official order and transforms against it | Simple and widely used (Google Docs style). The server is needed, but the client logic is small. |
| Peer-to-peer OT | No central server, so every pair of peers must transform against each other | Very hard to get right. Correctness proofs are difficult, so this is rare in practice. |
A tiny model of one pair of edits on โcakeโ. Positions count from 0. When both people insert at the same spot, the lower site number (Alice, 1) goes first, so both sides pick the same order. Real OT systems also use a server to order many edits.
| Term | Meaning in easy words |
|---|---|
| Operation | A small change described as data, such as insert โaโ at position 5 |
| Transform function | Rewrites one operation so it applies correctly after another concurrent one |
| Concurrent operations | Two operations made without either person having seen the other |
| Convergence | All copies end up with the same document |
| Intention preservation | Each user's edit keeps its meaning after transformation |
| Causality preservation | If one edit was made after seeing another, it is applied after it everywhere |
| Revision / version | The number of operations applied so far, used to say what an op was based on |
| Optimistic update | Showing your own edit at once, before the server confirms it |
| Tie-break | A fixed rule (for example by user ID) to order two inserts at the same spot |
| Operation history | The server's ordered log of all accepted operations |
| Transformation property (TP1) | A then transformed B equals B then transformed A |
| Choice | We gain | We lose |
|---|---|---|
| OT with a central server | Simple order, small documents, small messages | The server is a dependency, and it is hard offline |
| CRDT (for example Yjs) | Peer-to-peer and offline work, simpler correctness | More metadata per character, and heavier memory |
| Locking sections | Very simple to build | People wait for each other, which kills the live feel |
| Last-writer-wins on the whole file | Trivial | Concurrent edits are lost |
| More operation types | Richer editing (formatting, tables) | The number of transform pairs grows quickly |
| Sending one op at a time | Easy server logic | Slower on very slow networks |