Concept Simulators / Copies and agreement
Eventual consistency and conflicts
Last write wins, detecting conflicts, and CRDTs when two copies disagree.
On the roadmap:Advanced · step 2: Consistency models
Settings
What is stored
How the copies are merged
Every write carries a timestamp from the clock of the machine that took it. When the copies meet, the highest timestamp is kept and the other write is discarded.
Result after merging
Ada Lovelace
What both users meant
Ada Lovelace
Silently lost
nothing
The merge gave Ada Lovelace, which is the right answer: The later timestamp wins, and here the clocks agree with reality.
What happened, in order
- NetworkThe network between the two copies fails. The name is “Ada”.
- Copy ACopy A sets the name to “Ada L.” at time 100.
- Copy BCopy B sets it to “Ada Lovelace” at time 105 by the real clock, which its own clock reads as 105.
- NetworkThe network heals and the copies compare notes.
The same story, every way of merging
| Merge | Result | Lost silently |
|---|---|---|
| Ada Lovelace | nothing | |
| both kept: “Ada L.” and “Ada Lovelace” | nothing | |
| both kept: “Ada L.” and “Ada Lovelace” | nothing |
Green is right or safely surfaced, red lost something. What both users meant: Ada Lovelace.
In plain words
When two copies of data accept writes while cut off from each other, they disagree when they reconnect, and one rule has to decide what survives. Last write wins is simple and silently loses data. Detecting conflicts keeps both versions for someone to resolve. CRDTs are designed so that merging is automatic and loses nothing.
Why it matters
Systems that stay available during a network failure, such as shopping baskets, collaborative documents and offline apps, must accept conflicting writes and sort them out later. The merge rule decides whether users lose work. Here you can run three kinds of data through three rules, and see which silently throws updates away.
How real systems do it
The simulation is a simplified model. Here is what production systems add or do.
- Apache Cassandra resolves conflicts with last write wins, using the timestamp of each cell, so it relies on clocks being closely synchronised and can silently drop a concurrent write.
- Amazon's Dynamo paper used vector clocks to detect conflicts, and gave the shopping cart as an example where it keeps both versions and merges them, which could bring back an item that a customer had deleted.
- Riak and Redis Enterprise's Active-Active databases offer CRDT data types, such as counters and sets. Libraries such as Automerge and Yjs use CRDTs to merge edits to shared documents.
- Merge rules are a product choice as well as a technical one: for a bank balance you would not accept silent loss, and for a typing indicator you probably would.
Study: Eventual consistency and conflicts
Once you have played with it, learn the same idea from people who build these systems. Watch first for the picture, then read for the detail.
44 minCRDTs and the Quest for Distributed Consistency
InfoQ · 68K views
Then
16 minDistributed Systems 3.3: Causality and happens-beforeMartin Kleppmann: Distributed Systems Lectures · 57K views
19 minDistributed Systems 7.2: LinearizabilityMartin Kleppmann: Distributed Systems Lectures · 52K views
13 minLinearizable Databases | Systems Design Interview 0 to 1 with Ex-Google SWEJordan has no life · 18K views
46 minChubby - Eventual Consistency Is Too Hard... | Distributed Systems Deep Dives With Ex-Google SWEJordan has no life · 8K views