Sub topic 12
Resolving conflicts: last-write-wins and CRDTs
This chapter assumes you know what replication is, how eventual consistency allows replicas to diverge, and how vector clocks detect concurrent writes. You should be able to read short Python functions.
Why conflicts need a rule
Under eventual consistency, two replicas can accept writes to the same key at the same time. When the replicas exchange data, each holds a different value. A vector clock tells you the writes are concurrent, but it does not tell you which value to keep. You need a conflict resolution rule, and every replica must apply the same rule so they converge.
Last-write-wins
Last-write-wins (LWW) attaches a timestamp to each write. On conflict, the replica keeps the value with the highest timestamp and discards the others. It is simple, needs no application logic, and always converges.
def lww_merge(a, b):
"""Each value is a (timestamp, data) tuple. Keep the later write."""
return a if a[0] >= b[0] else b
print(lww_merge((10, "red"), (12, "blue"))) # (12, 'blue')The cost is silent data loss. Wall clocks on different machines drift, so "last" may not match the real order of events. A replica with a fast clock can overwrite newer data. Even with perfect clocks, one of two truly concurrent writes is discarded without any error.
CRDTs
A conflict-free replicated data type (CRDT) is a data structure designed so that concurrent updates always merge without losing information. The merge function must be:
- Commutative: merge(a, b) equals merge(b, a).
- Associative: grouping of merges does not change the result.
- Idempotent: merging the same state twice changes nothing.
With these properties, replicas can receive updates in any order, with duplicates, and still converge.
The simplest example is a grow-only counter (G-Counter). Each replica increments only its own slot. The merge takes the maximum per slot, and the value is the sum of all slots.
def merge(a, b):
"""Merge two grow-only counters (G-Counters) held by different replicas."""
return {k: max(a.get(k, 0), b.get(k, 0)) for k in set(a) | set(b)}
def value(counter):
return sum(counter.values())
r1 = {"r1": 3}
r2 = {"r2": 2}
print(value(merge(r1, r2))) # 5
print(merge(r1, r2) == merge(r2, r1)) # TrueBoth increments survive. An LWW register would have kept only one of them.
Common errors
- Using the maximum without per-replica slots. A single shared number with max-merge loses concurrent increments.
- Assuming a G-Counter supports decrement. It does not. A PN-Counter uses two G-Counters, one for increments and one for decrements.
- Expecting CRDTs to preserve every application rule. A CRDT guarantees convergence, not invariants such as "balance never below zero".
Choosing between them
| Approach | Preserves concurrent updates | Complexity | Typical use |
|---|---|---|---|
| Last-write-wins | No | Low | Caches, session data |
| CRDT | Yes, by design | Higher | Counters, sets, collaborative editing |
| Application merge | Depends on your code | Highest | Domain-specific data |
Exercise
Two replicas each start with a G-Counter {"r1": 0, "r2": 0}. Replica r1 increments twice and replica r2 increments three times while disconnected. What is the merged value, and why would a plain LWW register holding the count give a different answer?
Show answer
The merged counter is {"r1": 2, "r2": 3}, with value 5. An LWW register would hold either 2 or 3, depending on which timestamp is later, because it keeps one whole value and discards the other. That loses the concurrent increments.