Sub topic 9
Resolving conflicts: last-writer-wins, version tracking and CRDTs
This chapter assumes you know how asynchronous cross-region replication works and why two regions can accept writes to the same record at the same time. If you can't avoid conflicts by assigning write ownership, you need a rule for resolving them. Three common approaches are last-writer-wins, version tracking, and CRDTs.
Why conflicts happen
With asynchronous replication, each region accepts a write before it knows about writes made elsewhere. If eu-west-1 and us-east-1 both update the same record before replication catches up, each region holds a different value, and neither is wrong. The system must pick a merge rule. A good rule is deterministic (the same inputs give the same result) and order-independent (every region ends with the same value, whatever order the updates arrive in).
Last-writer-wins
Last-writer-wins (LWW) keeps the version with the highest timestamp. A tie-breaker, such as the region name, makes every region choose the same winner.
def lww_merge(a, b):
"""Each version is a (timestamp, region, value) tuple.
The higher timestamp wins; the region name breaks ties
so every region picks the same winner."""
return a if (a[0], a[1]) >= (b[0], b[1]) else b
from_eu = (1700000005.2, "eu-west-1", "blue")
from_us = (1700000005.2, "us-east-1", "green")
print(lww_merge(from_eu, from_us)) # (1700000005.2, 'us-east-1', 'green')
print(lww_merge(from_us, from_eu)) # same result in either orderLWW is simple and converges. It has two costs:
- It silently discards the losing write. The user who wrote "blue" never learns that it was lost.
- It relies on clocks. If one region's clock runs ahead, its writes win even when they happened earlier.
Version tracking
Version tracking detects conflicts instead of hiding them. Each replica keeps a version vector, which is a map from region to the number of updates that region has applied. When two versions meet, you compare vectors:
- If one vector is greater than or equal to the other in every slot, it descends from the other, so it replaces it.
- If neither dominates, the writes were concurrent. The system keeps both versions (called siblings) and either merges them with application logic or asks the user to choose.
This avoids the clock problem and loses no data. The price is complexity: clients or application code must handle siblings, and stored versions carry extra metadata.
CRDTs
A conflict-free replicated data type (CRDT) is a data structure designed so that any two replicas can be merged without conflict. The merge function is commutative, associative, and idempotent. Replicas converge no matter how updates are ordered, delayed, or duplicated.
A grow-only counter (G-counter) shows the idea. Each region increments only its own slot, and a merge takes the per-region maximum.
def merge_counters(a, b):
"""G-counter CRDT: each region increments only its own slot.
Merging takes the per-region maximum."""
return {r: max(a.get(r, 0), b.get(r, 0)) for r in a.keys() | b.keys()}
def value(counter):
return sum(counter.values())
eu = {"eu-west-1": 3, "us-east-1": 1}
us = {"eu-west-1": 2, "us-east-1": 4}
merged = merge_counters(eu, us)
print(merged, value(merged)) # {'eu-west-1': 3, 'us-east-1': 4} 7Other CRDTs include PN-counters (two G-counters, one for increments and one for decrements), sets, and registers. Their limit is expressiveness. You can't express every business rule, such as "stock must never go below zero", as a CRDT.
Choosing a strategy
| Strategy | Detects conflicts | Loses data | Needs synchronized clocks | Best for |
|---|---|---|---|---|
| Last-writer-wins | No | Yes | Yes | Low-value, overwrite-style fields |
| Version tracking | Yes | No | No | Documents and carts with app-level merge |
| CRDT | Not needed | No | No | Counters, sets, collaborative data |
Exercise
Two regions update the same user profile at nearly the same moment, and the us-east-1 clock is 3 seconds ahead. Under last-writer-wins, which write can win incorrectly, and which alternative would detect the conflict instead?
Show answer
The us-east-1 write can win even if it happened earlier, because its timestamp is inflated by the clock skew, and the other write is dropped silently. Version tracking with version vectors would detect that the writes were concurrent and keep both as siblings for merging.
Exercise
In the G-counter example, why does merging {"eu-west-1": 3, "us-east-1": 1} with {"eu-west-1": 2, "us-east-1": 4} take the maximum per region instead of adding the values?
Show answer
Each region only increments its own slot, so a higher number in a slot is simply a newer view of the same count. Adding would double-count increments the other replica already saw. Taking the maximum is also idempotent, so duplicated or reordered merges give the same result.