Real-time collaborative editing is one of the most complex and rewarding problems in distributed systems. Google Docs provides a near-instantaneous collaborative writing experience for users around the world — thanks to a technique called Operational Transformation (OT).

But OT is just one of several automatic conflict resolution techniques discussed in Designing Data-Intensive Applications by Martin Kleppmann. In this blog, we’ll explain how OT works, compare it with other techniques like CRDTs and mergeable persistent data structures, and help you decide which to use for your application.


🔧 What is Operational Transformation (OT)?

OT allows concurrent changes to a shared document by multiple users while ensuring eventual convergence, intention preservation, and real-time responsiveness.

🔮 How It Works:

  1. Each user edits a local copy of the document.
  2. Operations (e.g., insert "A" at position 5) are sent to a central server.
  3. The server serializes operations and broadcasts transformed versions to other users.
  4. Each client transforms incoming operations to preserve intent with their own pending changes.

🔍 Example:

  • User A: Insert("A", pos=0)
  • User B: Insert("B", pos=0)

Without OT: One write overwrites the other.
With OT:

  • User A sees: "A"
  • B’s operation is transformed to: Insert("B", pos=1)
  • Final result: "AB" or "BA", based on order and transformation rules

📄 Components:

  • Local Buffers: Temporarily store unsynced edits
  • Transformation Engine: Transforms remote ops against local context
  • Server: Orders ops and manages state convergence

🖼️ Diagram: Operational Transformation

User A: Insert("a", pos=0)
User B: Insert("b", pos=0)

OT transforms:
User B sees → Insert("b", pos=1)

Final doc: "ba" or "ab" (depending on logic)
  • Shows how concurrent inserts are transformed to maintain intent.

🧑‍🤝‍🧑 Real-World Example: Google Docs

Google Docs uses OT to allow multiple users to type, delete, and format text simultaneously with:

  • High speed (edits visible within milliseconds)
  • Strong responsiveness (edits can be made offline and synced later)
  • Conflict resolution without user intervention

It achieves this through:

  • Intention preservation: Keeps user expectations intact
  • Consistency: All users converge to the same state
  • Causality tracking: Ensures edits are applied in meaningful order

🔁 How OT Compares with Other Conflict Resolution Strategies

In distributed data systems, OT is one of three main strategies:

1. CRDT (Conflict-free Replicated Data Types)

  • Designed for eventual consistency using mathematically-mergeable data structures (e.g., sets, counters).
  • Pros: Offline-safe, no central server needed
  • Cons: Limited to specific types of operations

🖼️ Diagram: CRDTs

Replica A:   add(milk), add(eggs)
Replica B:   add(bacon), remove(eggs)

Merge:
Final CRDT Set = {milk, bacon}
  • Shows merging two sets (milk, eggs) without conflict.

2. Mergeable Persistent Data Structures

  • Uses three-way merge based on version history.
  • Example: Git
  • Pros: History-aware, great for manual conflict resolution
  • Cons: Not suitable for real-time editing

🖼️ Diagram: Mergeable Persistent Data Structures

base
      /  \
   userA userB
     |     |
    vA    vB

Merge(base, vA, vB) → final version
  • Shows merging versions vA and vB based on common ancestor.

3. OT (Operational Transformation)

  • Transforms and reorders operations in real-time.
  • Pros: Best for live editors
  • Cons: Complex to implement, requires transformation logic

📊 Comparison Table

Feature CRDT Mergeable Structures Operational Transformation (OT)
Real-time editing ❌ Limited ❌ No ✅ Yes
Offline support ✅ Yes ✅ Yes ✅ Yes
Transformation logic needed ❌ No ❌ No ✅ Yes
Use case Shopping carts, counters Git-style version control Google Docs, Figma, Miro
Central coordination required ❌ No ✅ Optional ✅ Yes
User intention preservation ❌ Not always ✅ Manual via merge tools ✅ Yes

📚 When to Use Each

Use Case Best Fit
Real-time co-editing (text/code) Operational Transformation
Distributed sets, counters, maps CRDTs
Offline file versioning Mergeable Persistent Structures

📆 Final Thoughts

OT powers seamless collaboration in tools like Google Docs. It’s complex but irreplaceable when you need real-time multi-user editing with conflict resolution baked in.

For other types of systems (e.g., NoSQL databases, Git), CRDTs or mergeable structures may be a better fit. Each method has trade-offs in latency, conflict handling, and implementation complexity.

Choose wisely based on your app’s need for speed, interactivity, and correctness.


Inspired by Chapter 5 of "Designing Data-Intensive Applications" by Martin Kleppmann