πΉ What is Operational Transformation (OT)?
Operational Transformation (OT) is an algorithm that enables real-time collaboration by allowing multiple users to edit the same document simultaneously while ensuring consistency (everyone sees the same final state).
π Problem: If two people type at the same time, edits may conflict. OT ensures the document converges to the same state on all clients, regardless of the order of operations.
πΉ The Core Idea
- Every edit (insert, delete, replace) is represented as an operation.
- When two operations happen concurrently, OT transforms one operation against the other so both can be applied without conflict.
- This ensures intention preservation β each userβs action has the effect they intended, even if others edit in parallel.
πΉ Example
Document = "hello"
π© User A: inserts "X" at position 0 β Insert(0, "X")
π¨ User B: inserts "Y" at position 5 β Insert(5, "Y")
If processed in order:
- Apply A:
"Xhello" - Apply B:
"XhelloY"β
But what if applied in opposite order?
- Apply B first:
"helloY" - Now Aβs operation (
Insert(0, "X")) still works:"XhelloY"β
No problem here.
Conflict Example
Document = "hello"
π© User A: deletes "h" at position 0 β Delete(0)
π¨ User B: inserts "X" at position 1 β Insert(1, "X")
If processed in order:
- Apply A:
"ello" - Apply B (Insert at 1):
"eXllo"
If processed in reverse:
- Apply B:
"hXello" - Apply A (Delete at 0) β
"Xello"
β οΈ Results differ ("eXllo" vs "Xello")
πΉ How OT Fixes This
OT defines transformation rules so operations are adjusted before applying:
When Bβs Insert(1, "X") sees that A deleted "h" at 0,
- The insertion point shifts left β becomes
Insert(0, "X").
Now both orders lead to "Xello". β
πΉ Properties OT Must Ensure
- Convergence β All users end with the same document.
- Causality preservation β Operations respect their logical order.
- Intention preservation β Each userβs edits have the effect they intended.
πΉ Where OT is Used
- Google Docs (classic implementation).
- Etherpad, ShareJS.
- Real-time collaborative editors.
πΉ OT vs CRDT (Conflict-free Replicated Data Types)
- OT β transform operations on-the-fly, requires central server for coordination.
- CRDT β mathematical structures designed for distributed systems, allow eventual consistency without central server.
- Modern apps (like Figma) often prefer CRDTs over OT for decentralized sync.
β In summary: OT = algorithm that transforms concurrent operations so all users end up with the same final state, preserving intention.