Structures

Complexity Introductory

G-counter

Alice, Bob, and Carol count birds on three trails. Each hiker writes their own sightings in a notebook and leaves notes with their running count at checkpoints. The notes may arrive late, out of order, or more than once. By the end of the hike, all three notebooks must show the same total.

An open birdwatching notebook and binoculars beside a trail checkpoint box holding a folded message.

Three hikers, three notebooks

Alice takes the ridge trail, and Bob takes the river trail. Each notebook has one row for Alice, one for Bob, and one for Carol. A hiker adds one to their own row for each bird they see. The other rows hold the latest counts received from the other hikers.

Row Alice's notebook Bob's notebook Carol's notebook
Alice700
Bob030
Carol000
Birds seen730
Before the first notes arrive, each hiker knows only their own running count.

To share counts, the hikers leave notes at checkpoints. Alice does not send seven separate sightings. She leaves one note that says her row has reached 7. Bob leaves one note when his row reaches 3.

AliceRunning bird count7

BobRunning bird count3

Each notebook is a replica of the shared counter. The notes let the replicas catch up with each other.

Keep the largest note from each hiker

When a hiker finds a note, they compare its count with the row that the note names. If the note's count is larger, they copy it into that row. The three rows add up to the bird count.

This is a grow-only counter, or G-counter. A note carries a running count, not new sightings, so a hiker never adds a note to a row. If Bob's note for 3 arrives twice, his row stays at 3.

Three trail notebooks merge into one G-counter A B C TRAIL NOTEBOOK ALICE KEEP MAX 7 BOB KEEP MAX 3 CAROL KEEP MAX 0 BIRDS SEEN 10
Each hiker owns one row. Merge keeps the largest value in every row, then adds the rows.

Their checkpoint notes cross

On Carol's route, the first note at the north fork is from Alice: 7 birds. At the river bridge, she finds Bob's note for 3. Carol has not seen a bird herself, so her own row stays at 0.

Checkpoint and note Alice Bob Carol Total
Trailhead0000
North fork: Alice 77007
River bridge: Bob 373010
Old lookout: Alice 473010
Carol updates her notebook as she reaches each checkpoint.

At the old lookout, Carol finds an earlier note from Alice with a count of 4. Her notebook already contains 7, so she leaves that row alone: max(7, 4) = 7. Had she found 4 first, the row would have changed from 0 to 4, then from 4 to 7. Either path through the notes leads to the same notebook and the same total.

In the sandbox, record sightings for any hiker and control when notes arrive. The worked example follows the story: Alice records 7, Bob records 3, and they leave their notes at the same time. Until the notes arrive, the three notebooks disagree.

Record sightings on three hikes

Use the controls in each notebook to explore the counter. Each hiker leaves their running count at a known checkpoint. Turn off Auto-deliver to hold several notes before delivery.

Merge rule Keep the largest bird count left by each hiker.

New note In transit to a checkpoint

Shared note Being delivered to the other hikers

Alice

Connected to checkpoint network

0

Birds in this hiker's view

Checkpoint 0 notes
  1. No checkpoint note shared yet.

Bob

Connected to checkpoint network

0

Birds in this hiker's view

Carol

Connected to checkpoint network

0

Birds in this hiker's view

Count birds with Alice, Bob, or Carol, or leave Alice's and Bob's checkpoint notes together.

Run the worked example

Reset the sandbox and leave Alice's and Bob's notes together, or adjust how the notes travel.

Enable JavaScript to run the demo. The initial values and expected result remain available in the article.

Open the hikers' count tables

Each hiker's notebook has one row for Alice, Bob, and Carol. A row stores the largest count received from that hiker. Checkpoint notes can be delivered in any order.

Hiker Alice's view Bob's view Carol's view
Alice 000
Bob 000
Carol 000

A repeated or older note cannot lower a row. Each hiker compares the note with the count already in the table and keeps the larger value.

Every trail catches up

After delivery of both checkpoint notes, Alice, Bob, and Carol all read 10. Their views were temporarily inconsistent, but the G-counter kept both hikers' observations and made every replica agree.

This is eventual consistency. For each hiker, merge keeps the larger count. Arrival order does not change the answer, because the rule is commutative and associative. A repeated note changes nothing, because the rule is idempotent.

A total is not an increment

Start at 0. Alice adds 7 while Bob adds 3, before either sees the other's note.

The map selects one total. The G-counter preserves both increments and reaches 10.

NotebookLWW map of whole totalsG-counter components
Alice 0 0
Bob 0 0
Carol 0 0

Ready: both paths have the same starting input.

Inspect the decision evidence

LWW map of whole totals

The same bird-count key stores whole totals. Delivered sequence: 6.

G-counter components

Per-writer components: [[],[],[]]

Enable JavaScript to run the comparison. The starting state and expected lesson remain above.

Two limits

Every hiker needs a unique ID. If two hikers share one, they write in the same row, and keeping the larger count can discard one hiker's sightings.

The count also cannot go down. The merge rule can only keep or raise each row. If a hiker finds a counting mistake, lowering their row is unsafe, because an older note with a larger count could restore the mistake. The next lesson, PN-counter, records corrections without changing this merge rule.

Sources and implementation

Sources: Watershed's public G-counter kernel. The state-based grow-only counter is 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.