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

Intermediateabout 10 to 15 min7 experiments
Experiments
New here? A few short steps show what happens, one at a time.

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

  1. NetworkThe network between the two copies fails. The name is “Ada”.
  2. Copy ACopy A sets the name to “Ada L.” at time 100.
  3. Copy BCopy B sets it to “Ada Lovelace” at time 105 by the real clock, which its own clock reads as 105.
  4. NetworkThe network heals and the copies compare notes.

The same story, every way of merging

MergeResultLost silently
Ada Lovelacenothing
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.

Check yourself

1 of 4. What can go wrong with last write wins?

Go deeper