Skip to content

What are CRDTs

Key idea:

CRDT (Conflict-free Replicated Data Types) — a class of data structures that can be updated independently on multiple replicas and merged deterministically without conflict resolution. Foundation of collaborative editors (Notion, Figma, Linear). Types: G-Counter (grow-only), LWW-Set (last-write-wins), RGA (replicated growable array for text). Libraries: Yjs, Automerge.

Below: details, example, related terms, FAQ.

Check your site →

Details

  • Commutative: A+B = B+A (operation order irrelevant)
  • Associative: (A+B)+C = A+(B+C)
  • Idempotent: applying the same update twice = same result
  • Vector clocks / Lamport timestamps for ordering
  • Use cases: collaborative editing, offline-first, distributed counters

Example

// Yjs collaborative text
const ydoc = new Y.Doc();
const ytext = ydoc.getText('content');
ytext.insert(0, 'Hello');
// Syncs over WebSocket — other users see update

Related Terms

How CRDTs Work: Mechanisms and Algorithms

CRDTs operate on the principle of enabling distributed systems to achieve eventual consistency without the need for centralized coordination. This is accomplished through a set of algorithms that define how operations are applied to data replicas. The fundamental mechanisms include:

  • Commutativity: Operations can be applied in any order, and the final state will remain consistent across all replicas.
  • Idempotence: Applying the same operation multiple times does not change the result beyond the initial application.
  • Associativity: The grouping of operations does not affect the final state, allowing flexibility in execution.

For example, in a G-Counter CRDT, each replica maintains its own counter. When a replica increments its counter, it sends the operation to other replicas, which then apply the same operation to their own counters. The merging process involves taking the maximum value from all replicas to ensure consistency.

In the case of an LWW-Set, when an element is added or removed, it is tagged with a timestamp. The system resolves conflicts by keeping the element with the most recent timestamp. This ensures that all replicas eventually converge to the same state without manual conflict resolution.

Practical Examples of Implementing CRDTs

Implementing CRDTs can be straightforward, especially with libraries that abstract some of the complexities. Below are practical examples of how to use CRDTs in a collaborative application using Yjs, a popular CRDT library.

To create a simple collaborative text editor, you can initialize a Yjs document and set up a WebSocket connection for real-time updates:

import * as Y from 'yjs';
import { WebsocketProvider } from 'y-websocket';

// Create a Yjs document
const ydoc = new Y.Doc();

// Connect to a WebSocket provider
const provider = new WebsocketProvider('ws://localhost:1234', 'my-room', ydoc);

// Create a text type
const ytext = ydoc.getText('text');

// Listen for changes
ytext.observe(event => {
  console.log('Text changed:', ytext.toString());
});

In this example, changes made to the text are automatically synchronized across all users connected to the same room. Each user's edits are tracked by the Yjs library, which ensures that all replicas eventually converge to the same state.

For a G-Counter implementation, you can define it as follows:

const gCounter = new Y.Map();

// Increment the counter
const incrementCounter = (userId) => {
  const currentValue = gCounter.get(userId) || 0;
  gCounter.set(userId, currentValue + 1);
};

// Observe changes
gCounter.observe(event => {
  console.log('Counter updated:', gCounter.toJSON());
});

This simple setup allows concurrent increments to be made by multiple users without conflict, demonstrating the power of CRDTs in collaborative applications.

CRDTs vs. Traditional Data Synchronization Techniques

Understanding the differences between CRDTs and traditional data synchronization techniques is crucial for developers working on distributed systems. Traditional methods often rely on centralized servers or locking mechanisms to maintain consistency, which can lead to bottlenecks and increased latency.

Key differences include:

  • Conflict Resolution: Traditional systems often require complex conflict resolution strategies, such as last-write-wins or manual merges, which can be error-prone. In contrast, CRDTs inherently resolve conflicts through their mathematical properties, ensuring that all operations commute and are idempotent.
  • Scalability: CRDTs are designed to scale across distributed nodes without the need for coordination, making them ideal for environments with high availability requirements. Traditional systems can struggle as the number of nodes increases, leading to performance degradation.
  • Latency: CRDTs allow for local updates that are immediately visible to users, reducing perceived latency. Traditional approaches may require waiting for a central server to process and propagate changes, resulting in delays.

For instance, in collaborative applications like Google Docs, traditional synchronization methods can create conflicts when multiple users edit the same document simultaneously. CRDTs, on the other hand, allow users to make changes concurrently without the risk of overwriting each other's work.

Overall, while traditional synchronization techniques have their place, CRDTs provide a robust alternative for building responsive and scalable collaborative applications, particularly in distributed environments.

Learn more

Frequently Asked Questions

CRDT vs Operational Transform?

OT (Google Docs) — server-authoritative, transforms operations. CRDT — peer-to-peer compatible, merges without a server. CRDT wins offline-first.

Performance?

Yjs handles thousands of edits/sec. Automerge is slower but more featureful. Both in production (Linear, Figma).

Do I need it for a simple todo app?

No — overhead. CRDTs make sense for collaborative editing or true offline-first UX.

Try the live tool that powered this guide

Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.