---
title: "Two replicas without a link"
url: "https://www.mikefluff.com/en/experiments/js-ch17-merge-replicas"
locale: "en"
author: "Mikhail Savchenko (Mike Fluff)"
updated: "2026-11-18"
description: "Conflict-free replicated data types (CRDT)"
book: "https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems"
alternates:
  - locale: "en"
    url: "https://www.mikefluff.com/en/experiments/js-ch17-merge-replicas"
    markdown: "https://www.mikefluff.com/en/experiments/js-ch17-merge-replicas.md"
  - locale: "ru"
    url: "https://www.mikefluff.com/ru/experiments/js-ch17-merge-replicas"
    markdown: "https://www.mikefluff.com/ru/experiments/js-ch17-merge-replicas.md"
  - locale: "pt"
    url: "https://www.mikefluff.com/pt/experiments/js-ch17-merge-replicas"
    markdown: "https://www.mikefluff.com/pt/experiments/js-ch17-merge-replicas.md"
  - locale: "es"
    url: "https://www.mikefluff.com/es/experiments/js-ch17-merge-replicas"
    markdown: "https://www.mikefluff.com/es/experiments/js-ch17-merge-replicas.md"
---

# Two replicas without a link

*Conflict-free replicated data types (CRDT)*

## What is a CRDT?

A CRDT, a conflict-free replicated data type, is a data structure built so that replicas changed independently can always be merged without coordination, and the merge does not depend on the order or the number of times replicas exchange state. Replicas that have seen the same updates end up in the same state. A G-Counter keeps a separate slot per replica and sums them; an OR-Set tags each addition so that a removal deletes only the tags it has seen. The idea was formalised by Marc Shapiro and colleagues in 2011. A CRDT guarantees convergence, not that the merged state satisfies the application’s rules.

When a system chooses availability during a network partition, its replicas keep working apart and must be merged when the link comes back. Amazon’s Dynamo paper of 2007 used the shopping cart as its main example and merged diverging carts by union, so an added item is never lost and a removed one can return. Many stores simply let the last write win. CRDTs move the merge rule into the data structure itself.

Pick a tab and edit the two replicas, a phone and a laptop, which cannot see each other. Then press “Merge A and B”, “Merge again” and “Merge in the other order”, or play the book’s scenario. The counter shows last writer wins next to a G-Counter. The cart shows a set union next to an OR-Set and lets you remove an item on one replica while putting it back on the other. The seats tab has one free seat, a booking button on each replica, a link you can cut and a choice between answering at once and agreeing first.

In the book, in Node 24.16.0, a counter of 5 with +1 on each replica merged to 6 under last writer wins and to 7 as a G-Counter, in either order and on a repeated merge. The cart {book, ball}, with the ball removed on one replica and a pen added on the other, came out of the union as book, ball and pen, and out of the OR-Set as book and pen. When the ball was removed on one replica and added again on the other, the add won. One free seat booked on both replicas merged into 2 bookings, and the rule “no more bookings than seats” was broken.

The chapter’s point is in the last tab. A CRDT promises that replicas converge, not that the result makes sense. Rules such as not selling more seats than exist or not letting an account go negative require the replicas to agree before answering the user, and no data structure removes that requirement. The programmer decides where replicas must coordinate and how much latency that is worth.

_The page has an interactive experiment that runs in a browser: https://www.mikefluff.com/en/experiments/js-ch17-merge-replicas_

## Sources

- [Conflict-free replicated data type (Wikipedia)](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type)
- [DeCandia et al., Dynamo: Amazon’s Highly Available Key-value Store (SOSP 2007)](https://www.allthingsdistributed.com/files/amazon-dynamo-sosp2007.pdf)

## From the book

Beside it in the book:

> The last mode of the experiment shows this. There is one free seat in the hall, and two people book it on two copies that cannot see each other at that moment. The bookings counter merges into two without a single conflict, because CRDTs have no conflicts by construction. Only there was one seat. In 2014 Peter Bailis and his co-authors worked out which data integrity rules can be preserved without coordination between copies and which cannot. A rule like «do not sell more seats than exist» or «do not let the account go negative» requires the copies to agree before answering the user. No data structure can remove that need.

- Read the chapter: [Chapter 17. The Limits of Consistency](https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch17) (opening free, rest in the full book; Markdown: https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch17.md)

- Book: [JavaScript: From Closures to Autonomous Systems](https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems)
