Skip to content
Operate

Upgrade and roll back

Pin a release, mirror it, deploy it, and know which upgrades you cannot roll back.

On this page

You choose when the engine changes. A release is one version number, you copy it into your own registry first, and the deploy only ever reads from that copy. Most upgrades can be undone by deploying the previous version. A few cannot, and this page lists them.

Here is an upgrade from start to finish.

sequenceDiagram
    participant You as Your operator
    participant Project as Your Google Cloud project
    participant Engine as MedHook engine
    You->>Project: Apply the new bundle's Terraform
    You->>Project: Mirror the new version
    You->>Project: Deploy the new version
    Project->>Project: Preflight, backup, helm upgrade
    Project->>Engine: Migrate, roll pods, check /api/health
    Project->>Engine: rotate-master-key --seal-only
    Engine-->>You: Healthy

Pin a release

Every deploy names one exact version, such as 1.0.0-rc.19, in MEDHOOK_VERSION (_MEDHOOK_VERSION in Cloud Build). There is no latest.

  • The engine and MLLP images are pinned to that version.
  • The third-party images (PostgreSQL, Redis, Medplum, Prometheus, Grafana) are pinned by version and digest, from a list inside the bundle.
  • Image tags in your registry are immutable, so a version always runs the same code.

The running version is in /api/version, /api/health and the top of the engine UI's sidebar. Read the release notes before you pick a version, in particular any warning that a release cannot be rolled back.

Upgrade

  1. Get the bundle for the new version through your MedHook contact, and unpack it.

  2. Copy your Terraform variables into it. The bundle never holds them.

    bash
    gcloud storage cp "gs://${PROJECT_ID}-tfstate/medhook/gke/${ENV}/terraform.tfvars" terraform/gke/terraform.tfvars
  3. Run terraform plan and terraform apply from terraform/gke, even if you expect no change. Wait until the apply returns and the cluster has no operation running.

    bash
    gcloud container operations list --project="$PROJECT_ID" \
      --location="$(cd terraform/gke && terraform output -raw cluster_location)" --filter=status=RUNNING
  4. Mirror the new version, as in Install.

  5. Deploy the new version, as in Install.

GKE upgrades the cluster itself only inside the maintenance_window in your Terraform variables (Saturday and Sunday 07:00 to 15:00 UTC by default). Add a maintenance_exclusions entry to freeze upgrades around a go-live.

What a deploy does

You do not run these by hand. The deploy does them in order, and stops where it says it stops.

  1. Preflight. preflight.sh renders the chart and checks the cluster before anything changes. It stops within seconds if a cluster operation is running, if the chart needs infrastructure Terraform has not built yet, or if the deployer lacks a permission. Each failure names its fix.
  2. Backup. With _CLOUDSQL_INSTANCE set, it takes an on-demand Cloud SQL backup with the description medhook pre-deploy <version>. If the backup fails, the deploy stops with nothing changed. _PRE_DEPLOY_BACKUP=false skips it on purpose.
  3. Upgrade. helm upgrade --wait installs the release. _HELM_TIMEOUT is 12 minutes by default.
  4. Migrate. Each new engine pod runs database migrations in its migrate init container before the server starts.
  5. Roll. The previous pods keep serving until new ones are ready.
  6. Check. The build waits for the rollout, then needs /api/health to answer HTTP 200.
  7. Seal. The deploy runs rotate-master-key --seal-only inside the engine. It encrypts any webhook trigger secret still stored as plaintext and moves values that are on an old key to the current one. It is fast, safe to repeat and safe while the engine serves. If it fails, the deploy still succeeds and prints a warning, and you run the command by hand.

A failed upgrade is not rolled back for you. The previous release's pods keep serving, and the build prints the Helm history and the pods so you can find the cause before you deploy again or roll back.

What users see

You don't need to tell people to refresh. Every open engine page checks /api/version once a minute while its tab is in view, and again when the tab gets focus. When the build changes, after an upgrade or a rollback, the page shows MedHook was updated. with a Reload button. It compares with the build that served the page and waits for two checks to agree, so during a rolling deploy, while old and new pods both answer, the notice doesn't flicker and never announces the old build as new. A page that tries to load code the deploy removed says it is out of date instead. The page never reloads by itself, so unsaved changes in the builder stay until the user reloads. Later hides the notice until the next build. While the engine is unreachable mid-rollout the page shows nothing.

The release that indexes message search (1.0.0-rc.19) adds lookup columns and a few small indexes in its migrations. Each index builds without locking the table, at about 10 seconds per 5 million stored messages, so the default _HELM_TIMEOUT is enough up to about 50 million stored messages (about 40 seconds per 5 million for all of them). Above that, raise _HELM_TIMEOUT for this deploy, for example to 30m. The full-text index is larger: it took about 8 minutes per 5 million messages in our test. It is not built by the deploy, so it can't outlast the Helm timeout. Build it, and index the messages stored before the upgrade, with one command after the deploy is green:

bash
kubectl exec -n medhook deploy/medhook-engine -c engine -- backfill-message-index
  • The engine keeps serving while it runs. Until it finishes, searches return the right results but run slower. body: and HL7 field searches use the bounded 2,000-row scan until the full-text index is ready.
  • It is safe to stop and run again. If the session drops while an index is building, Postgres leaves that index INVALID. The next run finds every INVALID executions_msg_* index, drops it and builds it again.
  • It exits with code 2 and names the index if one is still INVALID or missing at the end. Run it again. --indexes-only checks and builds the indexes without indexing old messages.
  • At the end it warms the full-text index with the pg_prewarm extension, installing it if it can, so the first body: search does not read the index from disk. On Cloud SQL pg_prewarm is an allowed extension, and the database owner can install it. If it can't be installed, the step logs why and the run still succeeds. Searches still work; the first body: search is just slower (about 1 s on 5 million messages on a laptop SSD, possibly several seconds on network storage). To install it yourself, run CREATE EXTENSION pg_prewarm; as a privileged user.

