SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
  • Consensus Algorithms
  • Paxos
  • Raft
  • Leader Election
  • ZooKeeper and etcd
  • Distributed Locks
  • Fencing Tokens
  • Gossip Protocol
  • CRDTs
  • Operational Transformation
  • Distributed Unique ID Generation (Snowflake, UUID)
  • Two Generals Problem
  • Byzantine Fault Tolerance (BFT)
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Distributed Systems/Coordination and Consensus

Operational Transformation

How collaborative editors like Google Docs merge edits made at the same time.

โฑ 13 min readAdvancedHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

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.

  • An edit is sent as an operation, like "insert X at position 3", not as the whole document.
  • When two edits happen at once, the positions in one of them are out of date. OT transforms it (shifts the position) to fix that.
  • It powers Google Docs-style editors, and usually uses a central server to decide the order of edits.

02Concept: What is it?

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".

Transforming a crossing edit
๐Ÿ“„cakeboth start here
โ†’โ†“Alice: insert X at 0
๐Ÿ“„Xcake
โ†’โ†“Bob's โ€œinsert Y at 4โ€ becomes โ€œat 5โ€
โœ…XcakeYBob has the same
The same edit means a different position once other text has changed.
๐Ÿง Keep the intent
The goal is not only that both copies match. Each person's intent must survive: Bob wanted Y after the last letter, so Y must still be there after the last letter.

03Real-Life Example

๐ŸตTwo Editors Marking the Same Printed Page
Two editors each get a copy of a printed page. The first writes, "add a word after line 1". The second writes, "delete line 3". If they just swap notes, the line numbers are wrong for the person who has already added a line: line 3 is now line 4. A careful assistant sits between them and rewrites each note: "delete line 3" becomes "delete line 4" for the first editor. Both pages then end up identical, and each editor got what they meant.
Printed page
Operational Transformation
A note like โ€œdelete line 3โ€
An operation (insert or delete at a position)
The page changed since the note was written
Concurrent operations
The assistant who rewrites the note
The transform function
โ€œLine 3โ€ becoming โ€œline 4โ€
Shifting a position
Both pages end identical
Convergence
Both editors still get what they meant
Intention preservation
A head editor who sets the order of notes
The central server
Two people, one server
๐Ÿ‘ฉAlicelocal edits
โ†”ops
๐Ÿ—„๏ธServerorders and transforms
โ†”ops
๐Ÿ‘จBoblocal edits

04Why Do We Need It?

โŒจ๏ธInstant typing
Your own edits show at once, with no wait for the server. Others' edits are merged in as they arrive.
๐ŸคMany editors, one result
Everyone converges on the same document, however the edits cross.
๐ŸŽฏIntent is kept
A user's edit stays where they meant it to be, even when text moves around it.
๐Ÿ“‰Tiny messages
Sending an operation is far smaller than sending the whole document each time.
๐ŸงฐNo locking
People do not have to take turns or lock a paragraph. Everybody types freely.
๐Ÿ”Proven in practice
Used for over a decade in large products such as Google Docs.
When many people edit the same thing live, do not lock it. Let them all edit, and fix the positions of their edits as they cross.

05Where Do We Use It?

๐Ÿ“„Online documents
Google Docs and similar word processors.
๐Ÿ’ปCode editors
Live-share and pair-programming tools where two people type in the same file.
๐Ÿ“ŠSpreadsheets and slides
Operations such as insert row, delete column and change cell, transformed against each other.
๐Ÿ“Note-taking apps
Shared notes, wikis and whiteboard text.
๐Ÿ’ฌRich text and chat boxes
Anywhere a shared text box needs live merging.
๐ŸŽ“Classrooms and interviews
Shared coding pads where interviewer and candidate type together.

06Types / Parts / How It Works

The pieces of OT

What happens when an edit arrives
โœ‰๏ธIncoming opmade on an older version
โ†’โ†“compare with
๐Ÿ•’Concurrent local opsapplied since then
โ†’โ†“transform()
๐Ÿ”งAdjusted opvalid for the current text
โ†’โ†“apply
๐Ÿ“„Document
Compare the incoming operation with the operations done in the meantime, and adjust it.

How operations are transformed

