SequenceCrdt
Alice and Bob add trail stops to the same route without losing either one.
Two hikers edit the route
Alice and Bob revise their inspection route, an ordered list of trail stops, before either receives the other's edit.
The route begins Bridge, Weir, North gate. Alice adds Falls before Weir in her notebook. Bob adds Marsh before Weir in his. Carol has not received either insertion note, so her route is unchanged.
A note cannot say “insert at position 1.” Once Falls is in Alice's notebook, Weir moves from position 1 to position 2, so a position names a different stop in each notebook. JsonOt solved this with the ranger: it orders the edits and rewrites positions. SequenceCrdt needs no ranger. It names each stop by an ID that never changes.
Each stop gets an ID
| Hiker | Before sharing | After sharing |
|---|---|---|
| Alice | Bridge · FallsA:4 · Weir · North gate | Bridge · FallsA:4 · MarshB:4 · Weir · North gate |
| Bob | Bridge · MarshB:4 · Weir · North gate | Bridge · FallsA:4 · MarshB:4 · Weir · North gate |
| Carol | Bridge · Weir · North gate | Bridge · FallsA:4 · MarshB:4 · Weir · North gate |
A SequenceCrdt
gives every trail stop its own ID. Each ID is a
dot, as in the
observed-remove set,
and a new ID uses a counter one higher than any the hiker has seen:
the starting stops are A:1 to A:3, so Alice's Falls gets
A:4 and Bob's Marsh gets
B:4. Notes refer to stops by ID,
so a note still means the same stop when others are added nearby.
When two stops land in the same gap, every notebook orders them by ID.
Run the conflict
Alice and Bob act before either receives the other's note. Carol receives their notes too and checks that all three copies agree.
Edit the inspection route
Each hiker can add trail stops to their notebook. Turn off Auto-deliver to see their routes before insertion notes arrive.
Merge rule Give each trail stop its own ID, so adding or moving other stops does not change which one a note means.
Alice
Route in this notebook Bridge · Weir · North gate
Latest insertion No trail stop inserted yet. Each trail stop gets a unique ID.
Trail relay
Insertion notes travel to all three hikers
- No insertion notes yet.
Bob
Route in this notebook Bridge · Weir · North gate
Latest insertion No trail stop inserted yet. Each trail stop gets a unique ID.
Carol
Route in this notebook Bridge · Weir · North gate
Latest insertion No trail stop inserted yet. Each trail stop gets a unique ID.
- Bridge
- Weir
- North gate
Run the worked example
Queue Alice's Falls and Bob's Marsh together, or insert trail stops from any notebook.
Enable JavaScript to run the race. The expected result remains in the lesson.
Ready: Bridge · Weir · North gate
Moving a stop changes its position, not its ID
Both paths start with the same three trail stops, all marked open. Alice moves Weir from index 1 to index 0. From the original view, Bob marks Weir blocked. Carol has received neither change.
Without IDs: edit by position
This path applies status[1] = blocked to the stop at index
1 when the edit arrives. After the move, that stop is Bridge, not Weir.
This path intentionally uses no merge kernel.
Alice
0: bridgeopen1: weiropen2: north-gateopen
Intended field target: weir at index 1
Bob
0: bridgeopen1: weiropen2: north-gateopen
Intended field target: weir at index 1
Carol
0: bridgeopen1: weiropen2: north-gateopen
Intended field target: weir at index 1
With IDs: SequenceCrdt plus an LWWMap
The SequenceCrdt stores each stop's ID. An LWWMap stores each stop's
status under that ID. Bob sends p2p_set("weir", "blocked"),
not an edit at index 1. The move changes order; the map keeps the
intended stop.
Alice
0: bridgeopen1: weiropen2: north-gateopen
Map key: weir; current index: 1
Bob
0: bridgeopen1: weiropen2: north-gateopen
Map key: weir; current index: 1
Carol
0: bridgeopen1: weiropen2: north-gateopen
Map key: weir; current index: 1
Ready: all three copies share the same IDs, order, and open status.
Enable JavaScript to stage and deliver the changes. The native composition keeps Weir blocked in either delivery order.
Inspect native identities and operations
sequence_kernel.p2p_move preserves the sequence item identity.
Application IDs link the sequence to map fields. In this pinned kernel,
p2p_replace is DELETE + INSERT: it does not preserve item
identity and is not an in-place field edit.
Run the comparison to inspect native summaries and authored operations.
Read the result
Both new trail stops target the gap before Weir. Falls has ID A:4, and Marsh has ID B:4. Every notebook compares those IDs the same way, placing Falls before Marsh.
Concurrent stops follow ID order
When Alice and Bob insert into the same gap, every notebook agrees on the order, but the order comes from the IDs, not from what the hikers meant: Falls comes first only because A sorts before B. Every stop also carries its ID and an ordering reference.
The next lesson, TextCrdt, applies the same idea to every character in a field note.
Sources and implementation
Sources:
Watershed's sequence_kernel.
Implementation: The demo calls sequence_kernel.p2p_insert, sequence_kernel.apply_remote, and sequence_kernel.values
through the Gleam toolkit. The displayed client records and shared
view come from those kernel results.