Only One Keeper Opens The Gate - the cover: Nora alone opens the gate with one golden key tagged migrations while Biscuit, Miso, Pickle and Pip wait in a calm queue; an inset labelled before shows everyone jamming keys into the gate at once

Only One Keeper Opens The Gate

Every morning every keeper used to open the gate with their own key, and they pushed. Now the plan names one keeper, the gate opens first, and only then does feeding start. Tested in the shared library.

👋 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 tested behaviour of the shared library: who runs database migrations, and in what order at start. The failure in "What went wrong" is real and closed; how many services already moved to the new form is not counted here.

This is part 13 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. Biscuit: "My key first!" Miso: "No, mine!" Pickle, squeezed against the turnstile: "Ouch." Every keeper has a key to the big gate, so every keeper tries to open it at the same time - and most mornings it works, which is exactly why nobody fixed it.

A four-panel comic: Biscuit says my key first, Miso says no, mine, Pickle says ouch; Pip next to a sign gate key - Nora only says one key, one name; Nora next to an open fence says gate open, now feed them, and Biscuit asks if he can go back to sleep; Miso on a bench says I never wanted that key, and Pickle says nobody did

Pip hangs a sign: "Gate key - Nora only." One key, one name. Nora opens the gate first, and only then does feeding start - you cannot feed residents in an enclosure whose gate is still locked. Biscuit asks the right question: "Can I go back to sleep?" He can. And the joke of the last panel is the honest one: Miso never wanted that key, and nobody did. Everyone had it because handing out keys to everyone was the default.

Every daemon had a key

A service in our platform can run several daemons from one image: a server, a worker, a scheduler. They share one database pool. Until recently the manifest said whether the pool has migrations with a plain flag - migrations: true - and every daemon that used the pool ran them at start.

Most of the time that is invisible, which is why it lasted. But it leaves the questions nobody can answer from the manifest: which daemon changed the schema at this deploy, which one waited for it, and where to look when a migration fails. The word I would put on all of it is the one from the library's own changelog: silently. Nobody decided that the worker migrates. It just did, because the flag meant everyone.

Three tiles with faces: the service manifest, happy; the named daemon, proud; schema ready, happy; labels read migrations.daemons, and migrate, then partitions, enums; a pill reads tested

One name in the manifest

The new form replaces the flag with a list of names. Only the daemons named under migrations run them; the others start without migrating. Before, in the shape of the manifest - values elided:

# before - a flag: every daemon of the pool ran the migrations, silently infra: postgres: main: migrations: true 

After:

# service.yaml - a form infra: postgres: main: migrations: daemons: [server] # was: migrations: true  partitions: { ... } # created after the migrations  enums: { ... } # filled after the migrations 

A gate scene, no code: the migrations notice board lists server, worker and scheduler, only server has the golden key, and a crossed-out note reads was: everyone migrates; Nora opens the gate, and partitions, then enums, are set up after it; a pill reads tested

Nora, the one keeper with the gate key, holds a manifest board that reads migrations, daemons: [server], with the golden key hanging from it; Biscuit and Miso look on

It is one line, and it changes who is responsible. A reader of the manifest now sees which daemon opens the gate. Choosing it is a decision a person makes - the same way the list of daemons itself is a decision people make, not the generator. The library only enforces it.

Bars: before - daemons that migrate in an example pool of three, 3; after - daemons that migrate, 1; lines in the manifest to say so, 1; Nora, happy, says one key on my belt; a pill reads an example pool of three, not a count

The migrations travel inside the binary

The second half of the change is where the migrations live. The generator now embeds them into the service with one option, so the daemon that runs them does not look for files on a disk at start. The image carries its schema with it; there is no "the migrations folder was not copied" morning.

And the embedded migrations run in a fixed place in the start sequence: first, before partitions and enums. That order sounds obvious when written down. It was not obvious in code, and the next section is why it is written down at all.

At start Who does it Depends on
migrations only the named daemon nothing
partitions ahead the server on start, the worker on schedule the schema the migrations created
enum values from a constant, a config path, a column or the runtime the enum type the migrations created
everything else every daemon a ready schema

Columns: migrations first, partitions after, enums after; Pickle, proud, says gate before feeding; a pill reads taller equals earlier; an order, not a time

What the others do

The daemons that are not named still use the pool; they simply do not touch the schema. A worker that starts while the server is still migrating does not race it for the same change - it starts against whatever schema is there. For most services that is the right trade: the server owns the schema, the rest are its readers and writers. For a service where a worker must never see the old schema, that is a deploy-order question, and the manifest now at least makes it visible: one name, one owner of the gate.

What went wrong

The order broke in a real service. The library learned to create partitions as a resource - the server creates the next ones on start, the worker by schedule. A service that keeps its tables in its own schema upgraded, and on start the partitions step ran before the embedded migrations. The schema did not exist yet, and the start failed with the message every Postgres user has seen once: schema "<name>" does not exist.

The start sequence of that service, as a form - the order before the fix and after it:

before: open pool -> create partitions ahead -> run embedded migrations error: schema "<name>" does not exist after: open pool -> run embedded migrations (named daemon) -> partitions -> enums 

That was Nora feeding residents before opening the gate. The fix put the migrations first, before partitions and enums, and it came as one of the six complaints that the last part of this series is about. The lesson I keep from it: when a library adds a new step to the start of every service, the step's position is part of its contract, and "before or after the migrations" is the first question to answer, not the last.

An animation: a parcel travels under four tiles - the named daemon, migrations, partitions, enums - and the pill changes from the key holder starts to embedded migrations run, partitions find their schema, enums filled

There is a quieter lesson in the flag itself. A boolean that means "yes, everyone" is a default that nobody chose. Nothing about it was loud enough to file as a bug. Replacing true with a name removed a decision that the code had been making for us.

Before and after: migrations: true - every daemon of the pool migrated; silent, nobody saw who did it - and migrations: daemons - one named daemon migrates; first, before partitions and enums; a pill reads tested

One more rule stays as it was, and it is worth saying next to this change: an applied migration is immutable. The library treats an applied file as fixed; a change is a new migration, never an edit of an old one. One key opens the gate, and the gate's history is not rewritten.

Service tiles with faces: server proud, worker and scheduler happy, migrations happy; partitions and enums happy, a silent double run bored, an applied migration proud; a pill reads tested

What I do not claim

  • Not how many services already name their migrating daemon. The form is tested in the library.
  • Not that several daemons migrating at once broke data before. What I claim is that nobody chose it.
  • Not a measured start-up gain. The pool of three on the chart is an example.
  • Not the exact manifest syntax. The YAML above is a form.
  • Not that the order between partitions and enums matters. What matters is that both come after the migrations.

What of this is method

  • Name the actor. A list of daemons instead of a flag that means everyone.
  • Ship the schema with the binary. Embedded migrations, no files to forget.
  • Position is contract. A new start step says first whether it runs before or after the migrations.

Zoo keeping, built in - Part 13. Next: A Cage You Cannot Wire By Hand.

If you run several daemons from one image, which of them migrates? If every daemon of yours migrates, could you say which one did it last time? If you added a step to service start-up, did you place it before or after the schema? Who holds the gate key in your zoo?