Utilities · zero-dependency · MIT

larzcrdt

State that merges from any two replicas, always.

G-Counter, PN-Counter, LWW-Register and OR-Set - real conflict-free replicated data types. Every merge is commutative, associative and idempotent: merge in any order, merge twice, merge across a flaky network, the result always converges, with nothing silently lost. The same shape a wallet balance safe to update offline on two devices needs.

larzscript pkg install crdt
View on GitHub →Learn the stack

What you get

Try it

import "crdt" as crdt
let a = crdt.gcounter_new()
let b = crdt.gcounter_new()
crdt.gcounter_inc(a, "replica-a", 3)
crdt.gcounter_inc(b, "replica-b", 5)
let merged_ab = crdt.gcounter_merge(a, b)
let merged_ba = crdt.gcounter_merge(b, a)
print("merge order commutes:", crdt.gcounter_value(merged_ab) == crdt.gcounter_value(merged_ba))
print("value:", crdt.gcounter_value(merged_ab))

let common = crdt.orset_new()
crdt.orset_add(common, "apples", "t1")
let replica_b = crdt.orset_clone(common)
crdt.orset_remove(replica_b, "apples")
let replica_a = crdt.orset_clone(common)
crdt.orset_add(replica_a, "apples", "t2")
let final = crdt.orset_merge(replica_a, replica_b)
print("concurrent re-add beats a stale remove:", crdt.orset_contains(final, "apples"))
Output
merge order commutes: true
value: 8
concurrent re-add beats a stale remove: true

Why it's in the Larz Stack

larzcrdt is a real Larzscript package (larzscript pkg install crdt) - money-native, zero dependencies, the same language as the rest of the estate, not a separate Python runtime. It composes with the rest of the Larz Stack and the Larz framework.