Operational Transformation (OT) and CRDTs, which are key techniques for real-time collaborative systems.
⚙️ What Are They?
Both OT and CRDTs are methods to handle concurrent edits in distributed collaborative applications (like Google Docs, collaborative coding, or Figma), ensuring eventual consistency.
1️⃣ Operational Transformation (OT)
Definition:
OT transforms operations from multiple users so that all replicas converge to the same state, even if edits happen concurrently.
How it Works:
- Each user’s operation is sent to the server.
- If two operations conflict (e.g., inserting text at the same position), the server transforms one operation relative to the other to preserve intent.
- Transformed operations are broadcast to other clients.
- All clients apply operations in the same order, achieving consistency.
Example:
- Document:
Hello - User A inserts
Xat position 1 →HXello - User B inserts
Yat position 2 →HeYllo - OT transforms B’s operation relative to A → final consistent document:
HXYello
Pros:
- Preserves user intent in collaborative edits
- Used in Google Docs, Microsoft Office Online
Cons:
- Complex to implement
- Requires central server or authority to transform operations
2️⃣ CRDTs (Conflict-free Replicated Data Types)
Definition:
CRDTs are data structures designed for concurrent updates that can merge automatically without conflicts, guaranteeing eventual consistency.
How it Works:
- Each client can update its local replica independently.
- Updates are propagated asynchronously to other replicas.
- A merge function ensures that all replicas converge without coordination.
Types of CRDTs:
| Type | Example | Use Case |
|---|---|---|
| Grow-Only Counter | Increment-only counter | Likes, votes |
| PN-Counter | Positive/Negative counter | Accounting |
| G-Set | Add-only set | Collaborative lists |
| OR-Set | Add/remove set | Shared task lists |
| RGA / Sequence CRDT | Ordered list of elements | Collaborative text editing |
Pros:
- No need for central server
- Naturally fault-tolerant
- Scales well in distributed environments
Cons:
- More complex data structures
- Sometimes more memory/storage overhead
🔄 OT vs CRDTs Comparison
| Feature | OT | CRDT |
|---|---|---|
| Architecture | Usually client-server | Can be peer-to-peer |
| Conflict Handling | Transform operations | Merge replicas deterministically |
| Central Authority | Required | Optional |
| Complexity | Medium-High | High (complex data types) |
| Use Case | Real-time collaborative editing | Collaborative apps, distributed databases |
🧩 Analogy
- OT: “Adjust each person’s edits on the fly so everyone’s view matches” → like editing the same whiteboard with a mediator
- CRDT: “Every edit leaves a mark that can merge safely later” → like Lego blocks you can combine in any order
✅ Key Takeaways
- Both OT and CRDTs handle concurrent edits and ensure eventual consistency.
- OT: Centralized, transforms operations, preserves user intent.
- CRDTs: Decentralized, merges automatically, no coordination needed.
- Use OT for real-time text editors like Google Docs.
- Use CRDTs for decentralized apps, offline-first apps, or distributed databases.