Data types that can be changed on many machines and always merge to the same result.
A CRDT (Conflict-free Replicated Data Type) is a data type built so that many machines can change their own copy at the same time, without locking or asking anyone, and the copies always merge to the same result.
Say three replicas of a "likes" counter each receive some clicks while the network between them is broken. Later they reconnect. A plain number cannot tell which copy is right: if each copy says 5, 7 and 6, taking the newest or the biggest gives a wrong total, and some likes are lost.
A CRDT solves this by choosing a shape of data and a merge function that cannot conflict. The merge must be:
With these three rules, it does not matter if messages arrive late, out of order, or twice. Every replica ends in the same state once it has seen all updates.
| Style | What travels | Needs from the network | Cost |
|---|---|---|---|
| State-based | The full state (or a delta of it) | Almost nothing: messages can be lost, repeated or reordered | Larger messages |
| Operation-based | Small operations like โ+1โ or โadd xโ | Each operation delivered at least once, with causal order (no duplicates, or made idempotent) | Small messages, stronger delivery needs |
| Type | What it holds | How conflicts are handled |
|---|---|---|
| G-Counter | A count that only goes up | One slot per replica, merge takes the max of each slot |
| PN-Counter | A count that can go up and down | Two G-Counters: one for adds, one for subtracts |
| G-Set | A set you can only add to | Merge is a union |
| OR-Set (observed-remove) | A set with add and remove | Each add gets a unique tag, and remove only deletes the tags it has seen. A concurrent re-add wins. |
| LWW-Register | A single value | Highest timestamp wins. Simple, but the loser's write is lost. |
| Sequence / text CRDT | An ordered list or document | Every character gets a unique, ordered ID (used by Yjs and Automerge) |
Both rows get the same clicks and the same syncs. The +1 buttons in the two rows do the same thing.
The G-Counter is the simplest CRDT. Each machine only ever adds to its own slot, and merging takes the biggest value per slot. That merge gives the same answer in any order, any number of times (commutative, associative, idempotent), so the copies always converge.
| Term | Meaning in easy words |
|---|---|
| CRDT | Conflict-free Replicated Data Type: data designed so all replicas converge by merging |
| Replica | One copy of the data, on one machine or device |
| Strong eventual consistency | Replicas that have seen the same updates are always in the same state |
| Commutative | Order of merging does not change the result |
| Associative | Grouping of merges does not change the result |
| Idempotent | Merging the same data again changes nothing |
| State-based (CvRDT) | Replicas send their full state and merge it |
| Operation-based (CmRDT) | Replicas send operations and apply them |
| G-Counter / PN-Counter | A grow-only counter, and one that can also decrease |
| OR-Set | A set with add and remove, using unique tags to avoid conflicts |
| Tombstone | A marker left behind for a removed item, so a merge knows it was removed |
| Choice | We gain | We lose |
|---|---|---|
| CRDT instead of a leader | Always-available writes, low latency | No strict global order or limits |
| State-based | Simple, robust to bad networks | Bigger messages |
| Operation-based | Small messages | Needs reliable causal delivery |
| Last-write-wins register | Very simple | Concurrent writes are dropped silently |
| OR-Set | No lost adds, sensible removes | Extra metadata for tags and tombstones |
| Text CRDT | Works offline and peer-to-peer, no central server | Heavier than a server-ordered approach like OT |