Structures

Complexity Intermediate

LWWMap

Alice and Bob still have copies of the trail report table. They change the gate-status line while they are apart. Can Carol settle their disagreement without asking the ranger to number the notes?

Three setting records each have their own clock and writer tag; an incoming report lies beside the board.

A time beside each line

For SharedMap, the hikers used the ranger's ledger number to choose the gate answer. An LWWMap (LWW stands for "last writer wins") is a page of LWWRegisters: each line keeps the answer with the greatest timestamp, on its own. "Last" means the greatest timestamp, not the last note delivered.

Before they exchange gate notes, each hiker has a different local view. Carol wrote the bridge report earlier, and all three hikers copied it before they separated. Each line records an answer, a time, and a writer:

Notebookgate-statusbridge-status
Alice Trail open
· Alice
Inspection due
· Carol
Bob Trail closed
· Bob
Inspection due
· Carol
Carol Report pending
· Carol
Inspection due
· Carol
Alice and Bob changed only the gate line. Carol still has the earlier gate report; the bridge line is the same in all three notebooks. Scroll sideways to read every notebook.

Suppose the ranger had numbered Bob's note first and Alice's second. SharedMap would record Alice's answer, because it was numbered last. LWWMap records Bob's, because 11:42 a.m. is later than 11:40 a.m., whatever order the notes arrive in. If the ranger's ledger numbers were the timestamps, both rules would agree.

LWWMap needs no ranger: the trail relay only carries the notes, and the hikers can exchange them directly or after a delay.

A hiker can also cross out a line, and the crossing-out carries a time too. If a crossing-out and an answer have the same time, the crossing-out wins. If two answers have the same time, the greater writer ID wins, as in LWWRegister.

Three hikers' notebooks

Compare times on gate-status notes

Alice and Bob change the gate-status line. Carol uses the time on each note, not the order of delivery, to choose one answer.

How they agree Use the latest time; if tied, keep the removal, then compare writer IDs.

Alice

No notes yet

No entries

Note to share gate-status: Trail open time on writer A's note

Bob

No notes yet

No entries

Note to share gate-status: Trail closed time on writer B's note

Carol

No notes yet

No entries

Note to share No note in this race Carol observes delivery

Trail relay Compare the times beside each line 0 operations shared

What the notes show No race delivered yet.

  1. No operations shared yet.

Enable JavaScript to run the demo. The race and expected result remain in the lesson.

Run the race, or let any hiker change their own notebook.

Two limits

"Last" depends on the clock. If Bob's watch is fast, his earlier gate note can have a later time than Alice's newer note. All three hikers will record Bob's answer, but it will not be the last answer written in real time. Advancing a client's clock past times it has seen cannot fix a fast clock on another client. Use LWWMap only when the clock policy and the risk of losing a later human edit are acceptable.

Crossed-out lines pile up. Each notebook must keep a crossing-out until it is safe to forget it, or an old note could restore the answer.

The next lesson, OR-map, keeps new work that a removal did not see.

Sources and implementation

Sources: Watershed's LWWMap kernel.

Implementation: The demo calls the Watershed kernel through the site's Gleam toolkit. The trail relay carries notes without ordering them. Each client's runtime supplies wall-clock times and advances each line's clock past the times it has observed.