jguillaumesio
cloud-opsdevops

The hotfix shipped on Tuesday. The next release put the bug back.

A webhook consumer was dropping retries in production. We found it, fixed it, tagged it, shipped it. Two days later the scheduled release reintroduced the same bug, because it had been cut before the hotfix existed. Nothing in the pipeline noticed.

A webhook consumer was dropping retries under a condition we had never managed to reproduce outside production. We found it on a Tuesday, fixed it, tagged the hotfix, shipped it that afternoon. The fix was correct. The incident closed.

On Thursday the scheduled release went out, the one that had been stabilising in UAT since the previous week. Friday morning, the same consumer started dropping retries again.

Nothing had been skipped. Both versions were tagged. Both passed CI. Both were validated by someone who knew what they were looking at. The release that reintroduced the bug had been cut on Monday, a day before the hotfix existed, and no part of the pipeline knew or cared.

That is the shape of the problem. A release model is a set of assumptions about the path where nothing goes wrong, and a hotfix is the event that tests all of them at once.

This is the third article in a series on running a multi-tenant SaaS on AWS at team scale, after the autoscaler watching a metric that lied and the alert catalogue nobody read.

The model, and why it was a good one

The setup is worth describing, because most of it survived.

One long-lived branch, main. Feature branches merge into it. A feature built for one customer gets its own branch and stays there until it is either generalised for everyone or dropped, which keeps bespoke work from leaking into the trunk.

A release is a cut, not a permanent branch. We tag a release candidate, stabilise it, then tag the release. Every release is a GitHub tag, so “what exactly is version 1.5.0” has one answer, forever, and the answer is a commit SHA rather than someone’s memory.

Three environments: QA, UAT, PROD. QA follows main, UAT holds whatever is being stabilised, PROD holds the release.

When a fix has to land on a release line rather than on main, it comes back to main as a normal pull request with a human reviewer. A merge conflict between a hotfix and the trunk is a decision about which behaviour is correct, and that is not something to hand to a bot.

This is a good model. It is substantially better than the two things it usually replaces: deploying main and hoping, or long-lived per-customer forks that never converge. The tags give you an audit trail for free. The merge-back PR puts a human exactly where a human is needed.

It holds completely until the first hotfix, and then four separate things break.

1. The release candidate in flight does not contain the hotfix

This is the one that bit us, and it is pure sequencing.

Mon  cut 1.6.0-rc.1 from main      -> goes to UAT
Tue  bug found in prod (1.5.0)
Tue  fix on the release line, ship 1.5.1
Thu  promote 1.6.0 from rc.1       -> bug is back

The hotfix went into the 1.5 line and into main. The release candidate was cut from main on Monday, before either existed. Promoting it to production is a silent downgrade of the fix.

Nobody catches this by reading. The rule has to be mechanical: a release candidate cannot be promoted unless it contains every commit currently running in production. That is an ancestry question, and git answers it directly.

name: promote-to-prod
on:
  workflow_dispatch:
    inputs:
      tag:
        description: "Tag to promote, e.g. 1.6.0-rc.1"
        required: true

jobs:
  guard:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0   # ancestry needs full history and tags

      - name: What is live in production
        id: live
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          sha=$(gh api "repos/$GITHUB_REPOSITORY/deployments?environment=production&per_page=1" \
                  --jq '.[0].sha')
          echo "sha=$sha" >> "$GITHUB_OUTPUT"

      - name: The candidate must contain production
        run: |
          cand=$(git rev-parse "${{ inputs.tag }}^{commit}")
          live="${{ steps.live.outputs.sha }}"
          if ! git merge-base --is-ancestor "$live" "$cand"; then
            echo "::error::$live is live in production and is not an ancestor of ${{ inputs.tag }}"
            echo "Commits in production that the candidate is missing:"
            git log --oneline "$cand".."$live"
            exit 1
          fi

The useful part is the last line. It does not say “blocked”, it lists the exact commits you were about to drop on the floor.

There is a trap that will make you delete this gate within a month. If the merge-back is a squash merge, the production SHA is never an ancestor of anything, because a squash creates a fresh commit with a different parent. The gate fails forever and someone turns it off. So hotfix merge-backs are real merges (--no-ff), never squashes, enforced in branch protection rather than by convention. If you genuinely cannot avoid cherry-picks, git cherry compares patch IDs instead of SHAs and survives them:

git cherry "$candidate" origin/release/1.5 | grep '^+' && exit 1

This also assumes you emit a GitHub deployment on every deploy. If you do not, the poor substitute is a moving prod-current tag that the deploy workflow repositions. Less clean, available today.

2. The version numbers sort backwards

The naming we started with was 1.5.0-rc1 for candidates and 1.5.0-hotfix1 for hotfixes, which reads perfectly and sorts wrong.

SemVer is the MAJOR.MINOR.PATCH convention: major for a breaking change, minor for a compatible feature, patch for a compatible fix, with an optional pre-release suffix after a hyphen. Two of its precedence rules matter here.