Incoming opConcurrent op already appliedWhat the transform does
Insert at pInsert at q, with q before pShift p right by one
Insert at pInsert at the same spotUse a tie-break rule (such as user ID) so everyone picks the same order
Insert at pDelete at q, with q before pShift p left by one
Delete at pInsert at q, with q at or before pShift p right by one
Delete at pDelete at the same spotDo nothing: that character is already gone
Delete at pDelete at q, with q before pShift p left by one

Two main designs

DesignHow it orders editsGood and bad
Client-server OTA central server decides one official order and transforms against itSimple and widely used (Google Docs style). The server is needed, but the client logic is small.
Peer-to-peer OTNo central server, so every pair of peers must transform against each otherVery hard to get right. Correctness proofs are difficult, so this is rare in practice.
VisualizerTwo people edit โ€œcakeโ€ at the same timeFirst leave it on โ€œraw editโ€ and make both people insert at nearby spots: the copies diverge. Then switch to โ€œTransform first (OT)โ€. Try an insert against a delete, and both deleting the same letter.
What do we send to the other person?
Alice (X) does
Bob (Y) does
Both start with
cake
Alice's screen
1. She does: insert โ€œXโ€ at 0
Xcake
2. Bob's edit arrives. She applies: insert โ€œYโ€ at 4
XcakYe
Bob's screen
1. He does: insert โ€œYโ€ at 4
cakeY
2. Alice's edit arrives. He applies: insert โ€œXโ€ at 0
XcakeY
Alice sees
XcakYe
Bob sees
XcakeY
Same text?
No
Edits rewritten
0
by the transform
Diverged! Alice ends with โ€œXcakYeโ€ and Bob with โ€œXcakeYโ€. Each side applied the other person's edit at its old position, but the text had already moved. Switch to โ€œTransform first (OT)โ€.

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.

07How to Use It / Steps / Implementation

How a simple client-server OT system works

  1. 1
    Number the document versions
    The server keeps one history of operations. Each client tells the server which version its operation was based on.
  2. 2
    Apply your own edit at once
    The client shows the edit immediately (optimistic) and sends the operation to the server.
  3. 3
    Send one at a time
    A client keeps only one operation in flight and buffers the rest. This keeps the transform logic simple.
  4. 4
    Server transforms and orders
    The server transforms the incoming operation against any operations it accepted since that version, adds it to the history, and broadcasts it.
  5. 5
    Acknowledge the sender
    The sender gets an ack for its own op and then sends the next buffered one.
  6. 6
    Clients transform what they receive
    A client transforms the server's operation against its own pending operations before applying it.
  7. 7
    Test the transform
    Check that applying A then Bโ€ฒ gives the same text as B then Aโ€ฒ for every pair of operation types.
๐Ÿ’กThe core property
For any two concurrent operations A and B, OT must make A followed by transformed B equal B followed by transformed A. If this fails for even one case, the documents can diverge.
โš ๏ธDo not build it from scratch
OT is famous for subtle bugs. Use a proven library or service, such as ShareDB, or consider a CRDT library like Yjs or Automerge.

08Key Terms to Know

TermMeaning in easy words
OperationA small change described as data, such as insert โ€œaโ€ at position 5
Transform functionRewrites one operation so it applies correctly after another concurrent one
Concurrent operationsTwo operations made without either person having seen the other
ConvergenceAll copies end up with the same document
Intention preservationEach user's edit keeps its meaning after transformation
Causality preservationIf one edit was made after seeing another, it is applied after it everywhere
Revision / versionThe number of operations applied so far, used to say what an op was based on
Optimistic updateShowing your own edit at once, before the server confirms it
Tie-breakA fixed rule (for example by user ID) to order two inserts at the same spot
Operation historyThe server's ordered log of all accepted operations
Transformation property (TP1)A then transformed B equals B then transformed A

09Pros, Cons and Trade-offs

โœ… Pros
  • Instant local editing
  • Small messages (only the operations)
  • Keeps user intent
  • Proven at huge scale
  • Document stays compact, with little extra metadata
โš ๏ธ Cons
  • Hard to design and prove correct
  • Usually needs a central server for ordering
  • Transform rules grow with each operation type
  • Poor fit for fully offline peer-to-peer use
  • Bugs show up only in rare edit orders

