Real-Time Collaboration in the Browser: CRDTs vs OT in Practice

Team 8 min read

#webdev

#real-time

#crdts

#ot

Introduction

Real-time collaboration in the browser has moved from a cool demo to a baseline expectation for many apps. Users expect edits to propagate almost instantly, even when they’re offline and then reconnect. The two dominant architectural families to achieve this are CRDTs (Conflict-free Replicated Data Types) and OT (Operational Transformation). Each approach has its own guarantees, trade-offs, and engineering challenges.

In practice, the choice between CRDTs and OT often hinges on offline support, data model, server requirements, and the complexity you’re willing to manage. This post digs into how CRDTs and OT behave in real-time scenarios, what you gain or lose with each, and practical tips to help you decide which path to take.

What are CRDTs and OT?

  • CRDTs (Conflict-free Replicated Data Types): A design that lets multiple clients modify a data structure concurrently and guarantees convergence to a single consistent state without requiring a central server to order operations. CRDTs come in state-based and operation-based flavors and often support offline edits that later merge cleanly.

  • OT (Operational Transformation): A theory and family of algorithms that transform operations so they can be applied in different orders but converge to the same document state. OT typically relies on a central authority or a deterministic order to apply transformed operations, making real-time collaboration with strong ordering guarantees common in OT-based systems.

Key differences at a glance:

  • Convergence: CRDTs aim for convergence automatically; OT relies on transformative rules to achieve the same result.
  • Offline support: CRDTs generally excel at offline use cases; OT has offline strategies but often requires more infrastructure to handle latency and reordering.
  • Complexity: CRDTs can be simpler to reason about at the client level but can suffer from state growth and tombstone management; OT-centric systems trade some simplicity for robust, order-preserving collaboration.
  • Debuggability: OT history and intent can be easier to reason about for some kinds of text edits; CRDTs can be harder to inspect when conflicts emerge from complex data structures.

Real-time scenarios and conflict resolution

  • Concurrent edits on a single character: OT typically transforms edits based on a shared history; CRDTs merge changes as independent operations. In practice, both can feel immediate, but CRDTs avoid the need for complex reordering rules in many cases.

  • Offline edits and reconnect: CRDTs shine here. Changes made while offline are merged deterministically when reconnected. OT can support offline edits but usually requires careful versioning and reconciliation logic.

  • Rich data models (structured documents, panels, widgets): CRDTs can handle composites (maps, lists, nested structures) with libraries designed for these shapes. OT has strong history in text editing but can be extended to richer documents with more manual work.

  • Recovery and debugging: OT’s deterministic operation history can be easier to audit in certain apps. CRDTs often require tombstone management and garbage collection strategies for long-lived documents.

CRDTs in practice: benefits and pitfalls

Benefits

  • Robust offline support and eventual consistency without a central lockstep server.
  • Simple conflict resolution at the data-type level; edits propagate and merge deterministically.
  • Scales well with distributed clients, given a suitable data model and library.

Pitfalls

  • State growth: many CRDTs accumulate tombstones or metadata that can bloat documents or histories over time.
  • Garbage collection and schema evolution: removing or changing fields in a CRDT can be tricky; require explicit GC strategies.
  • Debugging complexity: understanding how a complex composition of CRDTs converges can be non-trivial.
  • Integration work: integrating CRDT libraries with your existing data layer and UI can require careful bridging code.

Practical notes

  • Choose a mature CRDT library that matches your data model (e.g., Yjs for complex collaborative documents, Automerge for JSON-like structures).
  • Plan for garbage collection early: tombstones, version pruning, and careful storage of historical metadata.
  • Consider awareness data: many CRDTs provide “awareness” channels for presence or cursors without affecting the document convergence.

OT in practice: benefits and pitfalls

Benefits

  • Strong, proven approach for real-time text editing with deterministic history.
  • Clear, well-understood model for transforming operations to preserve intent and ordering.
  • Often easier to reason about when your primary data is plain text and you require strict consistency across editors.

Pitfalls

  • Offline editing and re-ordering complexities: OT typically depends on a consistent operation order; offline work requires careful reconciliation.
  • Algorithmic complexity: implementing OT correctly for all edge cases (linting, cursors, selections) is intricate.
  • Infrastructure demands: many OT-based systems rely on a central server to serialize operations, which can become a bottleneck if not scaled properly.