A pre-release version has lower precedence than the normal version it belongs to. So 1.5.0-rc.1 sorts before 1.5.0, which is exactly right for a candidate and exactly wrong for a hotfix. 1.5.0-hotfix1 is formally older than 1.5.0. Any tool that sorts by version, a package manager, a Helm chart, a deployment script picking “the latest”, will reach past your hotfix for the release it supersedes.

The second rule is subtler: suffix identifiers are compared field by field, and a field containing letters is compared lexically rather than numerically. rc10 sorts before rc2. A dot makes the number its own numeric field, so rc.10 sorts after rc.2.

The scheme that sorts correctly:

StepTagWhy
Release candidate1.6.0-rc.1, -rc.2, -rc.10the dot isolates a numeric field, so the ordering is numeric
Release1.6.0
Hotfix in production1.6.1, then 1.6.2a hotfix is a patch bump, so it sorts after the release
Hotfix candidate1.6.1-rc.1same mechanism if the fix needs its own validation pass
Build traceability1.6.0+build.482metadata after + is ignored for precedence

Which produces one unambiguous line:

1.6.0-rc.1 < 1.6.0-rc.2 < 1.6.0 < 1.6.1 < 1.6.2 < 1.7.0-rc.1 < 1.7.0

Check it with a real implementation rather than sort -V, which does not follow SemVer on pre-release identifiers:

npx semver 1.6.0 1.6.1 1.6.0-rc.10 1.6.0-rc.2 1.7.0-rc.1

The word “hotfix” disappears from the version string, and that is the right trade. A version number exists to order things, not to describe them. Put the word in the branch name, in the build metadata, or in the release notes, none of which affect precedence.

3. The schema has one version, the code has several

The easy version of this point is about rollback: a tag is immutable, so rolling back means deploying the previous tag, which is true for code and false for the database. Redeploy the old tag and the code goes back, but the migration already ran, so you have version N-1 code talking to a version N schema.

That framing makes it sound like a problem for the day things go wrong. It is not. You already run two code versions at once, on every single deploy.

On a rolling ECS deployment, old tasks and new tasks serve traffic simultaneously, against the same database, for as long as the rollout takes. Same in blue/green. So the migration that just ran is already facing N-1 code, right now, with no incident and nobody having decided anything. If it is not backward compatible, you do not have a rollback risk, you have a window of errors on every release, lasting a couple of minutes, producing 500s that nobody connects to a deploy that “went fine”.

So the real constraint is not about a transition. The schema is a shared singleton that only moves forward. The code is plural and moves both ways. Which gives one rule:

the schema must stay compatible with every version of the code that can still reach it

And that list is longer than it looks. The two versions during the rollout. A task that did not drain cleanly. Workers, crons and queue consumers, which do not deploy on the same cadence as the API. A message already sitting in the queue, written by N code and consumed by N-1 code, where the window is no longer the rollout but the depth of the queue. The version you would roll back to, at any point in the future. Every tenant version still live, if tenants are pinned. Anything reading the database that is not the app at all, an analytics job or a reporting replica.

Three kinds of migration behave very differently against that rule:

  • Additive (nullable column, new table, new index): the old code ignores it, and rollback is safe
  • Destructive (drop, rename, narrow a type, add NOT NULL): the old code breaks, or the data is already gone, and rollback is not possible
  • Backfill (a long data rewrite): a rollback strands you half-way, in a state neither version understands

The technique that makes destructive changes survivable is expand and contract: spread one logical change across releases so no single step is irreversible. Renaming user.name to user.full_name:

ReleaseSchemaCodeRollback
N, expandadd full_name, nullablewrites both, reads namesafe
N+1none, a batched backfill jobwrites both, reads full_namesafe
N+2nonewrites full_name onlysafe
N+3, contractdrop nameunchangedsafe, nothing has used it for a release

Each step is individually reversible. The detail people get wrong is the last two rows: the drop never ships in the same release that stops writing the column. Put them together and rolling back one step resurrects code that writes to a column you just dropped. Split them and every one-step rollback stays valid, at every point in the sequence.

Note also that the backfill is a job, not a migration. Batched, idempotent, resumable. A deploy that takes forty minutes because a backfill is running inside it is an outage with extra steps.

For a release train the rule follows directly: a contract step never ships in the same release as its expand step, and never ships in a hotfix at all. A hotfix carries additive migrations or none. If a fix genuinely requires a destructive change, it is not a hotfix, it is a release, and it goes through the train.

The invariant worth saying out loud in review, because it is binary and checkable: the schema at version N works with code at version N-1 and with code at version N. If you cannot assert that about a migration, it is a contract step wearing a disguise.

The reason this needs a mechanism rather than care is that the worst case is one careful review cannot catch. Take a purely additive migration: add a column, N code writes to it, you roll back. No overlap, no window, the deploy finished days ago. The schema is compatible, nothing throws, the logs are clean. The N-1 code simply stops writing that column, and you accumulate incomplete rows in steady state until somebody notices. The migration was textbook. The defect lives in the pairing of code and schema, not in the migration file, so reading the migration file more carefully was never going to find it.

In multi-tenant with several versions live at once, that window is not N-1, it is “from the oldest supported tenant version to the newest”. If a tenant sits six months behind, your schema supports six months of code. That is the real cost of letting versions diverge per tenant, and it is almost always underestimated when the policy is set.

