Skip to content
tezvyn:
Menu

CRDTs: Syncing Data Without Locks or Conflicts

Source: Wikipedia: Conflict-free replicated data typeHardHow cards are made

CRDTs are data structures where updates can be applied in any order and reach the same state, avoiding locks. They enable offline editing in collaborative apps and distributed databases.

Why it exists

In distributed systems, keeping data consistent across multiple computers is hard. Traditional methods use locks or consensus algorithms, which reduce availability and performance, especially if replicas can go offline. CRDTs were invented to provide strong eventual consistency without this expensive coordination.

The mental model

Think of a CRDT as a data structure with special operations that are mathematically designed to not conflict. Instead of a generic set(value) operation, you have specific ones like add(element) or increment(). These operations are commutative, meaning their application order doesn't matter. No matter when or in what sequence replicas receive updates, they all eventually converge to the identical state, conflict-free by design.

How it works

CRDTs work because their update operations are guaranteed to produce the same result regardless of order. For example, a Grow-Only Set (G-Set) is a simple CRDT that only allows additions. To merge two G-Set replicas, you simply take the union of their elements. Since you can't remove elements, there's no possible conflict to resolve. Other CRDTs for lists, counters, and registers use similar mathematical properties to ensure convergence without needing a central coordinator to resolve disputes.

When to use it

Use CRDTs when you need high availability, low-latency writes, and offline-first capabilities. They are a natural fit for collaborative applications (like Google Docs or Figma), distributed counters (like social media likes), multi-player game state, and chat applications where any user can send a message at any time, even with a spotty connection.

When not to use it

Avoid CRDTs when you require strict, immediate consistency or need to enforce complex business rules. For example, guaranteeing a username is unique across a whole system cannot be solved with a simple CRDT set, as two users could independently claim the same name on different replicas. These scenarios still require traditional coordination mechanisms or transactional guarantees.

One canonical example

A G-Counter (Grow-Only Counter) is a basic CRDT. Each replica in the system maintains its own counter. To increment the global count, a replica simply increments its local counter. The total value is the sum of all local counters across all replicas. When replicas sync, they share their values, and everyone can calculate the same total sum. No increment is ever lost, and all replicas eventually agree on the final count.

Interview question

Which scenario presents a fundamental challenge for direct implementation using a simple CRDT?

  • a.Allowing multiple users to concurrently edit a document, ensuring eventual consistency.
  • b.Enabling users to add items to a shared list even when offline, with eventual synchronization.
  • c.Maintaining a shared counter that aggregates increments from multiple distributed clients.
  • d.Ensuring a username is strictly unique across all distributed system replicas at all times.Correct
Why?

The card explicitly states that CRDTs are not suitable when you need to enforce complex business rules like guaranteeing a username is unique across a whole system, as two users could independently claim the same name on different replicas. The other options describe scenarios where CRDTs are highly effective due to their conflict-free merging properties.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on distributed systems — each one lists the topics its interview covers.

See open roles