Merging edits without a referee: CRDTs
Two copies of the same data are edited while they cannot talk. When they meet, which edit wins? A CRDT is a data type whose copies always merge to the same answer, with no server to decide. Here is how counters, sets and text manage it.
01 Two offline edits and no one to decide
A notes app on a phone and a laptop. Both go offline, both are edited, both come back. A database with one leader would have refused one of the edits, or would pick a winner by timestamp. Here there is no leader and no network at the time of the edit, so each copy must accept writes on its own and sort it out later.
A CRDT (conflict-free replicated data type) is a data type built so that sorting it out is automatic. Each replica changes its own copy freely. When copies meet, a merge function combines them, and every replica that has received the same set of updates ends up in the same state, whatever order they arrived in. The idea was named and formalised by Marc Shapiro and colleagues in 2011.
02 Why the obvious merges lose data
Take a like counter at 5 on two replicas. Each gets one like while they cannot talk, so each shows 6. The right answer after merging is 7. The obvious merges both get it wrong:
max says 6. It cannot tell two different likes from the same one seen twice.{A: 1, B: 1} on top of the base. Taking the max of each entry is safe to repeat and in any order.The fix is to keep enough structure that a merge can tell what is new from what it already has. For a counter, that means a count per replica instead of one number.
03 A grow-only counter, merged by gossip
In a G-Counter each replica raises only its own entry. Merge takes the larger value per entry. The counter's value is the sum of the entries.
- just raised or merged
- all replicas agree
Three replicas of a like counter, each holding one count per replica, all 0. Each replica only ever raises its own entry.
To also allow decrements, a PN-Counter keeps two G-Counters, one for increments and one for decrements, and reports the difference. You cannot subtract from a G-Counter directly, because a lower number would look like old news and lose to the max.
04 The three rules a merge must follow
A merge that converges no matter how messages travel has three properties. Together they make it a join in maths terms: states only move upward, and merging finds the smallest state that includes both.
| Property | Means | Protects you from |
|---|---|---|
| Commutative | merge(a, b) = merge(b, a) | Updates arriving in different orders on different replicas |
| Associative | merge(merge(a, b), c) = merge(a, merge(b, c)) | Updates arriving by different routes, some already combined |
| Idempotent | merge(a, a) = a | The same update delivered twice, as gossip and retries do |
You can check the counter against all three by brute force: try every delivery order and add a duplicate.
That is a real run with Python 3.14. Six orders, a duplicate in each, one answer. The naive merge reports 2 out of 4 likes.
05 Sets: what wins when add and remove cross
Sets are harder because operations can contradict each other. If Ana removes milk while Ben adds it, the merge has to choose. An observed-remove set (OR-Set) chooses: a remove only cancels the adds it has seen. Any add it could not have seen survives. This is called add wins.
- just removed
- just added or merged
- replicas agree
A shared shopping list on two phones. Each add was stored with a unique tag: milk was added on A as tag a1, eggs as a2. Both replicas have both.
| Type | Holds | Merge | Catch |
|---|---|---|---|
| G-Counter | A count per replica | Max per entry | Only goes up |
| PN-Counter | Two G-Counters | Merge each | Can go below zero; no stock limits |
| LWW-Register | A value and a timestamp | Newest timestamp wins | Concurrent writes: one silently lost, and clocks can lie |
| G-Set | Elements | Union | Nothing can be removed |
| 2P-Set | Adds and removes as two G-Sets | Union each; removed wins | Once removed, never back |
| OR-Set | Elements with unique add tags, removed tags | Union; a tag is live unless removed | Removed tags pile up |
06 Text: giving every character an identity
Collaborative text editors such as those built on Yjs or Automerge use sequence CRDTs. Positions like "index 3" shift whenever someone types earlier in the document, so they cannot be merged. Instead, every character gets a permanent id, made from the replica that typed it and a counter, and each insert says which character it goes after.
| Char | Id | Inserted after | Typed by |
|---|---|---|---|
| c | A1 | (start) | Ana, earlier |
| a | A2 | A1 | Ana, earlier |
| t | A3 | A2 | Ana, earlier |
| h | A4 | A1 | Ana, now |
| r | B4 | A1 | Ben, now |
Three characters now claim the spot after c: a, h and r. Every replica sorts them by the same rule on their ids. In RGA, one common design, a higher counter goes first and a tie is broken by replica name, so r (B4), then h (A4), then a (A2): every replica shows crhat. It may not be what either person pictured, but it is the same on every screen. A deleted character is usually kept as a hidden tombstone, because later inserts may still point at it.
Converging is not the same as being what people meant. If Ana and Ben both fix the same typo differently, a CRDT keeps both fixes, side by side, and every replica agrees on that odd result. CRDTs remove the need for a referee; they do not remove the need for people to look at the merged text.
07 Sending states or sending operations
The counter above shipped its whole state. Many systems ship each operation instead, which is smaller but asks more of the network.
08 Where CRDTs bite
stock -= 1Rules that span replicasA PN-Counter will happily merge two sales of the last item into a stock of -1. Any rule like "never below zero" or "usernames are unique" needs coordination, or a design that splits the stock between replicas in advance.replica_id = "phone"Two replicas with the same idIf two devices or two browser tabs share a replica id, they overwrite each other's entries and increments vanish. Give every writer, often every session, its own id.