Build state-based CRDTs: a PN-counter, an add-wins OR-Set without tombstones and a last-writer-wins map

hardAbout 60 minutesUpdated CodingProjavascriptcrdtdistributed-systemsoffline-firstcollaborative-editingsync
Not solved

Share this question

Problem

Offline-first apps, such as notes and to-do apps, let several devices change the same data without a server in the middle of every edit, then sync later. CRDTs (conflict-free replicated data types) make that safe: every replica keeps a state, states are sent around in any order, any number of times, and merge always brings replicas to the same value. Build three state-based CRDTs for a shared shopping list.

const phone = new ORSet("phone");
const laptop = new ORSet("laptop");
phone.add("milk");
laptop.merge(phone.state());
phone.remove("milk"); // removes the milk it has seen
laptop.add("milk"); // concurrently adds milk again
phone.merge(laptop.state());
phone.has("milk"); // true: add wins

Ref. UR-1449 · © UIRound · uiround.com/questions/build-state-based-crdts-a-pn-counter-an-add-wins-or-set-without-tombstones-and-a-last-writer-wins-map

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment