Catalog My learning Blog
Distributed Systems Explained: CAP, Clocks and Replication · Phase 2 · Lesson 10 · 5 min read

Conflict Resolution: Last-Write-Wins vs CRDTs

On this page
  1. Write conflicts: two regions, one counter
  2. Last-write-wins (LWW)
  3. Detecting conflicts with vector clocks
  4. CRDTs: conflicts that resolve themselves
  5. An observed-remove set
  6. Choosing a conflict resolution strategy
  7. Check yourself

Write conflicts: two regions, one counter

A like counter is replicated in two regions, both accepting writes. During a network partition, Europe records 3 likes and the US records 5 likes and 1 unlike. When the link heals, the replicas disagree. Which value is right? Neither alone: the correct total is 3 + 5 − 1 = 7. Multi-leader and leaderless replication (Day 7) make conflicts like this inevitable, so the resolution strategy is a design decision.

Last-write-wins (LWW)

Attach a timestamp to each write and keep the newest. Simple, and used widely (Cassandra's default per column). But:

  • it discards all but one concurrent write, silently,
  • "newest" depends on clocks that disagree (Day 3), so it may discard the write that came later.

For the counter, LWW keeps one region's value and loses the other's change. LWW is acceptable only where losing a concurrent update is fine, such as a "last seen" time or a cache entry.

Detecting conflicts with vector clocks

Vector clocks (Day 4) detect whether two versions are causally ordered or concurrent. Ordered: keep the later one. Concurrent: a real conflict. Then:

  • Keep both as siblings and let the application or user merge on the next read (the original Dynamo shopping cart merged by taking the union of items).
  • Apply a domain rule: for a document, keep both edits and flag for review; for a status, apply a precedence order ("cancelled" beats "shipped").

Correct, but every reader must handle siblings, which spreads complexity through the code.

CRDTs: conflicts that resolve themselves

Conflict-free replicated data types are designed so concurrent updates always merge to the same result, regardless of order or duplication. Merge must be commutative, associative and idempotent. A PN-counter keeps per-node increment and decrement totals and merges with an element-wise maximum:

Python
# crdt.py: a PN-counter that merges concurrent updates without conflicts
class PNCounter:
    def __init__(self, node):
        self.node, self.p, self.n = node, {}, {}     # increments and decrements per node

    def inc(self, k=1): self.p[self.node] = self.p.get(self.node, 0) + k
    def dec(self, k=1): self.n[self.node] = self.n.get(self.node, 0) + k
    def value(self): return sum(self.p.values()) - sum(self.n.values())

    def merge(self, other):                          # element-wise max: commutative, idempotent
        for mine, theirs in ((self.p, other.p), (self.n, other.n)):
            for node, count in theirs.items():
                mine[node] = max(mine.get(node, 0), count)

eu, us = PNCounter("eu"), PNCounter("us")
eu.inc(3); us.inc(5); us.dec(1)                      # concurrent updates during a partition
eu.merge(us); us.merge(eu); eu.merge(us)             # merge in any order, any number of times
print(eu.value(), us.value())                        # 7 7

Both replicas converge to 7, the right answer, no matter how often or in which order they exchange state. Last-write-wins on the same data would keep one region's value and lose the other's.

Last-write-wins keeps one concurrent update and drops the other; vector clocks detect the conflict for the application; CRDTs merge both automatically.
Last-write-wins keeps one concurrent update and drops the other; vector clocks detect the conflict for the application; CRDTs merge both automatically.

Other CRDTs: grow-only and observed-remove sets (add and remove items concurrently), registers with multi-value semantics, maps, and sequence CRDTs for collaborative text editing (the idea behind libraries such as Automerge and Yjs). Redis Enterprise's active-active databases and Riak's data types use them.

An observed-remove set

Sets are harder than counters, because add and remove don't commute: if one replica removes "milk" while another adds it, which wins? An observed-remove set (OR-set) answers "the add". Every add gets a unique tag, and a remove deletes only the tags that replica has observed. A concurrent add carries a tag the remove never saw, so it survives the merge.

Python
# orset.py: an observed-remove set; a concurrent add survives a remove
import uuid

class ORSet:
    def __init__(self):
        self.adds, self.removes = set(), set()          # sets of (item, unique tag)

    def add(self, item):
        self.adds.add((item, uuid.uuid4().hex))         # every add gets a fresh tag

    def remove(self, item):                             # remove only the tags seen here
        self.removes |= {(i, t) for (i, t) in self.adds if i == item}

    def items(self):
        return sorted({i for (i, t) in self.adds - self.removes})

    def merge(self, other):                             # union both sides: order doesn't matter
        self.adds |= other.adds
        self.removes |= other.removes

phone, laptop = ORSet(), ORSet()
phone.add("milk"); laptop.merge(phone)                  # both replicas have milk
phone.remove("milk")                                    # phone removes it...
laptop.add("milk")                                      # ...while laptop adds it again, offline
phone.merge(laptop); laptop.merge(phone)
print(phone.items(), laptop.items())
Code
['milk'] ['milk']

Both replicas end with milk, because the laptop's add happened without seeing the phone's remove. Add-wins is what a shopping list or a cart usually wants. The cost is that removed tags must be kept as tombstones until every replica has seen the remove, which is part of the metadata cost below. Production CRDT libraries compact tombstones; this version never does.

CRDTs aren't free: metadata grows with the number of replicas and operations, and only some operations fit the model. "Balance must never go below zero" can't be enforced by merging after the fact; that needs coordination.

Choosing a conflict resolution strategy

DataStrategy
Last-seen time, cached valuesLast-write-wins
Counters, likes, set membership, collaborative textCRDTs
Business records with rules (orders, documents)Detect with version vectors; resolve with domain logic
Invariants that must never break (balances, unique names)Avoid concurrent writes: single leader or consensus

Check yourself

It keeps one replica's value and silently discards concurrent changes made elsewhere. If two regions each increment a counter from 10 to 11, the merged result is 11 instead of 12, and the lost increment is never reported.

The merge is commutative, associative and idempotent, so merging states in any order, any number of times, gives the same result. Replicas can exchange state over an unreliable network and still converge.

No. Two replicas can each accept a withdrawal that's fine locally, and the merged result overdraws the account. Enforcing a global invariant like that needs coordination before accepting writes, such as consensus or a single owner.

The item stays: the remove only deletes the add tags it had observed, and the concurrent add carries a new tag it never saw.