Trade-offs

ChoiceWe gainWe lose
OT with a central serverSimple order, small documents, small messagesThe server is a dependency, and it is hard offline
CRDT (for example Yjs)Peer-to-peer and offline work, simpler correctnessMore metadata per character, and heavier memory
Locking sectionsVery simple to buildPeople wait for each other, which kills the live feel
Last-writer-wins on the whole fileTrivialConcurrent edits are lost
More operation typesRicher editing (formatting, tables)The number of transform pairs grows quickly
Sending one op at a timeEasy server logicSlower on very slow networks

10Common Problems and Mistakes

Common mistakes

  1. Forgetting a case in the transform. Insert vs insert, insert vs delete, delete vs delete: every pair needs a rule.
  2. No tie-break for the same position. Two inserts at one spot then land in different orders on each screen.
  3. Deleting twice. If both users delete the same letter, the second delete must turn into a no-op, or an extra letter vanishes.
  4. Ignoring the revision number. Without it you cannot know what an operation was based on.
  5. Flooding the server with in-flight ops. Sending many unacknowledged operations makes the transform logic far harder.
  6. Writing your own peer-to-peer OT. Many published algorithms were later shown to be wrong in rare cases.

Things to watch

๐ŸŒ—Divergence
One missed transform case and the two screens differ. Property-test every pair of operations.
๐Ÿ”๏ธLong history
The server log grows. Take snapshots and trim old operations.
๐Ÿ“ถReconnects
A client that was offline must catch up and transform its buffered edits.
๐ŸงฎRich text
Formatting and nested structures need many more operation types.

11Real-World Examples

๐Ÿ“„Google Docs
Built on OT with a central server, and it grew from the Jupiter and Google Wave work.
๐ŸŒŠGoogle Wave
An early large OT system. Much of today's practical OT knowledge came from it.
๐ŸงฉShareDB / ShareJS
Open-source OT libraries and servers for building your own collaborative apps.
๐Ÿ“Etherpad
An open-source real-time text editor built around OT.
๐ŸชŸMicrosoft Office online
Co-authoring in web Office apps uses operation-based merging of edits.
๐Ÿ”€A move to CRDTs
Some newer editors and local-first apps choose CRDTs (Yjs, Automerge) to work offline and peer-to-peer.
๐Ÿ“Note
Product internals change. Treat these as well-known examples of the idea and check the project's own papers or docs for the exact current design.
Mini case: Alice and Bob edit โ€œcakeโ€ at once
๐Ÿ‘ฉAliceinsert X at 0
โ†’โ†“first
๐Ÿ—„๏ธServerBob's โ€œat 4โ€ becomes โ€œat 5โ€
โ†’โ†“then
๐Ÿ‘จBobinsert Y at 4
First edit Transform Second edit
The server orders Alice first, then transforms Bob's edit against hers.

12Related Topics to Read Next

CRDTsโ†’

The main alternative for merging concurrent edits, good for offline and peer-to-peer.

WebSocketsโ†’

A live connection is how edits flow between the editors and the server.

Logical Clocksโ†’

Version numbers and ordering ideas help to tell which edits are concurrent.

Consistency Modelsโ†’

OT aims for eventual convergence of all copies.

Event Sourcingโ†’

A log of operations that is replayed is close to how OT keeps history.

Lamport Timestampsโ†’

A simple way to give operations an order across machines.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • โœ”What: live co-editing by sending operations and transforming them against concurrent ones.
  • โœ”Problem: positions go stale when another edit lands first.
  • โœ”Fix: transform(op, other): shift the position, or turn the op into a no-op.
  • โœ”Rule: A then Bโ€ฒ must equal B then Aโ€ฒ. Use a tie-break for same-spot inserts.
  • โœ”Design: client-server OT: the server sets the one order. Peer-to-peer OT is very hard.
  • โœ”Flow: apply locally, send one op at a time, server transforms and broadcasts.
  • โœ”Versus CRDT: OT is compact but needs a server. CRDT is peer-to-peer but heavier.
  • โœ”One line: โ€œRewrite each edit so it still means the same thing after the text has changed.โ€
โ† Previous
CRDTs
Next โ†’
Distributed Unique ID Generation (Snowflake, UUID)