The Simplest Conflict Resolution: The Clock Decides
Imagine two people editing a document title simultaneously. One approach: whoever has the later timestamp wins. This is Last-Write-Wins (LWW) — the simplest CRDT-adjacent strategy.
LWW is not a "pure" CRDT in the academic sense because it can lose data, but it's widely used in practice (Cassandra uses it by default) and understanding it clarifies why more sophisticated CRDTs exist.
How the LWW-Element-Set Works
An LWW-Element-Set maintains two internal sets:
- Add Set (A): Stores
(element, timestamp)for every addition. - Remove Set (R): Stores
(element, timestamp)for every removal.
To check if an element currently exists:
Look up the element in both sets and compare timestamps:
element_exists(e):
a_time = A[e].timestamp (or -∞ if not present)
r_time = R[e].timestamp (or -∞ if not present)
return a_time > r_time
Example walkthrough:
| Event | Add Set | Remove Set | Exists? |
|---|---|---|---|
| Alice adds "Task A" at t=10 | {Task A: 10} |
{} |
✅ Yes (10 > -∞) |
| Bob removes "Task A" at t=15 | {Task A: 10} |
{Task A: 15} |
❌ No (10 < 15) |
| Alice re-adds "Task A" at t=20 | {Task A: 20} |
{Task A: 15} |
✅ Yes (20 > 15) |
Merging two replicas is straightforward: take the maximum timestamp for each element in each set.
The Catch: Clock Skew
LWW relies entirely on wall-clock timestamps. In a distributed system, clocks are never perfectly synchronized — a difference of even a few milliseconds can cause a "newer" operation to appear older. If Alice's laptop clock is 30 seconds slow, her edits at her local t=100 might be treated as older than Bob's edits at his local t=90 (which is actually earlier in real time).
The result: silent data loss. Alice's work disappears with no error, no warning, no way to recover. This is why LWW is appropriate for metrics and caches (where losing a data point is acceptable) but dangerous for user-generated content.
Key Point: LWW is operationally simple and performs well, but it trades correctness for simplicity. Use it when data loss is acceptable (telemetry, leaderboards) and avoid it when preserving every user action matters (collaborative editing, shopping carts).