Structures · Maps

Complexity Expert

JsonOt

Keep concurrent changes to nested JSON paths and array positions.

A wide circle-marked edit and a short diamond-marked edit fit separate title and revision fields on a report form.

Three hikers edit one incident report

Each notebook holds the same incident report: a site name, a nested gauge reading, and an ordered crew list. Alice and Bob both add a crew member at the front while Carol raises the gauge stage.

The report starts with site Mill Race, gauge stage 24, and crew Ada and Ben. Alice inserts Cy and Bob inserts Dot at position 0, before either sees the other's insertion. Carol adds 1 to the nested number at .gauge.stage.

Alice immediately sees Cy at crew position 0, Bob sees Dot at the same position, and Carol sees the higher gauge stage. Each edit stays pending until the sequencer numbers it. Incoming operations are transformed against the concurrent work that their author had not seen.

A map winner can discard document intent

A SharedMap can store the whole report under one key, but then each edit replaces one complete value. If Alice writes a new crew array and Carol writes a new gauge object at the same time, the later map assignment can discard the earlier document change.

Splitting every field into a separate map key helps only until a field contains structure. The crew list has positions. Both Alice and Bob can honestly write insert at crew[0] against their local copy, but those two operations cannot remain at the same index after both are accepted.

JsonOt preserves operations, not only final field values. Each operation names a path and an action, such as list insert, object replace, list move, or number add.

The sequencer orders; each client transforms

JsonOt uses the same central sequence numbers as the other sequenced structures; in the story, the ranger station assigns them. The sequencer does not inspect JSON and does not rewrite an operation. It only assigns the shared order.

As in RegisterMap, each operation also carries a reference sequence number: the latest accepted sequence the author had seen. When an operation arrives, the client finds every operation in the gap between that reference and the new sequence number. It transforms the incoming path through that window before applying it.

Sequence Original operation Applied operation
SN 1 · Alice insert "Cy" at .crew[0] insert "Cy" at .crew[0]
SN 2 · Bob insert "Dot" at .crew[0] insert "Dot" at .crew[1]

Alice's earlier sequenced insertion shifts Bob's concurrent position. Every replica performs the same transform and reaches ["Cy", "Dot", "Ada", "Ben"]. The order comes from the sequencer; the valid array positions come from OT.

One operation travels while later edits wait

  1. Apply locally. The author sees the change in the optimistic document before the sequencer replies.
  2. Send one operation. The client sends the first pending operation with the current reference sequence number.
  3. Buffer later edits. If the author edits again, the kernel composes the new work into a buffer instead of sending a second operation with an unsafe context.
  4. Rebase on remote work. The kernel transforms both the incoming operation and its own pending work so the optimistic document does not jump backward.
  5. Release after acknowledgement. When the in-flight operation returns with a sequence number, the kernel sends the buffered work against the new confirmed version.

This single-operation-in-flight rule avoids the dOPT problem. The server remains a general sequencer; the JsonOt kernel owns the document-specific transformation rules.

Run the conflict

Turn off Auto-deliver to compare three optimistic JSON documents. Turn it back on to watch same-index list operations transform and converge.

Edit one incident report

Compare optimistic local documents, then watch the sequencer order and transform their operations.

Transform rule Rebase stale paths before applying them

Concurrent target

Alice and Bob both wrote against the original list.

.crew[0]

Bob's delivered path

The path changes after Alice's earlier insertion.

.crew[0] · waiting

Carol's crew view

The observer shows the shared result after delivery.

  1. 0: Ada
  2. 1: Ben

Gauge stage 24

Alice

Client state Synced at SN 0

{
  "crew": [
    "Ada",
    "Ben"
  ],
  "gauge": {
    "stage": 24,
    "trend": "steady"
  },
  "site": "Mill Race"
}

Bob

Client state Synced at SN 0

{
  "crew": [
    "Ada",
    "Ben"
  ],
  "gauge": {
    "stage": 24,
    "trend": "steady"
  },
  "site": "Mill Race"
}

Carol

Client state Synced at SN 0

{
  "crew": [
    "Ada",
    "Ben"
  ],
  "gauge": {
    "stage": 24,
    "trend": "steady"
  },
  "site": "Mill Race"
}

Run the worked example

Alice and Bob insert at the same list index while Carol changes a nested number. Delivery transforms Bob's stale index and keeps all three edits.

Enable JavaScript to run the document edits. The expected result remains in the lesson.

Ready: all clients share the baseline report.

Read the result

All three reports contain Cy and Dot in one deterministic crew order, and the nested gauge stage is 25. Bob's insertion path shifts when it is transformed past Alice's earlier sequenced insertion.

Every pair of edits needs a transform

JsonOt needs the ranger station to order operations, and it needs a correct transform rule for every pair of operation types: insert against insert, move against delete, and so on. Each new operation type adds rules to get right.

The next lesson, SequenceCrdt, handles shifting positions without a sequencer: each item gets a stable ID.

Sources and implementation

Sources: Watershed's json_ot_kernel and Watershed's json_ot.

Implementation: The demo calls json_ot.object_insert, json_ot_kernel.submit, json_ot_kernel.ack_local, json_ot_kernel.apply_remote, and json_ot_kernel.view through the Gleam toolkit. The displayed client records and shared view come from those kernel results.