Skip to main content

Implementing Rollback Strategies for Deployments

Roll back failed deployments fast with kubectl rollout undo, Argo CD revert, feature flags, and automated metric-based rollback in Argo Rollouts.

22 Terms

Overview and What You Will Learn

A rollback returns production to the last known-good version after a bad deployment. The fastest options are flipping a feature flag, running kubectl rollout undo, or reverting the commit in your GitOps config repository. Automated rollback in Argo Rollouts goes further by aborting a release the moment a metric crosses a threshold.

Rolling back application code is nearly free, since the old image still exists. Rolling back a database change is the hard part, so this topic also covers how to design migrations that are safe to leave in place.

By the end of this topic you will be able to:

  • Choose between rollback, flag flip, and roll-forward using a decision flow
  • Use kubectl rollout undo and Helm rollback in an emergency
  • Roll back through Git with Argo CD without fighting self-heal
  • Configure Argo Rollouts to abort and revert automatically on bad metrics
  • Design database migrations that do not block a rollback
  • Write and rehearse an incident rollback runbook

Why This Matters in Production

Recovery time is a major driver of how much damage a bad release does. A team that detects a failure and reverts within minutes has an incident that barely registers, while a team that must find the right person and recall a manual sequence can turn the same defect into a long outage. For payment and trading systems, such as those run by Razorpay or Zerodha, every extra minute of failure is directly visible to customers.

Rollback is also a process, not just a command. Missing permissions, a pruned revision history, or a GitOps agent that undoes your fix are all problems you want to find during a rehearsal, not at 3am during an incident.

Common Mistake

Working out the rollback procedure during the incident. Discover that kubectl rollout undo is blocked by RBAC, or that the revision history was pruned, in a rehearsal.

Core Principles

Pick the fastest safe option by asking a few questions in order. Feature flags are fastest, schema changes need extra care, and the delivery method decides which command is correct.

flowchart TD A(["Bad deploy detected"]):::red B{"Behind a feature flag?"}:::slate C["Flip flag off"]:::green D{"Schema change involved?"}:::slate E["Roll forward with fix"]:::orange F{"GitOps managed?"}:::slate G["Git revert, let sync apply"]:::blue H["kubectl rollout undo"]:::blue A --> B B -->|"yes"| C B -->|"no"| D D -->|"unsafe to undo"| E D -->|"safe"| F F -->|"yes"| G F -->|"no"| H classDef red fill:#EF4444,color:#ffffff classDef slate fill:#475569,color:#ffffff classDef green fill:#10B981,color:#ffffff classDef orange fill:#F97316,color:#ffffff classDef blue fill:#326CE5,color:#ffffff
kubectl rollout undo: The Emergency Button

kubectl rollout undo switches a Deployment back to its previous ReplicaSet directly in the cluster. It is fast and needs no Git access. It only reverts the Pod template, so changes to ConfigMaps, Services, replica counts, or other resources are not undone.

Bash
kubectl rollout undo deployment/order-gateway -n order-gateway-prod
kubectl rollout status deployment/order-gateway -n order-gateway-prod

Because it changes the cluster without changing Git, it creates drift. If a GitOps agent with self-heal watches this Deployment, it will restore the broken version on its next reconcile unless you also revert in Git.

Security

kubectl rollout undo needs write access to the cluster. If your normal path is GitOps-only, decide in advance who holds emergency credentials and how that access is granted and audited.

Helm Rollback

If a release is managed with Helm, roll back to a previous release revision, which restores all of the chart's resources and not only the Pod template.

Bash
helm history order-gateway -n order-gateway-prod
helm rollback order-gateway 14 -n order-gateway-prod --wait
Argo CD Rollback: Revert in Git

The GitOps-native rollback reverts the bad commit in the config repository and lets the existing sync apply it. This agrees with how the change was deployed, so self-heal does not undo it. Use a pull request if the branch is protected, and add -m 1 when reverting a merge commit.

Bash
cd config-repo
git switch -c revert-order-gateway-1-8-2
git revert <bad-commit-sha> --no-edit # add -m 1 for a merge commit
git push -u origin revert-order-gateway-1-8-2
argocd app get order-gateway-production --refresh

Argo CD can also sync to a specific earlier revision, which is useful for a temporary fix:

