Structures · Sequences

Complexity Advanced

SequenceCrdt

Alice and Bob add trail stops to the same route without losing either one.

New waterfall and marsh cards fit before the same weir card in a tagged inspection route with a bridge and gate.

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

HikerBefore sharingAfter sharing
AliceBridge · FallsA:4 · Weir · North gateBridge · FallsA:4 · MarshB:4 · Weir · North gate
BobBridge · MarshB:4 · Weir · North gateBridge · FallsA:4 · MarshB:4 · Weir · North gate
CarolBridge · Weir · North gateBridge · FallsA:4 · MarshB:4 · Weir · North gate
Yellow: Falls (Alice) Blue: Marsh (Bob) Each highlight shows a trail stop new to that notebook.

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

0 notes waiting
  1. 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.

Carol's current route
  • 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.

Shared inputs Alice: move(1, 0) Bob: index 1 = weir; status = blocked

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

  1. 0: bridgeopen
  2. 1: weiropen
  3. 2: north-gateopen

Intended field target: weir at index 1

Bob

  1. 0: bridgeopen
  2. 1: weiropen
  3. 2: north-gateopen

Intended field target: weir at index 1

Carol

  1. 0: bridgeopen
  2. 1: weiropen
  3. 2: 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

  1. 0: bridgeopen
  2. 1: weiropen
  3. 2: north-gateopen

Map key: weir; current index: 1

Bob

  1. 0: bridgeopen
  2. 1: weiropen
  3. 2: north-gateopen

Map key: weir; current index: 1

Carol

  1. 0: bridgeopen
  2. 1: weiropen
  3. 2: 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.