DEV Community

Anton Brilliantov
Anton Brilliantov

Posted on

A Notice Board For Every Keeper

A Notice Board For Every Keeper - the cover: Nora next to four steps: put a note, read the board, follow news, heal

A Notice Board For Every Keeper

Every keeper checks one board at the gate instead of phoning the others: read the whole board when you arrive, follow new notes while you work, and fix your memory from the board when they disagree. Built, barely used yet.


👋 Hi, I'm Anton - a software engineer. These days I work on a platform: contracts, generated code, data paths and the tooling around them. Running notes live on my GitHub: github.com/brilliant-almazov.

This part is about built code that is barely used yet: the state-watch package was lifted into the shared library from one service, and two services import it. The bug in "What went wrong" is real and fixed.

This is part 11 of the series on what a shared platform does for every service, told as a zoo. Maybe you already do it better, maybe you'd do it another way - this is how it is going, and where it stops.


In real life

Morning at the zoo. Nora wants to know one thing: "Who fed Miso?" Biscuit: "Ask Pickle!" Pickle: "Ask Nora!" Three keepers, three phones, and nobody knows - because the answer lives in three heads and none of them is the right one.

Pip puts a notice board at the gate. "Write it here. Everyone reads it." Fed Miso? Pin a note. Changed the feeding time? Pin a note.

A four-panel comic: Nora asks who fed Miso, Biscuit says ask Pickle, Pickle says ask Nora; Pip next to a notice board says write it here, everyone reads it; Biscuit, late, reads the whole board, then the news; Miso tore a note and Pickle fixes memory from the board

The board comes with two habits. Biscuit arrives late: he reads the whole board first, then follows the new notes as they go up - he does not start from whatever he half-remembers from yesterday. And when Miso tears a note down by accident, Pickle compares what she remembers with the board and fixes her memory from the board, not the other way round.

Replicas phoning each other

A service runs as several replicas, and some state has to be the same in all of them: which jobs are on, which source is paused, which version of a rule is current. The temptations are familiar. Each replica asks a database on every request, which is slow. Or each replica keeps its own copy and learns about changes when someone remembers to tell it, which is the three keepers on the phone.

The library's state-watch package is the board: observed state, an in-memory projection in every replica, and reconciliation when they drift. Its first real version lived in one service - the one that keeps metrics - and the library lifted it so others would not write it again.

Three tiles with faces: a publisher, happy; the watched store, proud; every replica, happy; labels read put or delete, and snapshot plus deltas; pills read built, and barely used yet

Put, read, follow, heal

In the shape of the code - names shortened, a form:

pub.Put(ctx, key, desired)   // pin a note on the board
pub.Delete(ctx, key)         // take a note down
w.Sync(ctx)                  // at start: read the whole board into memory
w.Run(ctx)                   // then follow every new note
rec.Reconcile(ctx)           // board vs memory: prune what is gone, upsert what changed
Enter fullscreen mode Exit fullscreen mode

A dark card with the state-watch form; the last line - the subscription lives until Stop, not until the start context ends - is highlighted; pills read built, and barely used yet

The board itself is a watched key-value store; the first backend is the NATS key-value store. A publisher writes and deletes keys. Each replica's watcher reads a snapshot of all keys on start - Biscuit reading the whole board - and then follows the deltas. The reconciler is the self-healing part: it compares the projection in memory with the store and fixes memory by pruning what is gone and upserting what changed.

Step Keeper's habit What it protects against
put / delete pin a note, take it down state living in one head
sync at start read the whole board first starting from a stale memory
run follow new notes missing a change while working
reconcile fix memory from the board a missed note, a torn note

An animation: the publisher puts a note, feed at noon; it lands on the watched store; replica 1 follows it; replica 2 reads it too

What goes on a board

A board is for short notes that every keeper must agree on, not for the zoo's archive. As a form - the keys and values are placeholders:

key:   <area>/<thing>              e.g. jobs/<name>, sources/<name>
value: { "enabled": true, "revision": <n> }
Enter fullscreen mode Exit fullscreen mode

The rule of thumb I use for what belongs there: a value that every replica must hold the same, that changes rarely compared with how often it is read, and that is small enough to hold in memory whole. A list of switches fits. A history of every change does not - that belongs in a table, and the board holds only where the table is now.

The other rule is the direction. The board is the truth and memory is a projection of it. A replica never writes its memory back to the board to "fix" it; it only reads. When the two disagree, the board wins, which is exactly what the reconciler enforces.

Who reads the board

Bars: steps - snapshot, deltas, reconcile - 3; services importing it 2; service it was lifted from 1; Nora says two keepers read it so far; pills read built, and barely used yet

Columns: files about 40, commits since October 6, services 2; Pickle, bored, says built, waiting for readers; a pill reads counted, rounded

Two services import the package. That is the honest size: a board with two keepers reading it, and a zoo that still has its phones.

What went wrong

The board had one real bug, and it is the most instructive part of this story. Before a fix in October, the subscription that follows new notes was tied to the context of the start - and that context ends when start is over. So a replica read the whole board, began to follow new notes, and then quietly stopped. Nothing failed. The memory simply stopped changing.

That is Biscuit reading the board when he arrives, then walking away from the gate for the rest of the day. The fix in the library's changelog is one line of meaning: the subscription lives until Stop, not until the start context ends. The reconciler would eventually have repaired the memory, but a self-healing loop that hides a stopped subscription is not healing, it is covering.

Before and after: the first version - start context ended the subscription; stale, memory stopped following - and the fix - until Stop, the subscription lives on; healing, reconcile catches drift; a pill reads fixed

The lesson I keep: when something has to run for the life of a process, tie it to the life of the process - not to the call that started it. A context is a promise about how long something may run, and the start context promises very little.

Service tiles with faces: the publisher, the watched store and two replicas are happy; a late replica happy; phone calls bored; the reconciler proud; other services bored; pills read built, and barely used yet

What I do not claim

  • Not that many services use the board. Two do.
  • Not that the reconciler replaces a working subscription. It catches drift.
  • Not a measured delay between a put and every replica seeing it.
  • Not that every backend is supported. The first one is the NATS key-value store.
  • Not the exact API. The Go above is a form.
  • Not that the rule of thumb for what goes on a board is the library's. It is mine.

What of this is method

  • One board, many readers. Desired state in a watched store, not in heads.
  • Read everything, then follow. A snapshot at start, deltas after.
  • Tie a lifetime to the process, not the start. A subscription ends at Stop.

Zoo keeping, built in - Part 11. Next: Moving Old Animals Out Without Closing The Zoo.

If your replicas share state, do they read it from a board or ask each other? If you watch a key-value store, what happens to a replica that missed a delta? If you had a subscription stop quietly, how did you notice? Which keeper in your zoo is still on the phone?

Top comments (0)