Understanding real-time collaboration with CRDTs

Shambhavi Shandilya
6 min readJul 21, 2024

--

Imagine you and your friends are working on a project together in Google Docs. Even if someone edits while offline, or the internet cuts out for a bit, everything seems to magically come together into one document in the end, without any manual resolution of merge conflicts like you might see in other version control tools. This teamwork superpower comes from a cool tech trick called CRDTs, which stands for Conflict-Free Replicated Data Types. Let’s jump in and see how CRDTs make real-time collaboration in Google Docs and other apps so smooth!

CRDTs are a special kind of data structure designed for distributed systems. They allow for concurrent modifications on different replicas (copies) of the data, eventually converging to a consistent state without the need for complex coordination. This translates to smooth user experiences in applications like collaborative editing tools or offline-first mobile apps.

Types of CRDTs

There are two main types of CRDTs: state-based and operation-based.

  • State-based CRDTs: These store the entire data state at each replica. Updates involve sending the complete updated state to other replicas for merging.
  • Operation-based CRDTs: Here, only the operations performed on the data are replicated. Each replica independently applies these operations to its local state, eventually leading to consistency.

State-based CRDTs: Keeping the Big Picture Up-to-Date

Imagine a group project where everyone has a full copy of the presentation. When someone makes a change, they email the updated presentation to everyone else. It might arrive in a different order for some people, but everyone can eventually merge their copies and get on the same page.

State-based CRDTs transmit their full state between peers, and a new state is obtained by merging all the states together. These CRDTs prioritize strong consistency, meaning all replicas eventually converge to the same exact state.

Update transmission in State-Based CRDT

To achieve this, they rely on the following mathematical assumptions:

  • Commutativity: The order in which updates are applied doesn’t matter. This means updates from different users can be applied in any order, and the final state will always be the same. Formally, for any updates U1 and U2, U1(U2(X)) = U2(U1(X)) for any state X.
  • Idempotence: Applying the same update multiple times has the same effect as applying it just once. This ensures that even if an update gets sent multiple times due to network issues, the final state remains consistent. Formally, for any update U and state X, U(U(X)) = U(X).

These properties, along with the assumption of reliable delivery (updates eventually reach all replicas), allow state-based CRDTs to offer strong consistency guarantees.

Operation-based CRDTs: Keeping it Light with Just the Edits

Imagine a group project where everyone has a copy of the presentation, but instead of sending the whole updated version, you just send a message saying “add this slide” or “change this text.” Everyone applies these changes to their own copy, and eventually, everyone’s presentation ends up looking the same, even if they applied the changes in a slightly different order.

Operation-based CRDTs transmit only the actions that users take, which can be used to calculate a new state. The drawback is that operation-based CRDTs impose constraints on the communication channel: messages must be delivered exactly once, in causal order, to each peer.

Update transmission in Operation-Based CRDT
  • Commutativity: Similar to state-based CRDTs, some operation-based CRDTs also benefit from commutativity. They can converge if these updates are commutative — no matter which order these updates are applied at a replica the resulting state will be the same.
Diagram explaining the commutative nature of the update operations

Tradeoffs Explained

State-based CRDTs offer strong consistency guarantees but can be less efficient due to the larger data size being transferred during updates. Operation-based CRDTs are more lightweight and scalable, but achieving eventual consistency might take a bit longer.

The choice between these two depends on your specific needs. If strong consistency is paramount, state-based CRDTs might be the way to go. But for applications prioritizing scalability and offline functionality, operation-based CRDTs shine.

In case of conflicts, some other resolution measures are used to ensure eventual consistency. For example, LLW (Last Write Wins) or updates sent by a prioritized (higher user id) gets written.

CRDTs in Action: Real-world Examples

Now, let’s see CRDTs in action! These powerful tools are behind the seamless collaboration in applications like:

  • Google Docs: Multiple users can edit a document simultaneously without worrying about conflicts. CRDTs ensure all changes eventually get reflected consistently.
  • Airtable: This collaborative database platform utilizes CRDTs for conflict-free data updates across various devices.

The initial motivation for the CRDT concept was actually collaborative editing. The concept outgrew collaborative editing in recent years and found its way into distributed databases, like Redis to achieve an Active-Active Geo-Distributed architecture.

Diving Deeper: Collaborative Text Editor with CRDTs

Imagine a real-time collaborative text editor where multiple users can edit the same document simultaneously. Here’s how CRDTs can power this magic:

  1. Local Edits: When you make a change (insertion, deletion, formatting) in your document, the corresponding operation is first applied locally to your copy. This ensures immediate visual feedback for you.
  2. Server Broadcast: The server then broadcasts the operation to all other collaborators’ devices.
  3. CRDT Merging: At each collaborator’s device, CRDTs come into play. These CRDTs are designed to merge incoming operations from different users in a conflict-free manner, ultimately leading to a consistent document state.

Conflict Resolution with CRDTs

While operation-based CRDTs generally handle conflicts seamlessly, The editors might employ specific strategies depending on the edit type:

  • Insert Operations: If multiple users attempt to insert characters at the same position, CRDTs might prioritize the user with a stronger internet connection or the one who made the edit first. This ensures there are no overlapping insertions.
  • Delete Operations: In case of conflicting deletions, CRDTs might prioritize keeping the most recent insertion or the one with a higher timestamp to avoid unintended data loss.

CRDTs offer a compelling alternative to traditional approaches, enabling real-time collaboration, efficient data management, and eventual consistency even in the face of network delays and offline scenarios. As the world of distributed systems continues to evolve, CRDTs are poised to play an increasingly crucial role in ensuring data integrity and seamless user experiences.

Resources:

--

--