Testing it is the same discipline as the backup restore nobody runs. A rollback plan you have never executed is a hope. In CI, against a production-shaped anonymised snapshot of the kind described in the preprod data article:

  1. restore the snapshot at schema N-1
  2. run the migration
  3. run the N-1 test suite against the migrated schema

If it passes, your rollback exists. If it fails, you have found out in CI instead of at 2am.

The operational guardrails that go with it: block DROP, RENAME and SET NOT NULL in a CI migration lint unless a label and a human approval are present; open every migration with SET lock_timeout = '3s' so a migration that would block a hot table fails fast instead of taking production with it; create indexes CONCURRENTLY and outside a transaction; and run backfills as batched, idempotent, resumable jobs rather than as migrations. A deploy that takes forty minutes because a backfill is running inside it is an outage with extra steps.

4. The release tag rebuilds the artifact

This one is quiet, and it will cost you a night exactly once.

A git commit is immutable. A container build is not, because it depends on the state of the internet at the moment it runs.

FROM node:22-slim
RUN apt-get update && apt-get install -y openssl
COPY package*.json ./
RUN npm ci
COPY . .

Monday, you tag 1.6.0-rc.3 and CI builds. node:22-slim resolves to one digest, apt-get installs whatever the Debian repository holds that day. That is image A, and it is the one QA validates in UAT.

Thursday, you tag 1.6.0 on the exact same commit and CI builds again. In between, upstream republished node:22-slim with a security patch, and apt-get has newer packages. That is image B.

Same commit, same Dockerfile, different image. UAT tested A, production runs B.

The analogy that lands for anyone who has banned commits on main: node:22-slim is a branch, not a tag. It moves. Your FROM line does the equivalent of git checkout main in the middle of your build, against a repository you do not control.

The failure mode is specific and nasty. A patched OpenSSL hardens certificate chain validation; your service talks to an ageing third-party endpoint; it works in UAT and fails in production. You run git diff 1.6.0-rc.3 1.6.0 and get zero lines. Nothing in your repository explains the outage, because the cause is not in your repository.

The fix is to stop rebuilding. The candidate build produces a digest, and promotion means putting a new name on that digest:

docker buildx imagetools create --tag registry/app:1.6.0 registry/app:1.6.0-rc.3

No build, a second name on the same manifest. Deploy by digest and assert it:

test "$(crane digest registry/app:1.6.0)" = "$(crane digest registry/app:1.6.0-rc.3)"

Pinning the base image by digest helps and is worth doing anyway, but it does not close the hole: apt-get, the npm registry and anything else fetched over the network are still live. Pinning lowers the probability that two builds diverge. Promoting removes the question, because there is only ever one build. You do not need a reproducible build if you never replay it.

Where a hotfix actually gets tested

This is the part with no clean answer, and it is worth being honest about rather than pretending the pipeline solves it.

The awkward fact is that once the release has shipped, the release line has nowhere to run. QA follows main, which has moved on. UAT holds the next candidate. The fix is not really a missing environment, it is that environments were modelled as permanent installations bound to a branch instead of as slots you deploy a tag into. Make the UAT deploy take a tag as an input, add a lock and a visible board showing what currently occupies it, and the problem mostly dissolves without buying anything. The better version, once the seeded snapshot above exists, is an ephemeral environment created from the tag and destroyed on a TTL.

The ladder, with what each rung actually proves:

  1. Local: reproduce the bug. Not a test, an entry condition. If you cannot reproduce it, you do not have a fix, you have a hypothesis
  2. CI on the fix branch: a regression test that fails without the fix. The only mandatory automated gate, and the only thing that stops you shipping this same fix again in three months
  3. An environment running the release lineage, not main. This proves you have not broken the release. main has drifted, so validating there proves nothing about what ships
  4. A canary in production. For a bug that only reproduces under real load, real data or a real third party, this is the only honest test. A traffic slice or one tenant, with the rollback signal defined before the deploy, not during it
  5. A named post-deploy check. The specific metric or log line that proves the bug is gone, confirmed before widening

Steps 1 and 2 prove you fixed a bug. Step 3 proves you did not break the release. Step 4 proves you fixed the bug. Most teams do 1 and 2, declare victory, and find out about step 4 anyway, just without having planned for it.

And the asymmetry that should drive the design: the slower the hotfix path, the greater the pressure to go around it. A four-hour hotfix path gets bypassed at 3am by someone tired with an SSH key. That is the real argument for making environments cheap to create from a tag. It matters far more for hotfixes than for anything else.

The takeaway

Every one of these is the same mistake in a different costume: a property that holds on the happy path, quietly assumed to hold everywhere. Tags are immutable, so rollback works, until a migration runs. Versions sort by recency, until a pre-release suffix inverts it. The release is the thing we tested, until it gets rebuilt. The candidate is ahead of production, until a hotfix overtakes it.

A release model is not tested by the releases that go well. It is tested the first time you have to ship a fix out of order, under time pressure, with someone watching. Write the gates while nobody is watching, because that is the only time you will write them properly.