Bash
argocd app sync order-gateway-production --revision <previous-good-sha>
Remember

With automated sync enabled, a manual sync to an older revision is overwritten at the next reconcile, and argocd app rollback is refused entirely. Either disable auto-sync during the incident, or make the revert in Git so the desired state itself changes.

Tip

Prefer git revert over git reset --hard on a config repository. A revert adds an auditable commit and works with branch protection, while a hard reset rewrites history.

Argo Rollouts: Automated Rollback on Metrics

With a canary strategy and an analysis step, Argo Rollouts queries a metrics provider during the release and aborts and reverts without a human if the result is bad. This is the fastest path because it starts the moment the threshold is crossed.

YAML
apiVersion: argoproj.io/v1alpha1
kind: AnalysisTemplate
metadata:
name: error-rate-check
spec:
args:
- name: service-name
metrics:
- name: error-rate
interval: 1m
count: 5
failureLimit: 1 # one bad reading aborts the rollout
successCondition: result[0] < 0.02
provider:
prometheus:
address: http://prometheus.monitoring.svc:9090
query: |
sum(rate(http_requests_total{service="{{args.service-name}}",status=~"5.."}[2m]))
/
sum(rate(http_requests_total{service="{{args.service-name}}"}[2m]))
YAML
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
name: order-gateway
namespace: order-gateway-prod
spec:
replicas: 6
selector:
matchLabels:
app: order-gateway
strategy:
canary:
steps:
- setWeight: 10
- analysis:
templates:
- templateName: error-rate-check
args:
- name: service-name
value: order-gateway
- setWeight: 50
- pause: { duration: 5m }
template:
metadata:
labels:
app: order-gateway
spec:
containers:
- name: order-gateway
image: 123456789012.dkr.ecr.ap-south-1.amazonaws.com/order-gateway:1.8.2
The Hard Problem: Database Migrations

Application rollback is close to free because the old image still exists. Schema changes are not symmetric: a migration that drops a column cannot be undone without losing data written in the meantime. Rolling back the app and rolling back the database are separate decisions.

The standard mitigation is the expand and contract pattern, so no single deploy leaves the system in a state the old code cannot handle:

  1. Expand: add the new column as nullable, or add a new table, without removing anything.
  2. Dual write: deploy code that writes to both old and new, and backfill existing rows.
  3. Switch reads: move reads to the new column once the data is complete.
  4. Contract: drop the old column in a later, separate release, after the new path has run safely for a while.

If a migration truly cannot be made backward compatible, write and review its rollback plan before the migration runs.

Feature Flags: The Fastest Rollback

If the risky change is behind a flag, rollback is switching it off. There is no deployment and no pipeline run, and the change often reaches every instance within seconds. This is why high-risk changes are often shipped dark, with the flag off by default, and enabled gradually after the code is already deployed.

Rollback or Roll Forward
  • Roll back when the previous version is known good and the fix is not ready.
  • Roll forward when undoing would require reversing an unsafe schema change, and a verified fix is faster and safer than reasoning about the data.
  • Flip the flag when the problem is contained inside a flagged feature.

Detailed Step-by-Step Practical Lab

This lab builds and rehearses a rollback runbook for an order-gateway service. Run it in staging first.

Step 1 - Check How Many Revisions You Can Roll Back To

revisionHistoryLimit controls how many old ReplicaSets Kubernetes keeps, and the default is 10. If it is set too low, an emergency undo has nothing useful to revert to.

Bash
kubectl rollout history deployment/order-gateway -n order-gateway-staging
# Inspect what a specific revision contained
kubectl rollout history deployment/order-gateway -n order-gateway-staging --revision=3
Step 2 - Rehearse kubectl rollout undo in Staging

Deploy a deliberately broken image, wait for the rollout to fail, then undo it, timing the whole sequence.

Bash
kubectl -n order-gateway-staging set image deployment/order-gateway \
order-gateway=123456789012.dkr.ecr.ap-south-1.amazonaws.com/order-gateway:does-not-exist
kubectl rollout status deployment/order-gateway -n order-gateway-staging --timeout=60s \
|| kubectl rollout undo deployment/order-gateway -n order-gateway-staging
kubectl rollout status deployment/order-gateway -n order-gateway-staging
kubectl get pods -n order-gateway-staging

