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.
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 1 | Alice | +3 |
| SN 2 | Bob | -1 |
Each hiker shows their own change at once, then waits for the numbered copies:
| Moment | Alice | Bob | Carol |
|---|---|---|---|
| Agreed count | 10 | 10 | 10 |
| Notes in transit | 13 | 9 | 10 |
| SN 1 and SN 2 applied | 12 | 12 | 12 |
A SharedCounter stores one integer and sends signed operations through this sequenced path. The local update is optimistic. The numbered broadcast confirms it.
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.
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.