After a database restart or failover, the database caches start empty. Warm the index again with:

bash
kubectl exec -n medhook deploy/medhook-engine -c engine -- backfill-message-index --prewarm-only

To check by hand:

sql
SELECT c.relname, i.indisvalid FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid
 WHERE c.relname LIKE 'executions_msg_%';

Roll back

To go back, deploy the previous version again. It is still in your registry.

For an immediate rollback without a build, from a shell with cluster access:

bash
helm history medhook -n medhook
helm rollback medhook -n medhook

Releases keep schema changes compatible with the previous release. CI rejects drops, renames, type changes and locking index builds unless a migration is reviewed and marked. Rolling the application back therefore normally needs no database restore.

Upgrades you cannot roll back

Some changes rewrite stored data in a form older engines do not read. For these, roll forward or restore the database. Do not deploy an older version over the new data.

ChangeWhy an older engine failsWhat to do
Channel builder (1.0.0-rc.13)Saved filters are stored 1-based (filterVersion: 2), and channels contain router nodes. An engine from before the channel builder reads both wrongly.Roll forward, or restore the database.
Encrypted webhook trigger secrets (1.0.0-rc.14)Once secrets are sealed, an older engine reads the enc:v1: value as the secret itself. Every signed webhook answers 403, and a webhook checked only by expected headers stops matching, so nothing runs.Roll forward, or restore the database.
Durable intake (1.0.0-rc.15)Webhook, X12 and FHIR subscription deliveries and workflow retries are written as pending rows in the durable work table, with ordering lanes in Postgres. An older engine never runs them.Roll forward, or restore the database.
Step output stored once (1.0.0-rc.16)A step output equal to the input or an earlier output is stored as a reference (data_ref), and loop body rows store only what differs from the loop input. An older engine reads those outputs as empty.Roll forward, or restore the database.
Encrypted stored messages (1.0.0-rc.17)Once stored message encryption has been on for a workflow, its messages are sealed in a {"$enc":…} envelope. An older engine reads the envelope as the message, so retries, replays and queued deliveries run on it. Turning the setting off does not decrypt stored rows.Roll forward, or restore the database.
Destination queues (1.0.0-rc.17)An older engine does not know queued deliveries or paused queues.Deliver or skip every queued message first (Messages › Queues).
DICOM listener (1.0.0-rc.18)An older engine and chart have no DICOM listener. Rolling back removes it, so DICOM senders get no answer and workflows with a DICOM trigger stop receiving. The chart keeps the DICOM store volume.Point DICOM senders elsewhere first, or roll forward.
Webhook test cases read under data (1.0.0-rc.18)An older engine's Test with sample gives a webhook workflow's steps the bare body, so saved test cases rewritten to read $.data.* fail there and can block Deploy. Live runs are not affected.Roll forward, or have an admin override the failing cases at deploy.
Workflow composition (1.0.0-rc.19)An older engine does not know Component steps, Call workflow steps that wait, topics, connections per sender or template parameters, so workflows that use them do not run as built. Components and templates show there as ordinary draft workflows.Roll forward, or rebuild those workflows without them first.
Waiting calls, topic events and batches (1.0.0-rc.19)An older engine does not resume runs waiting on a called workflow, deliver topic events or continue batches.Let them finish, or cancel them, first.

To restore the backup the deploy took just before the upgrade:

bash
SQL=$(cd terraform/gke && terraform output -raw cloudsql_instance)
gcloud sql backups list --instance="$SQL" --filter="description~pre-deploy"
gcloud sql backups restore BACKUP_ID --restore-instance="$SQL"

A migration failed

You see a failed deploy build and a new pod stuck in Init:Error or Init:CrashLoopBackOff on its migrate container. The pods of the running release keep serving.

bash
kubectl get pods -n medhook -l app.kubernetes.io/component=engine
kubectl logs -n medhook STUCK_POD -c migrate

The log does not show P3009. Nothing was recorded as failed, for example the database was unreachable. Fix the cause and deploy again.

The log shows P3009. Prisma recorded a failed migration, and every new pod stops at migrate until you resolve it. Do not restart the pods that are still serving. From one of them, run prisma migrate status --schema=/app/engine/prisma/schema.prisma, then:

  • If the failed migration changed nothing, which is the usual case because each migration runs in one transaction, mark it rolled back and deploy again:

    bash
    kubectl exec -n medhook deploy/medhook-engine -c engine -- \
      prisma migrate resolve --rolled-back MIGRATION_NAME --schema=/app/engine/prisma/schema.prisma
  • If it applied partway, which can happen with a migration that is not transactional, finish its SQL by hand and run prisma migrate resolve --applied MIGRATION_NAME, or restore the pre-deploy backup.

Checklist

  • You read the release notes for every version between the one you run and the one you deploy.
  • Terraform is applied and no cluster operation is running.
  • The new version is mirrored.
  • The pre-deploy backup is on, or you took one yourself.
  • You know whether this release is one you cannot roll back.
  • You ran the checks in the release notes' Before you upgrade list, such as the step-config query for 1.0.0-rc.16, and each workflow's saved test cases pass.
  • After the deploy, rotate-master-key --check is clean, or you have run --seal-only.

Reference