Conflict-free replicated data type

In distributed computing, a conflict-free replicated data type (CRDT) is a data structure that is replicated across multiple computers in a network, with the following features:

  1. The application can update any replica independently, concurrently and without coordinating with other replicas.
  2. An algorithm (itself part of the data type) automatically resolves any inconsistencies that might occur.
  3. Although replicas may have different state at any particular point in time, they are guaranteed to eventually converge.

The CRDT concept was formally defined in 2011 by Marc Shapiro, Nuno Preguiça, Carlos Baquero and Marek Zawirski. Development was initially motivated by collaborative text editing and mobile computing. CRDTs have also been used in online chat systems, online gambling, and in the SoundCloud audio distribution platform. The NoSQL distributed databases Redis, Riak and Cosmos DB have CRDT data types.

Background

Concurrent updates to multiple replicas of the same data, without coordination between the computers hosting the replicas, can result in inconsistencies between the replicas, which in the general case may not be resolvable. Restoring consistency and data integrity when there are conflicts between updates may require some or all of the updates to be entirely or partially dropped.

Accordingly, much of distributed computing focuses on the problem of how to prevent concurrent updates to replicated data. But another possible approach is optimistic replication, where all concurrent updates are allowed to go through, with inconsistencies possibly created, and the results are merged or "resolved" later. In this approach, consistency between the replicas is eventually re-established via "merges" of differing replicas. While optimistic replication might not work in the general case, there is a significant and practically useful class of data structures, CRDTs, where it does work — where it is always possible to merge or resolve concurrent updates on different replicas of the data structure without conflicts. This makes CRDTs ideal for optimistic replication.

