Quick revision
The short answer
A CRDT's merge must be commutative, associative and idempotent, so the order, grouping and repetition of merges never change the result; mathematically the states form a join-semilattice. State-based CRDTs send their whole state or deltas and merge with a join, while operation-based CRDTs send operations, which must commute and need reliable, exactly-once and usually causally ordered delivery. A G-Counter gives each replica its own slot, merges by per-slot maximum and reads the sum, and an OR-Set tags every add with a unique id so a remove deletes only the tags it has seen and a concurrent add survives. The costs are growing metadata and invariants, such as a balance never below zero, that CRDTs cannot enforce.
What to remember
In this answer, we saw that CRDTs merge concurrent updates instead of choosing between them, and that this works because the merge is commutative, associative and idempotent, forming a join-semilattice. State-based CRDTs send state and tolerate any delivery, while operation-based ones send small operations but need exactly-once, causally ordered delivery. A G-Counter merges per-replica slots by maximum and sums them, and an OR-Set tags adds so concurrent adds survive removes. Metadata grows with replicas and tombstones, and invariants such as non-negative balances still need coordination.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A CRDT's merge must be commutative, associative and idempotent, so the order, grouping and repetition of merges never change the result; mathematically the states form a join-semilattice. State-based CRDTs send their whole state or deltas and merge with a join, while operation-based CRDTs send operations, which must commute and need reliable, exactly-once and usually causally ordered delivery. A G-Counter gives each replica its own slot, merges by per-slot maximum and reads the sum, and an OR-Set tags every add with a unique id so a remove deletes only the tags it has seen and a concurrent add survives. The costs are growing metadata and invariants, such as a balance never below zero, that CRDTs cannot enforce.
Why this question is asked
CRDTs power collaborative editors, offline-first apps, shopping carts in replicated stores and counters in geo-distributed databases such as Redis Enterprise's active-active mode and Riak. Interviewers ask about them to see whether we understand why some data can be merged automatically and what makes a merge safe. In this answer, we will recap the problem CRDTs solve, explain the three properties of a merge, compare state-based and operation-based designs, build a grow-only counter in Java, walk through an observed-remove set, and finish with the costs.
1A Quick Recap: Merging Instead of Choosing
In a replicated system where every replica accepts writes, two replicas can change the same data concurrently. Last write wins resolves this by choosing one version and discarding the other. A conflict-free replicated data type, or CRDT, takes a different approach: it designs the data structure so that any two versions can be merged into one that contains both changes, and every replica that has seen the same updates reaches the same state, without any coordination.
That is a strong promise, and it works only for data types whose operations fit together naturally: counters, sets, maps of CRDTs, registers and text sequences.
2The Three Properties a Merge Must Have
Replicas exchange states over an unreliable network: messages arrive late, out of order, and sometimes twice. For every replica to end in the same state anyway, the merge must not care about any of that. Commutative means merge(a, b) equals merge(b, a), so order does not matter. Associative means merge(merge(a, b), c) equals merge(a, merge(b, c)), so grouping does not matter, and replicas can merge in any pattern. Idempotent means merge(a, a) equals a, so receiving the same state twice changes nothing.
A structure with such a merge is called a join-semilattice: the states have a partial order, and the merge of two states is their least upper bound, the smallest state that includes both. Taking the maximum of two numbers is the simplest example; taking the union of two sets is another.
3State-Based Versus Operation-Based
There are two families. State-based CRDTs, also called convergent, periodically send their full state, or in the delta variant just the recent changes, and the receiver merges it with a join. Because the join is idempotent and commutative, the network may duplicate and reorder messages freely; the cost is larger messages.
Operation-based CRDTs, also called commutative, send each operation, such as add 5 or remove x. Messages are small, but the operations must commute, and the delivery layer must deliver every operation exactly once, because applying add 5 twice is wrong, and usually in causal order, so a remove never arrives before the add it refers to. They push the hard part into the messaging layer.
| Aspect | State-based | Operation-based |
|---|---|---|
| What is sent | full state or deltas | each operation |
| Merge or apply | join (least upper bound) | apply commuting operations |
| Network requirements | any order, duplicates fine | exactly-once, usually causal order |
| Message size | larger | small |
| Typical use | gossip between database replicas | collaborative editors over a reliable channel |
4A Grow-Only Counter in Java
A G-Counter counts upwards across replicas. Each replica has its own slot in a map and only ever increments that slot. The merge takes the maximum per slot, and the value is the sum of all slots.
Notice why this works. Because each slot is written by only one replica, a higher value in a slot always means a newer value, so the maximum is the right merge, and the maximum is commutative, associative and idempotent. A plain counter merged by addition would double-count on a repeated message. To support decrements, a PN-Counter pairs two G-Counters, one for increments and one for decrements, and its value is the difference.
5The Observed-Remove Set
Sets are harder because of add and remove. If replica A removes milk while replica B concurrently adds milk, which wins? A naive set cannot tell a fresh add from an old one. The observed-remove set, OR-Set, solves this by tagging every add with a unique id, such as a replica id and a counter. The set holds pairs of element and tag. A remove deletes only the tags it has seen at that replica, recording them as removed.
So when A removes milk, it removes milk with tag a1, which it had observed. B's concurrent add created milk with tag b7, which A never saw, so A's remove does not touch it. After merging, milk with tag b7 is still there: the element is in the set. This is called add wins, and it matches what users usually expect: a concurrent add is a newer intention than a remove of an older copy.
6The Costs and the Limits
CRDTs are not free. A G-Counter holds one slot per replica that ever wrote, so with thousands of devices the metadata grows with them. An OR-Set must remember removed tags, tombstones, so that an old add arriving late is not resurrected, and those tombstones grow with every removal until a garbage collection step, which itself needs some coordination, can safely drop them.
The bigger limit is invariants. A CRDT guarantees convergence, not business rules. A PN-Counter for an account balance will happily converge to minus 50 if two replicas each approve a withdrawal of 100 from a balance of 150, because each was valid locally. Rules such as balance never below zero, or no more than 100 tickets sold, need coordination, for example escrow, where each replica is given a share of the allowance in advance, or a consensus step for the operation that might break the rule.
7Follow-up questions
Why can't a CRDT enforce a balance never below zero?
Because each replica decides locally whether a decrement is allowed, without knowing about concurrent decrements elsewhere. Both can be valid alone and invalid together. Preventing that needs coordination, such as consensus or pre-allocated shares per replica.
What is a delta-state CRDT?
A state-based CRDT that sends only the part of the state that changed recently, which is itself a small state that can be joined. It keeps the tolerance for reordering and duplication of state-based CRDTs with message sizes closer to operation-based ones.
Where are CRDTs used in practice?
In collaborative editing libraries such as Automerge and Yjs, in Riak's data types, in Redis Enterprise active-active databases, and in many offline-first mobile sync engines. Check each product's documentation for which types and semantics it supports.
8Conclusion
In this answer, we saw that CRDTs merge concurrent updates instead of choosing between them, and that this works because the merge is commutative, associative and idempotent, forming a join-semilattice. State-based CRDTs send state and tolerate any delivery, while operation-based ones send small operations but need exactly-once, causally ordered delivery. A G-Counter merges per-replica slots by maximum and sums them, and an OR-Set tags adds so concurrent adds survive removes. Metadata grows with replicas and tombstones, and invariants such as non-negative balances still need coordination.
Open with
A CRDT is a data type designed so replicas can update it independently and merge in any order, always converging to the same state. For that, the merge has to be commutative, associative and idempotent: order, grouping and duplicate deliveries do not change the result.
Then, if they want more
Mathematically the states form a join-semilattice and merge is the least upper bound, like max or set union. There are two styles. State-based CRDTs send their state, or deltas, and merge with that join, so the network can reorder and duplicate freely.
Operation-based ones send operations, which are smaller, but the operations must commute and the delivery layer must give exactly-once and usually causal delivery, because applying add 5 twice is wrong. A G-Counter has one slot per replica; each replica increments only its own slot, merge takes the per-slot maximum, and the value is the sum.
A PN-Counter is two G-Counters, one for increments and one for decrements. An OR-Set tags every add with a unique id. A remove deletes only the tags that replica has seen, so if someone concurrently adds the same element with a new tag, that add survives, which is add-wins semantics.
The costs are metadata that grows with the number of replicas and with removed elements, and the fact that CRDTs only guarantee convergence, not invariants. A rule like balance never below zero needs coordination, such as consensus or escrow.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Which properties must a CRDT's merge have, how do state-based and operation-based CRDTs...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/60192bcc6f/