Sub topic 10
Rolling back and rolling forward
This chapter assumes you know the pipeline from earlier chapters: one immutable image tagged with a commit SHA, promoted from staging to production, with environment configuration kept outside the image. It also assumes you can read basic kubectl commands. Here you will learn the two ways to recover from a bad release and how to choose between them.
Two ways to recover
A release can pass staging and still misbehave in production. You then have two options.
- Rollback redeploys a previous known-good build. Nothing new is built.
- Roll-forward ships a new build that contains a fix.
Both rely on the build-once rule. A rollback works because earlier artifacts are kept unchanged in the registry. If your pipeline rebuilt per environment, "the previous version" would be a guess, because rebuilding old source today can give a different result.
Rolling back
For a Kubernetes Deployment, the cluster keeps a history of revisions. Undoing the last rollout takes two commands:
kubectl --namespace production rollout undo deployment/app
kubectl --namespace production rollout status deployment/app --timeout=120sThe first command switches the Deployment back to its previous revision, which points at the earlier image. The second waits for the rollout and fails if it does not finish, so a script or pipeline job can detect a failed rollback.
You can also roll back to a specific revision. List the history first:
kubectl --namespace production rollout history deployment/app
kubectl --namespace production rollout undo deployment/app --to-revision=3A rollback is fast because it skips build and test. That speed is also its risk. The old build runs against today's production state, which may have changed since that build last ran.
What makes a rollback unsafe
Check these before you rely on a rollback:
- Database schema. If the new release dropped or renamed a column, the old build may crash. The later chapter on database changes covers the expand and contract pattern that avoids this.
- Data written in a new format. The old build may not read records the new build created.
- Configuration drift. If configuration changed alongside the release, the old build may need the old values.
- Pruned artifacts. A registry cleanup policy may have deleted the old image. Keep every image that ever ran in production for a defined period.
Rolling back through the pipeline
Running kubectl by hand is acceptable in an emergency, but it leaves the record of what runs where out of date. Prefer a pipeline path that redeploys a chosen image through the same deploy.sh script:
./deploy.sh production registry.example.com/app:3f9c1abBecause the script only changes the image, this deploys the older build with the same configuration, the same rollout wait, and the same approval and audit trail as a normal release. In GitHub Actions you can expose this as a workflow_dispatch job that takes the image tag as input.
Rolling forward
Roll-forward means committing a fix, building a new image, and promoting it through staging and production as usual. It is the right choice when:
- the old build is not safe to run, for example after an incompatible migration;
- the fix is small and already understood;
- rolling back would lose data or leave environments inconsistent.
Do not skip staging to save time. A rushed fix that bypasses verification is the most common way one incident becomes two. If you need a faster path, shorten the pipeline with fewer slow tests, and keep the same stages.
Choosing between them
| Situation | Usual choice | Reason |
|---|---|---|
| Bug in new code, no schema change | Roll back | Fastest way to restore service |
| New release applied a destructive migration | Roll forward | The old build cannot run on the new schema |
| Fix is a one-line change with tests | Either | Compare build time with the cost of a second deploy |
| Cause is unknown | Roll back | Restores a known state while you investigate |
A good default is to roll back first when it is safe, since it restores service quickly, and then roll forward with a proper fix.
Exercise
Production shows errors after a release. The release included a migration that dropped a column the previous build still reads. Should you roll back or roll forward, and why?
Show answer
Roll forward. The previous build reads the dropped column, so running it against the migrated database would fail. Ship a fix build through staging and production. To avoid this in future, split the change so the column is removed only in a later release, after no running build reads it.
Exercise
Why does a rollback depend on the build-once rule, and what registry practice does it also require?
Show answer
A rollback redeploys an earlier artifact exactly as it ran before, which is only possible if each build is kept as one unchanged image identified by its commit SHA. The registry must keep previously deployed images and not prune them while they might be needed for rollback.