Structures

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 station log.

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.

The ranger opens a numbered log

The hikers stop exchanging cumulative notebook pages. Each hiker now sends a signed change to the ranger station. The ranger writes the change on the next numbered line and sends a copy to all three hikers.

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

For Alice and Bob's race, the ranger's ledger has two lines:

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

The ranger broadcasts each line after he writes it. Carol misses SN 1, but then receives SN 2. Her notebook says that SN 1 must come next, so she holds SN 2 and asks the ranger to resend the missing ledger line. She applies Alice's +3 from SN 1, then Bob's -1 from SN 2.

The sequencer has three jobs in this story:

  1. Assign a number. Stamp each accepted operation with the next sequence number.
  2. Broadcast the operation. Send the same numbered stream to every replica.
  3. Support recovery. A runtime can store operations or summaries for replicas that reconnect later.

Carol can request SN 1 only if the ranger or the runtime retains the numbered operations. A production runtime can send a current snapshot instead of replaying old ledger lines. The demo shows assignment and broadcast, and delivers every numbered copy. It does not simulate Carol's missed message or recovery request.

One number means one application

A sequence number does not force the network to deliver one copy. A the network can still deliver a numbered copy twice. The number gives each replica a stable operation identity: apply a new number, and ignore a number that has already been applied.

The numbers also show relative order. If SN 2 appears before SN 1, the replica can detect a gap instead of losing an operation without warning. The sequence number exposes the gap. The replica must ask the runtime to replay the missing operation or send a snapshot.

new SN → apply signed delta
seen SN → ignore duplicate
missing SN → wait or recover the gap

The ranger sequences two notes

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

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.

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

Applied through baseline

10

Birds in this hiker's view

Bob

Applied through baseline

10

Birds in this hiker's view

Carol

Applied through baseline

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. A replica remembers the sequence numbers it has applied, so the repeated copy does not change the total again.

new SN → apply signed delta
seen SN → ignore duplicate

This demo keeps the log in memory. Durable storage and reconnect recovery are separate runtime responsibilities.

The number moved; the safety mechanism changed

All three counters have a value of 12, but they do not exchange the same information. The G-counter sends cumulative positive components. The PN-counter sends cumulative positive and negative components. The SharedCounter sends signed operations 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.

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.

Sources and implementation

This lesson calls Watershed's public SharedCounter API and runs three connected documents through Sluice.

The signed operation, optimistic state, acknowledgement, rollback, and integer summary are defined by Watershed's counter kernel.