Practical notes

  • Consider OT for text-centric apps or when you need robust, deterministic ordering across clients.
  • Use established OT libraries (e.g., ShareDB or OT.js) and evaluate their performance and beta features in your target environment.
  • Build with a clear versioning and reconciliation strategy to handle reconnections, undo/redo, and concurrent edits.

Comparisons by dimension

  • Latency and responsiveness:

    • CRDTs: typically fast, as changes can propagate without centralized sequencing; good for offline-first apps.
    • OT: can be fast but often relies on a server to serialize operations; latency depends on the network path to the server.
  • Consistency guarantees:

    • CRDTs: eventual convergence guaranteed by design, with state-based or operation-based variants.
    • OT: strong consistency through transform rules and a canonical operation order.
  • Availability and partition tolerance:

    • CRDTs: high availability in offline or partitioned networks; merges when connectivity returns.
    • OT: availability depends on server architecture; requires handling partial failures and reconciling state.
  • Data model fit:

    • CRDTs: flexible for structured documents and complex data types, beyond plain text.
    • OT: excels in text-centric collaboration and domains where ordering of edits is paramount.
  • Debuggability and tooling:

    • CRDTs: tooling improves, but debugging can be opaque due to distributed state and tombstone semantics.
    • OT: historical operation sequences can be easier to audit, especially for text edits.

Case studies and libraries (high-level)

  • CRDT-friendly stacks:

    • Yjs: a popular CRDT library for real-time collaborative editing, supporting rich data types and awareness channels.
    • Automerge: a CRDT library focused on JSON-like documents with a friendly API and offline merge semantics.
  • OT-friendly stacks:

    • ShareDB: a real-time synchronization engine built on OT for JSON documents, with a central server path and reconciling edits.
    • OT.js or similar implementations: research and production-grade OT strategies for various data shapes.

Note: When evaluating libraries, look for:

  • Maturity and maintenance activity
  • Documentation and example patterns for your data model
  • Memory and performance characteristics under realistic workloads
  • Interoperability with your backend, auth, and persistence layers

Implementation tips and guardrails

  • Start with a small, well-scoped prototype: replicate a simple document editing feature to compare latency, UX, and complexity.
  • Define your data model early: CRDTs shine with structured data types; OT often maps cleanly to text edits but can be extended with care.
  • Plan offline behavior and reconnection flows: ensure your choice supports smooth reconciliation when users go offline and come back online.
  • Establish a clear conflict resolution strategy: even with CRDTs, you’ll need to handle non-conflicting but logically related changes and UI updates.
  • Monitor and instrument: collect metrics on convergence time, message volume, and merge paths to identify bottlenecks.
  • Budget for governance: decide how to handle schema evolution, data retention, and garbage collection for long-lived documents.
  • Consider a hybrid approach: some apps use CRDTs for the core document while keeping OT-like transformable layers for specific features that require strict ordering.

When to choose CRDTs vs OT

  • Choose CRDTs if:

    • Offline support and high availability across distributed clients are priorities.
    • You’re dealing with complex, structured data beyond plain text.
    • You want simpler, local conflict resolution without a heavy central ordering dependency.
  • Choose OT if:

    • Your primary use case is real-time text editing with strong, deterministic history.
    • You have an established server path that can serialize and transform operations predictably.
    • You need mature tooling and patterns for undo/redo and presence with a focus on text.
  • Consider a hybrid approach if:

    • Your app contains both rich structured data and text editing components.
    • You want offline resilience for some parts of the document while preserving strict ordering for others.
    • You already have infrastructure for OT and want to gradually introduce CRDT-based components.

Conclusion

Real-time collaboration in the browser is a multi-faceted challenge. CRDTs offer robust offline capabilities and automatic convergence for complex data, at the cost of managing growth and debugging complexity. OT provides deterministic ordering and strong text editing semantics but can demand more centralized infrastructure and careful reconciliation logic for offline scenarios.

Your best path often comes from aligning the technology with your core data model, offline requirements, and operational realities. Start with a concrete use case, prototype both approaches, measure latency and convergence, and choose the approach that best fits your app’s goals and team capabilities.