Build and Deploy · Chapter 4 of 4 · 3 min

Rolling Back

When a deploy goes wrong, first restore service, then fix forward. The one Cloud Run command that sends traffic back, and the habits around it.

Restore first, understand later

When a new version is broken in production, there are two jobs: get visitors back to a working site, and understand what went wrong. Do them in that order. Debugging while visitors see errors only makes the outage longer.

Rollback is the fastest way to restore service, because the previous version still exists and is known to work.

The command

Cloud Run keeps every revision. Rolling back means moving traffic to an older one. First, find it:

gcloud run revisions list --service iamravi-site --region asia-south1 --project=PROJECT_ID --limit 5

Then send all traffic to the last good one:

gcloud run services update-traffic iamravi-site --region asia-south1 --project=PROJECT_ID --to-revisions=REVISION_NAME=100

This takes seconds. No rebuild, no waiting for tests: the old revision is already built and was already serving yesterday.

What a rollback does not undo

Rollback changes the code that serves requests. It does not undo:

So before rolling back, ask: did the bad version change any data or anything outside itself? If not, roll back without hesitation.

After the rollback: the pinned state

After update-traffic points at a named revision, the service is pinned: the next deploy creates a new revision but does not automatically receive traffic. That is useful while you investigate, and dangerous if you forget. When the fix is ready, deploy it and send traffic to the newest revision explicitly:

gcloud run services update-traffic iamravi-site --region asia-south1 --project=PROJECT_ID --to-latest

Fix forward through the normal path

The real fix goes through the normal pipeline: a branch, a test that reproduces the problem, a pull request, green checks, merge. Or, if the fastest fix is to remove the bad change, revert the merge commit (git revert -m 1 <merge>) and let that revert go through the pipeline like any other change. Either way, main ends up describing what production runs again.

Avoid "hotfixes" made directly on the server or outside Git. They are lost on the next deploy and leave main out of step with reality.

Practise before you need it

A rollback you have never done is a rollback you will do slowly, under pressure, at night. Practise it on a quiet day:

  1. note the current revision;
  2. roll back to the previous one and check the site;
  3. return with --to-latest and check again.

Five minutes, and the commands are in your fingers when it matters.

When to roll back, in one table

Situation Action
New version errors or is badly slow; no data changed Roll back now, investigate after
New version changed data Pause; decide whether old code can read new data; consider restoring data too
Small cosmetic bug Fix forward through a normal PR
You are not sure Roll back. It is cheap and reversible

Try it yourself

Write your rollback commands, with your real service name, region and project, into your runbook today. In an incident, typing the right command from memory is harder than it sounds.