Real-Time Collaboration Engines: CRDTs, OT, and Local-First Architecture
Real-time collaboration has moved from a niche feature to a core expectation in modern software. From Google Docs and Figma to Notion and multiplayer whiteboards, users expect instant updates, seamless offline support, and zero conflicts. Behind these experiences lie sophisticated synchronization engines that reconcile concurrent edits across devices and networks. Two fundamental approaches dominate: Operational Transformation (OT) and Conflict-free Replicated Data Types (CRDTs). This article explores both, explains the local-first architecture that powers resilient apps, and offers practical guidance for building your own collaboration engine.
The Core Challenge: Concurrent Editing
When multiple users edit the same document simultaneously, their operations can arrive in different orders at different replicas. Without coordination, the replicas diverge. The goal is to ensure all replicas converge to the same final state, ideally without locking or central bottlenecks. The CAP theorem reminds us that under network partitions, we must trade off consistency and availability. Collaboration engines often choose eventual consistency with conflict resolution, allowing offline edits and merging them later.
Operational Transformation (OT)
Operational Transformation was pioneered by Google Docs and remains a proven approach for text editing. In OT, each edit is an operation, such as insert at position 5 or delete range 10-12. A central server sequences operations and transforms them against concurrent operations. For example, if user A inserts ‘X’ at position 5 while user B inserts ‘Y’ at position 3, the server transforms A’s operation to account for B’s insertion, shifting the position. The transformed operations are then broadcast to all clients.
How OT works:
- Operations are applied optimistically on the client.
- A central server maintains a total order of operations.
- When a client receives remote operations, it transforms its pending local operations against them.
- The transformation functions must satisfy properties like TP1 and TP2 to ensure convergence.
Pros of OT: Efficient for text, well-understood, low metadata overhead. Cons: Requires a central server, transformation functions are complex and error-prone, and it struggles with rich data types and offline-first scenarios.
Conflict-free Replicated Data Types (CRDTs)
CRDTs take a different approach. Instead of transforming operations, they design data structures where all concurrent operations commute. This means the order of application does not matter, and replicas automatically converge. CRDTs come in two flavors: state-based (convergent) and operation-based (commutative).
State-based CRDTs merge entire states using a join operation that is commutative, associative, and idempotent. Examples include G-Counter (grow-only counter) and LWW-Register (last-writer-wins register). Operation-based CRDTs broadcast operations that must be delivered exactly once, in causal order. Examples include RGA (Replicated Growable Array) for sequences and WOOT for text.
Key CRDT examples:
- G-Counter: Each replica has a counter; the total is the sum of all counters.
- PN-Counter: Supports increment and decrement using two G-Counters.
- LWW-Register: Stores a value with a timestamp; the latest timestamp wins.
- RGA and YATA: Sequence CRDTs used for collaborative text editing, as in Yjs.
- Automerge: A JSON-like CRDT library that supports rich data types and history.
Pros of CRDTs: Works offline, no central server required, handles complex data structures, and is robust to network partitions. Cons: Higher metadata overhead, potential memory growth, and more complex garbage collection.
Local-First Software
Local-first software is a philosophy where the user’s device holds the primary copy of data, and the cloud acts as a sync and backup layer. This enables instant responsiveness, offline functionality, and data ownership. CRDTs are a natural fit for local-first because they allow independent edits that merge automatically.
Principles of local-first:
- Data is stored locally and available offline.
- Changes sync automatically when connectivity returns.
- Conflicts are resolved deterministically without user intervention.
- Users control their data and can export it easily.
- Collaboration is peer-to-peer or via lightweight sync servers.
Examples of local-first apps include Linear, Figma (partially), and any app built with Automerge or Yjs. The local-first community, led by researchers like Martin Kleppmann, promotes these ideas as a counterpoint to cloud-centric models.
Architecture Patterns for Collaboration Engines
Building a collaboration engine involves several components: a data model, a sync protocol, a transport layer, and conflict resolution logic.
Centralized vs. Decentralized
OT typically uses a centralized server to order operations. CRDTs can be fully decentralized (peer-to-peer) or use a central relay for efficiency. A hybrid approach uses a server for persistence and discovery but allows direct peer connections via WebRTC.
Transport Layers
WebSockets are the most common transport for real-time sync. For peer-to-peer, WebRTC data channels offer low latency. For offline-first, IndexedDB stores local state and a service worker manages background sync.
Sync Engines
Yjs and Automerge provide CRDT implementations and sync protocols. Yjs uses a binary encoding and supports providers for WebSocket, WebRTC, and IndexedDB. Automerge offers a JSON-like API and efficient binary format. Both handle version vectors, causal ordering, and conflict resolution.
Performance and Scalability Considerations
CRDTs can grow in size as history accumulates. Techniques like tombstone garbage collection, snapshotting, and delta compression help manage memory. For text editing, Yjs uses a optimized sequence CRDT that compresses operations. OT is generally more lightweight but requires a central server that can become a bottleneck.
Scalability tips:
- Use binary encodings (e.g., Yjs updates) to reduce bandwidth.
- Implement periodic snapshots and prune old history.
- Shard documents across multiple servers if using a central relay.
- Use presence and awareness protocols separately from document sync.
Security and Access Control
Collaboration engines must handle authentication, authorization, and encryption. In a local-first model, end-to-end encryption is desirable, but it complicates server-side conflict resolution. Some systems use group encryption keys and encrypt CRDT updates. Access control can be enforced at the document level or via capability-based systems. Always validate operations on the server if one exists, and consider rate limiting and abuse prevention.
Real-World Examples
Google Docs uses OT with a central server. Figma uses a custom CRDT for multiplayer design. Notion uses a combination of OT and CRDT-like structures. Open-source projects like Yjs power many collaborative editors, including JupyterLab, and Automerge is used in local-first apps like Ink & Switch’s Upwelling. These examples show that both approaches can succeed, but the choice depends on requirements.
Choosing Between OT and CRDTs
Use OT when you need efficient text editing with a central server, and you can tolerate a centralized architecture. Use CRDTs when you need offline-first, peer-to-peer, or complex data types with automatic merging. For many modern apps, CRDTs offer a better developer experience and future-proof architecture.
Future Trends
The line between OT and CRDTs is blurring. New algorithms like Fugue and Peritext improve CRDT performance for text and rich text. AI-assisted collaboration may generate or suggest edits, requiring new conflict resolution strategies. Local-first is gaining traction as users demand privacy and ownership. Expect to see more standardized sync protocols and better tooling for building collaboration engines.
Conclusion
Real-time collaboration is a fascinating blend of distributed systems, data structures, and user experience. Whether you choose OT or CRDTs, understanding the trade-offs is essential. Local-first architecture, powered by CRDTs, offers a compelling vision for resilient, user-centric applications. By leveraging existing libraries like Yjs and Automerge, you can build collaborative features that work seamlessly online and offline. The future of software is collaborative, and the engines that power it will only get smarter.

