Structures

Complexity Intermediate

SharedCounter

Alice, Bob, and Carol still need to combine new sightings with corrections. This time, they send each change to a ranger who keeps one numbered ledger.

Envelopes, a mechanical stamp, an ordered paper log, and a stack of outgoing copies on a station desk.

The ranger numbers every change

The hikers stop exchanging running counts. Each hiker now sends a signed change, such as +3 or -1, to the ranger station. The ranger writes each change on the next numbered line of a ledger, then sends a copy to all three hikers.

That ranger is a sequencer: a service that gives accepted operations one shared order.

The group starts from the agreed count of 10. Alice counts 3 new birds, and Bob finds one duplicate sighting. They send +3 and -1 before either sees the other's change. The ranger's ledger gets two lines:

Sequence Note from Signed change
SN 1Alice+3
SN 2Bob-1

Each hiker shows their own change at once, then waits for the numbered copies:

Moment Alice Bob Carol
Agreed count101010
Notes in transit13910
SN 1 and SN 2 applied121212

A SharedCounter stores one integer and sends signed operations through this sequenced path. The local update is optimistic. The numbered broadcast confirms it.

A ranger numbers signed bird-count changes before broadcasting them A B C RANGER SEQUENCE LOG SN 1 ALICE +3 SN 2 BOB -1 BIRDS SEEN 12 +3 -1 SN 2 -1
The ranger stamps each signed change with the next sequence number, then sends the numbered operation to every hiker.

One number means one application

The trail can still deliver a numbered copy twice. The sequence number gives each change a stable identity: a hiker applies a new number and ignores a number they have already applied.

The numbers also expose gaps. Carol misses SN 1 but receives SN 2. She holds SN 2, asks the ranger to resend SN 1, then applies Alice's +3 and Bob's -1 in order. That works only if the ranger or the runtime keeps the numbered changes. A production runtime can send a current snapshot instead.

Send signed changes through the ranger

The ranger receives every note before another hiker does. The ranger stamps one sequence number, then broadcasts numbered copies.

Application rule Apply each sequence number once, in sequencer order.

Unnumbered note In transit to the ranger

Stamped copy Being delivered to every hiker

Alice

Agreed count received

10

Birds in this hiker's view

Bob

Agreed count received

10

Birds in this hiker's view

Carol

Agreed count received

10

Birds in this hiker's view

Ranger sequencer SN 0 stamp · broadcast
  1. No signed change sequenced yet.

Enable JavaScript to run the demo. The numbered example and final result remain available in the article.

Send a signed change, or race Alice's count against Bob's correction.

Open the sequence-number rule

A network can repeat a stamped copy. Each hiker records the sequence numbers they have applied and ignores repeated copies.

        next SN → apply remote delta or acknowledge own edit
        seen SN → ignore duplicate
        missing SN → hold later operations and request history

The single-browser mode keeps its log in memory. Three-device mode uses durable storage for up to 1,000 numbered operations. Rejoining replays that log from the agreed count of 10. Auto-deliver controls only local delivery animation, not the real server's sequencing or storage.

Same total, different safety mechanism

All three counters reach 12, but they share different information. The G-counter shares each hiker's running count. The PN-counter shares running counts on two pages. The SharedCounter sends signed changes with sequence numbers.

Signed addition gives the same final sum in either order. The sequencer still matters because a raw +3 must be distinguished from a second delivery of the same +3. Other sequenced data structures also use the shared order when operation order changes the result.

The ranger station must be reachable

This design depends on the sequencer. If that service is unavailable, replicas can keep optimistic local work, but they cannot confirm a new shared order until sequencing resumes.

The next lesson, GSet, starts a new problem: the hikers keep a shared list of wildlife cameras, again without the ranger.

Sources and implementation

Sources: Watershed's public SharedCounter API and counter kernel.

Implementation: The single-browser mode calls the API through the Gleam toolkit and connects three documents through Sluice, which plays the ranger station. It delivers every numbered copy. The three-device room calls the same counter kernel; each browser holds later operations and requests missing lines from the stored log. How multiplayer rooms work explains the shared connection service, sequencing, storage, and recovery.