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.
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 1 | Alice | +3 |
| SN 2 | Bob | -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:
- Assign a number. Stamp each accepted operation with the next sequence number.
- Broadcast the operation. Send the same numbered stream to every replica.
- 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 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.
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.