As an example, a one-way boolean event flag is a trivial CRDT: one bit, with a value of true or false. True means some particular event has occurred at least once. False means the event has not occurred. Once set to true, the flag cannot be set back to false (an event having occurred cannot un-occur). The resolution method is "true wins": when merging a replica where the flag is true (that replica has observed the event), and another one where the flag is false (that replica hasn't observed the event), the resolved result is true — the event has been observed.

Types of CRDTs

There are two approaches to CRDTs, both of which can provide strong eventual consistency: state-based CRDTs and operation-based CRDTs.

State-based CRDTs

State-based CRDTs (also called convergent replicated data types, or CvRDTs) are defined by two types, a type for local states and a type for actions on the state, together with three functions: a function to produce an initial state, a merge function of states, and a function to apply an action to update a state. State-based CRDTs simply send their full local state to other replicas on every update, where the received new state is then merged into the local state. To ensure eventual convergence the functions should fulfill the following properties: The merge function should compute the join for any pair of replica states, and should form a semilattice with the initial state as the neutral element. In particular, this means that the merge function must be commutative, associative, and idempotent. The intuition behind commutativity, associativity and idempotence is that these properties are used to make the CRDT invariant under package re-ordering and duplication. Furthermore, the update function must be monotone with regard to the partial order defined by said semilattice.

Delta state CRDTs (or simply delta CRDTs) are optimized state-based CRDTs where only recently applied changes to a state are disseminated instead of the entire state.

Operation-based CRDTs

Operation-based CRDTs (also called commutative replicated data types, or CmRDTs) are defined without a merge function. Instead of transmitting states, the update actions are transmitted directly to replicas and applied. For example, an operation-based CRDT of a single integer might broadcast the operations “+10” or “−20”. The application of operations should still be commutative and associative. However, instead of requiring that application of operations is idempotent, stronger assumptions on the communications infrastructure are expected — all operations must be delivered to the other replicas without duplication.

Pure operation-based CRDTs are a variant of operation-based CRDTs that reduces the metadata size.

Comparison

The two alternatives are theoretically equivalent, as each can emulate the other. However, there are practical differences. State-based CRDTs are often simpler to design and to implement; their only requirement from the communication substrate is some kind of gossip protocol. Their drawback is that the entire state of every CRDT must be transmitted eventually to every other replica, which may be costly. In contrast, operation-based CRDTs transmit only the update operations, which are typically small. However, operation-based CRDTs require guarantees from the communication middleware; that the operations are not dropped or duplicated when transmitted to the other replicas, and that they are delivered in causal order.

While operations-based CRDTs place more requirements on the protocol for transmitting operations between replicas, they use less bandwidth than state-based CRDTs when the number of transactions is small in comparison to the size of internal state. However, since the state-based CRDT merge function is associative, merging with the state of some replica yields all previous updates to that replica. Gossip protocols work well for propagating state-based CRDT state to other replicas while reducing network use and handling topology changes.

Some lower bounds on the storage complexity of state-based CRDTs are known.

Known CRDTs

G-Counter (Grow-only Counter)

payload integer[n] P
    initial [0,0,...,0]

update increment()
    let g = myId()
    P[g] := P[g] + 1

query value() : integer v
    let v = Σi P[i]

compare (X, Y) : boolean b
    let b = (∀i ∈ [0, n - 1] : X.P[i] ≤ Y.P[i])

merge (X, Y) : payload Z
    let ∀i ∈ [0, n - 1] : Z.P[i] = max(X.P[i], Y.P[i])

This state-based CRDT implements a counter for a cluster of n nodes. Each node in the cluster is assigned an ID from 0 to n - 1, which is retrieved with a call to myId(). Thus each node is assigned its own slot in the array P, which it increments locally. Updates are propagated in the background, and merged by taking the max() of every element in P. The compare function is included to illustrate a partial order on the states. The merge function is commutative, associative, and idempotent. The update function monotonically increases the internal state according to the compare function. This is thus a correctly defined state-based CRDT and will provide strong eventual consistency. The operations-based CRDT equivalent broadcasts increment operations as they are received.

PN-Counter (Positive-Negative Counter)

payload integer[n] P, integer[n] N
    initial [0,0,...,0], [0,0,...,0]
update increment()
    let g = myId()
    P[g] := P[g] + 1
update decrement()
    let g = myId()
    N[g] := N[g] + 1
query value() : integer v
    let v = Σi P[i] - Σi N[i]
compare (X, Y) : boolean b
    let b = (∀i ∈ [0, n - 1] : X.P[i] ≤ Y.P[i] ∧ ∀i ∈ [0, n - 1] : X.N[i] ≤ Y.N[i])
merge (X, Y) : payload Z
    let ∀i ∈ [0, n - 1] : Z.P[i] = max(X.P[i], Y.P[i])
    let ∀i ∈ [0, n - 1] : Z.N[i] = max(X.N[i], Y.N[i])

A common strategy in CRDT development is to combine multiple CRDTs to make a more complex CRDT. In this case, two G-Counters are combined to create a data type supporting both increment and decrement operations. The "P" G-Counter counts increments; and the "N" G-Counter counts decrements. The value of the PN-Counter is the value of the P counter minus the value of the N counter. Merge is handled by letting the merged P counter be the merge of the two P G-Counters, and similarly for N counters. Note that the CRDT's internal state must increase monotonically, even though its external state as exposed through query can return to previous values.

G-Set (Grow-only Set)

payload set A
    initial ∅
update add(element e)
    A := A ∪ {e}
query lookup(element e) : boolean b
    let b = (e ∈ A)
compare (S, T) : boolean b
    let b = (S.A ⊆ T.A)
merge (S, T) : payload U
    let U.A = S.A ∪ T.A

The G-Set (grow-only set) is a set which only allows adds. An element, once added, cannot be removed. The merger of two G-Sets is their union.

2P-Set (Two-Phase Set)

payload set A, set R
    initial ∅, ∅
query lookup(element e) : boolean b
    let b = (e ∈ A ∧ e ∉ R)
update add(element e)
    A := A ∪ {e}
update remove(element e)
    pre lookup(e)
    R := R ∪ {e}
compare (S, T) : boolean b
    let b = (S.A ⊆ T.A ∨ S.R ⊆ T.R)
merge (S, T) : payload U
    let U.A = S.A ∪ T.A
    let U.R = S.R ∪ T.R

Two G-Sets (grow-only sets) are combined to create the 2P-set. With the addition of a remove set (called the "tombstone" set), elements can be added and also removed. Once removed, an element cannot be re-added; that is, once an element e is in the tombstone set, query will never again return True for that element. The 2P-set uses "remove-wins" semantics, so remove(e) takes precedence over add(e).

LWW-Element-Set (Last-Write-Wins-Element-Set)

LWW-Element-Set is similar to 2P-Set in that it consists of an "add set" and a "remove set", with a timestamp for each element. Elements are added to an LWW-Element-Set by inserting the element into the add set, with a timestamp. Elements are removed from the LWW-Element-Set by being added to the remove set, again with a timestamp. An element is a member of the LWW-Element-Set if it is in the add set, and either not in the remove set, or in the remove set but with an earlier timestamp than the latest timestamp in the add set. Merging two replicas of the LWW-Element-Set consists of taking the union of the add sets and the union of the remove sets. When timestamps are equal, the "bias" of the LWW-Element-Set comes into play. A LWW-Element-Set can be biased towards additions or removals. The advantage of LWW-Element-Set over 2P-Set is that, unlike 2P-Set, LWW-Element-Set allows an element to be reinserted after having been removed.

OR-Set (Observed-Remove Set)

OR-Set resembles LWW-Element-Set, but using unique tags instead of timestamps. For each element in the set, a list of add-tags and a list of remove-tags are maintained. An element is inserted into the OR-Set by having a new unique tag generated and added to the add-tag list for the element. Elements are removed from the OR-Set by having all the tags in the element's add-tag list added to the element's remove-tag (tombstone) list. To merge two OR-Sets, for each element, let its add-tag list be the union of the two add-tag lists, and likewise for the two remove-tag lists. An element is a member of the set if and only if the add-tag list less the remove-tag list is nonempty. An optimization that eliminates the need for maintaining a tombstone set is possible; this avoids the potentially unbounded growth of the tombstone set. The optimization is achieved by maintaining a vector of timestamps for each replica.

Sequence CRDTs

A sequence, list, or ordered set CRDT can be used to build a collaborative real-time editor, as an alternative to operational transformation (OT).

Some known Sequence CRDTs are Treedoc, RGA, Woot, Logoot, and LSEQ. CRATE is a decentralized real-time editor built on top of LSEQSplit (an extension of LSEQ) and runnable on a network of browsers using WebRTC. LogootSplit was proposed as an extension of Logoot in order to reduce the metadata for sequence CRDTs. MUTE is an online web-based peer-to-peer real-time collaborative editor relying on the LogootSplit algorithm.

Industrial sequence CRDTs, including open-source ones, are known to out-perform academic implementations due to optimizations and a more realistic testing methodology. The main popular example is Yjs CRDT, a pioneer in using a plainlist instead of a tree (ala Kleppmann's automerge).

Industry use

Limitations

Trade-off with Strong Consistency

CRDTs are designed to remain available when the network is partitioned. According to the CAP theorem, a replicated system that can be affected by network partitions cannot guarantee both strong consistency (in the sense of linearizability) and availability. CRDTs choose availability: each replica accepts updates and answers queries using only its local state, without coordinating with other replicas. In exchange, they only guarantee strong eventual consistency. In his review of the CAP theorem, Eric Brewer described CRDTs as one way to manage operations performed during a partition and reconcile them afterwards.

This choice has several practical consequences:

Stale and Divergent Reads. A replica's state reflects only the updates it has received so far. Different replicas can therefore return different values at the same moment, and a read may not include updates already accepted elsewhere. Convergence is guaranteed only once all replicas have received the same set of updates.

Application Invariants. Because replicas do not coordinate, CRDTs cannot by themselves maintain invariants that depend on concurrent updates made at several replicas. Examples include a counter that must never become negative (such as a stock level or account balance) and a constraint that user names must be unique. Each replica may accept an update that is valid in its local state, and the merged result may still violate the invariant. Bailis et al. characterised which invariants can be preserved without coordination, a property they called invariant confluence. Other invariants need some coordination. For example, the bounded counter keeps a counter above a lower bound by distributing rights to decrement among replicas ahead of time. A replica that has used up its rights must contact others before it can decrement again.

Conflict Resolution Semantics. Merging is automatic, but for operations that do not commute, the merge must follow a fixed arbitration rule, such as last-writer-wins, add-wins or remove-wins. The result may not match what users intended. Under last-writer-wins, one of two concurrent writes is silently discarded. Under remove-wins, a concurrent addition is cancelled. As a result, the effect and output of an operation as first observed at its local replica may change after it is merged with concurrent operations from other replicas.

These anomalies have motivated proposals to strengthen the guarantees that CRDTs provide. Kuznetsov, Perion and Tucci-Piergiovanni framed such data types as an eventual form of state machine replication and proposed adding a stability property. Under this property, the effect of each operation, as first observed at its local replica, may be revised while it is reconciled with concurrent operations, but it eventually stops changing.

Metadata and garbage collection

Many CRDTs keep metadata, such as tombstones or version vectors, to merge concurrent updates correctly. Some of this metadata can be safely discarded once an update is known to have reached every replica, a condition known as causal stability. However, this cannot be established while a replica is unreachable or has crashed, so metadata may keep growing for as long as a partition lasts.

Byzantine Fault Tolerance

Most CRDT algorithms assume that replicas may crash or become disconnected, but that they otherwise follow the protocol correctly. They do not tolerate Byzantine faults, in which some replicas behave arbitrarily or maliciously. For example, a faulty replica might send malformed updates, or send different versions of the same update to different peers, which can prevent correct replicas from converging. This matters especially in peer-to-peer settings, where participants cannot be assumed to be trustworthy.

Some work has addressed this problem. Kleppmann and Howard defined Byzantine eventual consistency and studied which guarantees a peer-to-peer database can provide when an arbitrary number of replicas may be Byzantine. Kleppmann later proposed a scheme for making many CRDTs Byzantine fault tolerant, which can tolerate any number of Byzantine replicas and is therefore immune to Sybil attacks. Almeida and Shapiro proposed the blocklace, a CRDT based on a directed acyclic graph of signed blocks that is designed to resist Byzantine participants. As of 2026, Byzantine-tolerant CRDT designs have mainly been proposed in research prototypes, and mainstream CRDT libraries do not provide Byzantine fault tolerance.[citation needed ]

Hybrid Approaches

Some systems combine CRDTs with coordination for selected operations and give up availability only where it is necessary to preserve correctness. RedBlue consistency is one such approach. It runs commutative "blue" operations locally without coordination, and it places "red" operations, which could break invariants, in a single total order agreed on by all sites. As a result, red operations lose the availability of CRDTs. They cannot complete while a replica is cut off from the sites it must coordinate with, and even without partitions they have higher latency, because each one needs at least one cross-site round trip before it can complete. The performance of such systems therefore depends on keeping the share of red operations small.

See also