To return to a specific revision instead of the previous one, use kubectl rollout undo deployment/order-gateway --to-revision=3 -n order-gateway-staging.

Step 3 - Rehearse the Git Revert Path

Confirm that a revert commit actually triggers a sync and the cluster converges to the earlier state.

Bash
cd config-repo
git revert HEAD --no-edit && git push origin main
argocd app get order-gateway-staging --refresh
argocd app wait order-gateway-staging --health --timeout 120
Step 4 - Verify the Automated Rollback Triggers

Apply the Rollout and AnalysisTemplate from Core Principles to staging. Release an image that returns errors above 2 percent, and confirm the rollout aborts and returns to the stable version without anyone running a command.

Bash
kubectl argo rollouts get rollout order-gateway -n order-gateway-staging --watch
# Manual controls if you need them
kubectl argo rollouts abort order-gateway -n order-gateway-staging
kubectl argo rollouts undo order-gateway -n order-gateway-staging
kubectl describe analysisrun -n order-gateway-staging
Step 5 - Attach a Rollback Note to Every Migration

Review rollback safety alongside the migration in the pull request, using a short template.

TEXT
Migration: add_kyc_status_column
Forward: add nullable column, backfill in batches, default applied by app
Rollback-safe: yes, old code ignores the column
Rollback action if needed: none, no app rollback dependency
Contract step (drop old column): scheduled for a later release
Step 6 - Write the Incident Rollback Runbook

Store this where the on-call engineer can find it, and rehearse it on a schedule.

Bash
1. Confirm the suspected deployment and commit
2. Behind a feature flag? Flip it off first
3. GitOps managed: revert the config repo commit and watch the sync
4. If urgent and sync is too slow: kubectl rollout undo, then also revert
in Git so self-heal does not restore the bad version
5. Schema change in the deploy? Check the migration's rollback note
6. Verify health: pods ready, error rate and latency back to baseline
7. Record the timeline, the path used, and the time to recover

Production Best Practices & Common Pitfalls

  • Keep enough history. Set revisionHistoryLimit high enough that an emergency undo always has somewhere to go.
  • Pair emergency changes with Git. If you undo directly in the cluster, revert in Git too, or self-heal restores the broken version.
  • Review rollback safety at pull request time. Every migration should state whether it is safe to leave in place.
  • Rehearse on a schedule. A procedure nobody has run is a procedure whose timing you cannot trust.
  • Ship risky changes behind flags. Off by default gives you the fastest rollback before you need it.
  • Verify after rolling back. A rollback is only done when health checks and error rates return to normal.
  • Keep old images. Set ECR lifecycle rules that retain the recent tags you might need to roll back to.
Common Mistake

Rolling back the application while the database change stays in place without checking compatibility. If the old code cannot work with the new schema, the rollback itself becomes an outage.

Quick Reference & Troubleshooting Commands

Command Description
kubectl rollout history deployment/<name> List available revisions
kubectl rollout undo deployment/<name> Revert to the previous revision
kubectl rollout undo deployment/<name> --to-revision=N Revert to a specific revision
helm rollback <release> <revision> Roll back a Helm release
argocd app sync <app> --revision <sha> Sync to a specific Git revision
kubectl argo rollouts abort <name> Stop a progressing rollout
kubectl argo rollouts undo <name> Return a rollout to the previous version
Symptom Likely Cause Fix
Undo has nothing to revert to revisionHistoryLimit too low Raise it before the next incident
Rollback keeps getting reverted Self-heal is active Revert in Git, or pause auto-sync
argocd app rollback refused Auto-sync is enabled Use git revert or disable auto-sync
Errors continue after rollback Schema change not addressed Check the migration's rollback note
Automated rollback never fires Analysis query returns no data Run kubectl describe analysisrun and fix the query
helm rollback fails Revision number is wrong Check helm history for valid revisions

Resources

Explore More in Deployment Strategies and GitOps

All 6 Topics

Frequently Asked Questions

Is Implementing Rollback Strategies for Deployments free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Implementing Rollback Strategies for Deployments topic cover?

Roll back failed deployments fast with kubectl rollout undo, Argo CD revert, feature flags, and automated metric-based rollback in Argo Rollouts.

Found a mistake, an outdated step or a broken link?