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.
Free online tool — HTTP header checker: instant results, no signup.
// Yjs collaborative text
const ydoc = new Y.Doc();
const ytext = ydoc.getText('content');
ytext.insert(0, 'Hello');
// Syncs over WebSocket — other users see updateCRDTs 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:
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.
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.
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:
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.
OT (Google Docs) — server-authoritative, transforms operations. CRDT — peer-to-peer compatible, merges without a server. CRDT wins offline-first.
Yjs handles thousands of edits/sec. Automerge is slower but more featureful. Both in production (Linear, Figma).
No — overhead. CRDTs make sense for collaborative editing or true offline-first UX.
Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.