Mergeable Replicated Data Types
README.md

mrdt: mergeable replicated data types #

Replicated data types with three-way merges. Two concurrent versions of a value are reconciled against their lowest common ancestor (LCA), the way Git merges branches: merge ~ancestor a b. The values themselves hold no replication metadata, meaning no vector clocks, dots or tombstones, because the ancestor taken from the version history gives the merge its context.

The package sits beside crdt. A CRDT joins two states with a semilattice merge and pays for that with metadata inside each value. An MRDT keeps its values plain and pays with a version DAG kept outside them. That DAG also makes possible the garbage collection described below, which CRDTs do not have.

Installation #

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam install mrdt

Types #

Each type follows the relational merge specification that Kaki et al. give for it (OOPSLA 2019).

  • Counter merges as merge ~ancestor:l a b = l + (a − l) + (b − l), and add, sub and mult all converge.
  • Make_pair (A) (B) merges each component with its own merge.
  • Make_set (E) merges membership as (l ∩ a ∩ b) ∪ (a − l) ∪ (b − l). A new element survives, and a removal of an element the other side kept wins.
  • Make_queue (E) is an at-least-once FIFO queue. An element popped in either branch never comes back, pushes from both branches survive, the relative order is kept, and E.compare orders concurrent pushes.
  • Make_map (K) (V) merges its keys like a set, so a removal wins over a concurrent update. The values of the keys that survive merge with V.merge against the ancestor's value, or against V.zero where the ancestor had no such key.

Replicas and garbage collection #

Make_replica (S) turns a mergeable type into a distributed object with a fixed number of replicas, known in advance. Each replica keeps a version DAG. update commits a new version and pull syncs with a peer, which ends in nothing to do, a fast-forward or an LCA merge; as in Git, a criss-cross history is resolved by merging the LCAs recursively.

Knowing the number of replicas lets the history be compacted. A version vector maps each replica to the number of its updates that a version includes, and version u is an ancestor of version v when u's vector is at most v's in every entry (Parker et al., 1983). Every version carries its version vector, and on every sync the replicas exchange acknowledgement vectors, which record what each peer is known to hold. Once all replicas have acknowledged a version, it can never again be an LCA, so everything below that acknowledged frontier collapses into one snapshot. Soundarapandian et al. made this observation (PLDI 2022): a commit merged into the public branch of every replica frees its ancestors. Memory in the steady state then grows with the work done since the frontier and not with the whole history, which a long-lived collaborative document needs. The price is liveness: a replica that cannot be reached holds the frontier back until it syncs again.

Status #

The tests serve as the specification. test/test_mrdt.ml copies the worked examples of the paper as they are printed (counter Fig. 2, queue Figs. 5, 6 and 8, pair Sec. 4.1, map Rel-Merge), checks the replica and garbage collection contract, and feeds hostile inputs: merges against a non-ancestor, mismatched replica groups, overflow and deep criss-cross histories. fuzz/fuzz_mrdt.ml runs random schedules over counters, queues and maps, with compaction, and checks the merge laws and that replicas end on exactly the same value.

bench/bench.ml runs large merge and replica scenarios and reports throughput and words per operation; MEMTRACE=<file> dune exec bench/bench.exe profiles it. The merges keep structural sharing and never build the relations out in full, so the steady state allocates little.

The planned work, in order:

  1. Text. The line-level three-way merge of merge3 will become a mergeable type, and the typed merges this package shares with the Merge module of irmin will live in one place.
  2. A sync protocol. A wire state machine with no I/O of its own will run pull over a transport and send patches instead of reading the whole peer replica, with an Eio adapter.
  3. JavaScript. A js_of_ocaml and wasm_of_ocaml build will be published on npm with TypeScript definitions, together with a small collaborative application to show it.
  • Gowtham Kaki, Swarn Priya, KC Sivaramakrishnan, Suresh Jagannathan, "Mergeable Replicated Data Types", OOPSLA 2019, gives the relational merge specifications this package implements. The paper's OCaml and Irmin artifact is in gowthamk/ocaml-irmin.
  • Vimala Soundarapandian, Adharsh Kamath, Kartik Nagar, KC Sivaramakrishnan, "Certified Mergeable Replicated Data Types", PLDI 2022 (prismlab/peepul), verifies MRDTs in F*. The garbage collection condition, a version acknowledged by all replicas, comes from it.
  • irmin runs a block-level, two-phase three-way merge over a content-addressed store. This package is the typed layer, with no store.
  • crdt has state-based CRDTs with two-way semilattice merges, for systems that share no version history.

License #

ISC.