Sub topic 12
Rollbacks and feature flags
This chapter assumes you know what a pipeline is, how a build artifact is promoted between environments, and the basic deployment strategies (rolling, blue/green, canary). It covers the two main ways to undo or limit the damage from a bad release.
Why releases need an escape route
Tests do not catch every bug. Some problems only show up under real traffic or real data. A safe delivery process assumes a bad release will eventually happen and makes recovery fast and boring.
There are two tools for this:
- A rollback puts the previous version of the code back in production.
- A feature flag turns a feature on or off at runtime, without a new deploy.
Rollbacks
A rollback redeploys a version that was known to work. This is easy when you follow "build once, promote the same artifact". The old artifact is still stored, so the pipeline can deploy it again without rebuilding.
There are two common ways to roll back:
- Redeploy the previous artifact. Run the deploy job again with the last good version. This is fast and does not change Git history.
- Revert the commit. Use
git revertto create a new commit that undoes the bad change, then let the pipeline build and deploy it. This is slower, but the repository then matches what runs in production.
The following workflow lets an operator redeploy any earlier artifact version by hand. It assumes the same scripts/deploy.sh placeholder used earlier.
name: rollback
on:
workflow_dispatch:
inputs:
run_id:
description: "ID of the earlier ci-cd run whose dist artifact to redeploy"
required: true
jobs:
rollback:
runs-on: ubuntu-latest
environment: production
permissions:
actions: read
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: dist
path: dist/
run-id: ${{ inputs.run_id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- run: ./scripts/deploy.sh dist/Where rollbacks fail
A rollback only restores code. It cannot undo changes to shared state.
- Database migrations. If the new version dropped a column, the old code may crash. Use backward-compatible changes: add a column first, deploy code that uses it, and remove the old column in a later release.
- Data already written. Records created in a new format may confuse old code.
- External effects. Emails sent or payments made stay done.
Feature flags
A feature flag is a condition in the code that decides whether a feature is active. The new code is deployed, but stays off until someone enables it. This separates deploying (putting code in production) from releasing (letting users see it).
import os
def checkout_total(cart):
if os.environ.get("NEW_PRICING") == "on":
return new_pricing(cart)
return old_pricing(cart)
def old_pricing(cart):
return sum(item["price"] for item in cart)
def new_pricing(cart):
return round(sum(item["price"] for item in cart) * 0.9, 2)
print(checkout_total([{"price": 10}, {"price": 20}]))Reading an environment variable is the simplest flag, but changing it needs a restart. Flag services and config stores let you change a flag while the system runs. They can also enable a feature for a percentage of users or a chosen group, which gives canary-style control inside one deployment.
Rollback compared with a flag
| Aspect | Rollback | Feature flag |
|---|---|---|
| What changes | The deployed version | A runtime setting |
| Speed | Minutes, needs a deploy | Seconds, no deploy |
| Scope | Everything in the release | One feature |
| Main risk | State changes cannot be undone | Flag clutter and untested combinations |
Flag hygiene
Flags have a cost. Each one doubles the code paths you should test. Remove a flag once the feature is fully released, and give each flag an owner. Old flags left in place become a common source of confusing bugs.
Exercise
A release adds a new payment flow and also drops an old database column. Production errors rise after the deploy. Why might a rollback alone fail, and what would you do differently next time?
Show answer
Rolling back restores the old code, but the dropped column is gone, so the old code can fail on queries that use it. Next time, split the change. First deploy code that no longer needs the column, then drop the column in a later release. Put the new payment flow behind a feature flag so it can be switched off without a deploy.