DEV Community

Dhanush B
Dhanush B

Posted on

How Google Docs Uses Operational Transformation for Real-Time Collaboration

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)
Enter fullscreen mode Exit fullscreen mode
  • 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}
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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

Top comments (0)