0%
Distributed Systems: Consensus, Clocks, and Replication
Foundations
Time and Order
Consistency Models
Consensus
Transactions and Operations
Conflict Resolution and CRDTs
The last few lessons kept arriving at the same problem. Multi leader and leaderless replication accept writes at several replicas without coordination, so concurrent writes conflict, and the system must make replicas converge. Last write wins converges by discarding data. Keeping siblings preserves data but pushes merging onto application code. Conflict free replicated data types, or CRDTs, offer a third path: data types designed so that concurrent updates always merge automatically and deterministically, with no lost updates and no application merge code. This lesson explains how they work, the most important CRDTs and where they fit in real systems.
Strong eventual consistency
Eventual consistency promises that if writes stop, replicas eventually agree, but it says nothing about how they agree or what value they agree on, and it allows replicas to disagree arbitrarily in the meantime. CRDTs provide a stronger guarantee called strong eventual consistency: any two replicas that have received the same set of updates, in any order, are in the same state. There is no separate conflict resolution step and no need to wait for agreement; convergence follows directly from the data type definition. This was formalized by Marc Shapiro, Nuno PreguiƧa, Carlos Baquero and Marek Zawirski in 2011.
Why order independence is the key
Replicas in an uncoordinated system receive updates in different orders, possibly with duplicates when messages are retried, and possibly grouped differently when replicas exchange batches. If the result of applying updates depends on any of these, replicas diverge. So a CRDT merge must give the same result regardless of order, grouping and duplication. Those three requirements have names. Commutative: merging a with b gives the same as merging b with a. Associative: merging a with b and then c gives the same as merging a with the merge of b and c. Idempotent: merging a with itself gives a. A merge function with these three properties lets replicas exchange state in any pattern and still converge.
Join semilattices
The mathematical structure behind state based CRDTs is a join semilattice: a set of states with a partial order, where any two states have a least upper bound, called their join. The merge function computes the join. Updates only ever move a state upward in the order. Because the join of two states is unique and the join operation is commutative, associative and idempotent by definition, replicas that keep merging converge to the join of everything they have seen. Familiar examples are sets ordered by inclusion, with union as join, and integers ordered normally, with maximum as join. Every CRDT in this lesson is built by combining such simple lattices.
A failing example first
It helps to see why ordinary data types fail. Suppose a counter is stored as an integer, and two replicas each start at 10 and each increment once. Each replica holds 11. When they exchange states, each sees 11 from the other. Should the merge take the maximum, giving 11, or the sum, giving 22? The maximum loses an increment; the sum double counts the original 10, and repeated exchanges would keep doubling. No merge of plain integers gives the right answer of 12, because the integer does not record who contributed what. CRDTs fix this by storing enough structure to merge correctly.
State based and operation based CRDTs
There are two styles. State based CRDTs, also called convergent replicated data types, exchange whole states, or parts of states, and merge them with the join. They tolerate any message delivery, including loss, duplication and reordering, as long as states eventually reach every replica. Operation based CRDTs, also called commutative replicated data types, broadcast each update as an operation, which replicas apply. Operations that may be concurrent must commute, and the delivery layer must deliver every operation exactly once, usually in causal order. Op based CRDTs send less data per update; state based CRDTs need less from the network. The two styles are equivalent in expressive power, and a hybrid called delta state CRDTs, covered in the last section, combines their strengths.
What CRDTs cannot do
CRDTs guarantee convergence, not that the converged value satisfies every application invariant. A CRDT counter cannot prevent the count from going below zero when two replicas each decrement concurrently from one; both decrements are accepted, and the result is minus one. A CRDT set cannot enforce that at most ten people join a group. Invariants that depend on the global state, such as uniqueness, bounds and referential integrity, need coordination, as the multi leader lesson explained. Research on invariant confluence by Peter Bailis and colleagues gives a precise test: an invariant can be maintained without coordination if and only if merging any two states that each satisfy it produces a state that also satisfies it.
Where CRDTs are used
CRDTs are used in production in several places. Riak offers counters, sets, maps and flags as built in data types. Redis Enterprise uses CRDTs for active active geo distributed databases. Azure Cosmos DB and SoundCloud built on them. Collaborative editors such as those built on Yjs and Automerge use sequence CRDTs for text. Apple Notes, Figma and many local first applications use CRDT ideas for syncing. Within distributed systems infrastructure, membership lists, presence indicators and configuration flags are often CRDTs.
Monotonic growth
A useful way to think about state based CRDTs is that every update moves the state up and never down. Adding to a set grows it. Incrementing a counter slot raises it. Even removal is expressed as growth: adding a tombstone or a removed tag. Because states only grow in the lattice, a merge can never undo information, and two replicas that have seen different updates can always find a state above both. This is also why naive designs fail: as soon as an update moves a state down, such as decreasing a slot or deleting an element outright, the maximum based merge brings the old value back.
The three ingredients of a CRDT design
Designing a CRDT, or choosing one, comes down to three questions. What is the state, including whatever metadata is needed to merge, such as per replica slots, tags or identifiers? What is the merge, which must be a join? And what is the read, the function that turns the internal state into the value the application sees, such as the sum of slots or the set of elements with live tags? The read can be lossy, presenting a simple value while the state holds much more. Keeping these three apart helps when reasoning about new data types and when debugging unexpected merge results.
Convergence is not correctness
It is worth repeating that convergence only says replicas agree. They may agree on a value the user did not expect. An add wins set may bring back an item a user deliberately removed on another device. A last writer wins register may agree on the older of two titles if clocks were skewed. Choosing a CRDT is therefore partly a product decision about which outcome users should see when concurrent edits happen, and it is worth writing that decision down for each field, so that later engineers know the behavior is intended rather than a bug.
A simple self check for any proposed merge is to ask what happens if the same state arrives twice, if two states arrive in the opposite order and if a replica forwards an already merged state; if any answer differs, the design is not a CRDT.
A CRDT merge is commutative, associative and idempotent, so replicas that receive the same updates in any order, grouping or with duplicates converge. That gives strong eventual consistency without coordination, but not global invariants such as non negative balances or uniqueness.