Distributed systems
Concurrent writes and last-write-wins
A conflict happens when two replicas accept writes that neither has seen. Last-write-wins always picks one, but it silently drops the other write and trusts clocks that drift.
A single-leader database never has write conflicts, because one node puts every write in order. Once more than one replica accepts writes, two of them can change the same record at the same time. When they sync, something has to decide the result. The simplest decision is last-write-wins.
Concept
Before you start. Two writes are concurrent when neither replica had seen the other's write when it made its own. A timestamp is the time a write was stamped with. Replication is copying writes to other machines. See replication and clocks and ordering.
The big idea
Picture two people editing a shared document offline. Each one saves a new title. When they sync, the document has to show one title. Last-write-wins keeps the one with the higher timestamp and throws away the other.
The picture breaks down with clocks. The timestamp is only as honest as the clock that wrote it. A clock that runs fast wins every race, even when its write really happened first.
Walking through it
Two replicas both set the title of a document. The clocks are off, with numbers chosen for illustration.
Replica A clock: 10:00:05 writes title = "Q3 plan" ts = 10:00:05
Replica B clock: 10:00:02 writes title = "Q3 roadmap" ts = 10:00:02
(B's write is really 1 second later, but its clock is 3 s slow)
Last-write-wins keeps ts 10:00:05 -> title = "Q3 plan"
B's write is gone. No error is shown to anyone.Last-write-wins is safe when losing a concurrent update does no harm:
- Each write has a unique key, so nothing is overwritten.
- The value is a cache entry, a "last seen" time, or a preference such as a theme.
- Conflicts are rare, and a single atomic field is fine with "one of them".
Common mistakes
- Thinking last-write-wins only drops old writes. It drops whichever write has the lower timestamp, which can be the newer one.
- Using it for money or inventory. A silently lost update is a silently lost payment.
- Trusting device clocks. Phone and laptop clocks drift. A phone set wrong can win every conflict.
Check yourself
- Two items are added to a cart on two replicas at the same time. What does last-write-wins keep? Only one write's value. The other item disappears, and nothing reports the loss.
- Why does a fast clock win a conflict against a later write? Last-write-wins compares timestamps, not the real order of events.
Key takeaways
- Writes are concurrent when neither replica had seen the other.
- Last-write-wins always converges, but it drops concurrent writes without an error.
- It depends on clocks, and clock drift decides the winner.
- Use it only where losing a concurrent update is harmless.
Further learning
Next, see how replicas can merge writes instead of dropping them, in CRDT counters.
Version vectors can detect concurrency exactly, without trusting clocks. See clocks and ordering.
Related
Conflict resolution and CRDTs
What to do when replicas accept concurrent writes. The options run from last-write-wins and siblings to CRDTs, operational transformation and the sync engines used by collaborative editors.
Time, clocks and ordering
Why wall clocks lie across machines, how skew silently loses writes, and how Lamport clocks, vector clocks, hybrid logical clocks and TrueTime order events you can't time directly.
Replication
Keeping copies of the same data on several machines, the three topologies that databases use, and the anomalies that replication lag causes. Start here for the replication map.