Structures

Complexity Intermediate

PN-counter

Alice, Bob, and Carol have agreed on 10 birds. Then Alice finds 3 more while Bob finds a duplicate sighting. Their G-counter notebooks cannot erase a count, so each hiker adds a second page for corrections.

A two-page notebook preserves sightings and corrections beside two matching bird-report slips.

Bob counted one bird twice

Bob realizes that one of his sightings was a duplicate. He cannot lower his row: as the G-counter lesson showed, an older note with the larger count could arrive later and restore the mistake.

A second page for corrections

Each notebook now has two pages, and each page is a G-counter. The sightings page, P, records new birds. The corrections page, N, records birds to subtract. Each page has one row for each hiker, and a hiker writes only in their own rows.

Notebook page Row Starting value
Sightings (P)Agreed count10
Sightings (P)Alice0
Sightings (P)Bob0
Sightings (P)Carol0
Corrections (N)Alice0
Corrections (N)Bob0
Corrections (N)Carol0
Every hiker starts with the same 10 sightings and no corrections. The visible bird count is the sightings total minus the corrections total.

To keep the numbers small, each notebook starts with one row that holds the 10 birds from the G-counter lesson. No hiker changes it.

This is a PN-counter. Both pages still only grow, and each row merges by maximum. A new bird raises P; a correction raises N.

Alice writes 3 in her row on the sightings page. Bob writes 1 in his row on the corrections page. Each note lists its page, row, and cumulative value. The other hikers compare that value with the same row in their notebooks instead of applying +3 or -1 again.

Alice Sightings page · Alice row 3

Bob Corrections page · Bob row 1

Concurrent sightings and corrections merge into one PN-counter CORRECTED FIELD LOG SIGHTINGS 10 + 3 13 CORRECTIONS 1 BIRDS SEEN 12 +3 -1
Alice's new sightings and Bob's correction remain separate. Every replica subtracts the correction total from the sightings total.

Alice and Bob fill in different pages

Neither hiker has received the other's note. Alice's notebook has 13 sightings and no corrections, so she reads 13. Bob's has 10 sightings and 1 correction, so he reads 9. Carol has not found either note, so she still reads 10.

Notebook entry Alice Bob Carol
Agreed sightings101010
Alice sightings row300
Bob corrections row010
Birds seen (P - N)13910

The notes can arrive in either order. Each row keeps its largest value, so every notebook ends with 3 in Alice's sightings row and 1 in Bob's corrections row. A repeated note changes nothing.

Correct the shared bird count

The hikers agree on 10 birds. As soon as a hiker records a new sighting or correction, they leave a note at the checkpoint.

Merge rule Keep the largest sightings and corrections from each hiker.

New note In transit to a checkpoint

Shared note Being delivered to the other hikers

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

Checkpoint 0 notes
  1. No note shared yet.

Enable JavaScript to run the demo. The starting count and expected result remain available in the article.

Run the correction race, or record new birds and corrections for any hiker.

Open the hikers' two notebook tables

Each hiker's notebook has a sightings table for the positive G-counter and a corrections table for the negative G-counter. Both tables only grow and merge by per-hiker maximum.

birds = P - N
Sightings from Alice's view Bob's view Carol's view
Agreed starting count 10 10 10
Alice 0 0 0
Bob 0 0 0
Carol 0 0 0
Corrections from Alice's view Bob's view Carol's view
Alice 0 0 0
Bob 0 0 0
Carol 0 0 0

The correction survives the merge

After both notes arrive, P totals 13 and N totals 1. Alice, Bob, and Carol all read P - N = 13 - 1 = 12.

The count can go below zero

A PN-counter can also produce a negative value. That is correct counter behavior, but it may be invalid for the thing being counted. An application that must never show fewer than zero birds needs another rule to prevent or coordinate those updates.

The next lesson, SharedCounter, handles sightings and corrections a different way: a ranger station numbers every change.

Sources and implementation

Sources: Watershed's public PN-counter kernel implements the positive-negative counter described in A comprehensive study of Convergent and Commutative Replicated Data Types.

Implementation: The demo calls the Watershed kernel through the site's Gleam toolkit and passes the hikers' notes through Sluice. Sluice numbers the notes, but the PN-counter's merge does not use those numbers.