Skip to main content

Cloud and deployment · Intermediate · 17 min

Release a Cloud Run revision gradually with traffic splitting

By Padding · Updated 18 Nov 2026
Google Cloud Run · Google Cloud CLI 594.0.1

You will release a new Cloud Run revision to 10, then 50, then 100 per cent of traffic behind its own test URL, judge each stage on error rate and latency, roll back in one command, and describe the same split in a service YAML file.

On this page

The tutorial on deploying a container to Cloud Run ended with a rollback: when a change goes wrong, send all traffic back to an older revision. That works, but by then every user has already seen the broken version. A canary release does it the other way round. The new revision starts with no traffic, you test it at its own address, then you give it a small share of real users and widen the share only while the numbers stay healthy.

Cloud Run does this without a load balancer or a second service. Every revision of a service stays available, and you choose what percentage of requests each one gets. A tag gives a revision its own URL, so you can reach it directly whatever its share.

In this tutorial you release version 2 of hello-run in four stages (0, 10, 50 and 100 per cent), decide at each stage whether to continue, practise the one-command rollback, and then write the same release as a service YAML file you can keep in Git. The commands use the region europe-west1, as the earlier tutorial did; use your own region throughout if it differs.

This can cost money. Each revision that receives traffic starts its own instances, and they bill as normal while they handle requests. Old revisions with no traffic scale to zero. The service is the same one you already pay for, so the extra cost is small, but delete the service at the end if it was only for learning.

Step 1 · Write down the stable revision and decide your limits

Before you change anything, record which revision serves users now. That name is your rollback target. List the revisions and look at the service:

Bash
gcloud run revisions list --service hello-run --region europe-west1
gcloud run services describe hello-run --region europe-west1

The describe output includes a traffic section, something like 100% LATEST (currently hello-run-00004-xyz). Copy that revision name into a shell variable. Replace the example name with yours:

Bash
STABLE=hello-run-00004-xyz

Now pin all traffic to that revision by name, instead of to "whatever is latest". With traffic pinned, a new deploy cannot take over by accident:

Bash
gcloud run services update-traffic hello-run --region europe-west1 --to-revisions="$STABLE=100"

Then write your release rules down, before you have a new version you are hoping to ship. Deciding them afterwards tends to bend them. For this tutorial, use these:

  • Errors: the new revision's share of 5xx responses must be no more than 0.5 percentage points above the stable revision's.
  • Latency: its 95th percentile latency must be no more than 20 per cent above the stable revision's.
  • Volume: each stage must see at least 200 requests to the new revision, or 30 minutes, whichever comes later. A handful of requests proves nothing.

Put them in a text file next to your code, such as RELEASE.md, so the next person to release sees the same rules.

Check it worked

Run gcloud run services describe hello-run --region europe-west1 again. The traffic section now shows 100% against the revision name you copied, not against LATEST. echo "$STABLE" prints that same name, and your release rules file exists with all three limits in it.

Step 2 · Deploy version 2 with no traffic and a tag

Change the service so that you can tell the versions apart. Go to the hello-run folder and replace server.js with this version, which adds a version field and reads the GREETING setting from the earlier tutorial:

Bash
cd hello-run
cat > server.js <<'JS'
const http = require('node:http')

const port = process.env.PORT || 8080
const greeting = process.env.GREETING || 'hello'

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' })
  res.end(JSON.stringify({ message: `${greeting} from Cloud Run`, version: '2' }) + '\n')
})

server.listen(port, '0.0.0.0', () => {
  console.log(`Listening on port ${port}`)
})

process.on('SIGTERM', () => {
  server.close(() => process.exit(0))
})
JS

Deploy it with three extra flags. --no-traffic keeps every user on the stable revision. --tag canary gives the new revision its own URL. --revision-suffix v2 names it hello-run-v2, which is easier to type than a generated name:

Bash
gcloud run deploy hello-run --source . --region europe-west1 \
  --no-traffic --tag canary --revision-suffix v2

When the build finishes, the output gives the tagged address. It starts with the tag and three hyphens, something like https://canary---hello-run-<hash>-ew.a.run.app. Save it and the main service URL:

Bash
CANARY_URL=https://canary---hello-run-abc123-ew.a.run.app
URL=$(gcloud run services describe hello-run --region europe-west1 --format='value(status.url)')

Replace the example CANARY_URL with the one from your output. The tag URL has the same access rules as the service: if hello-run is public, so is the tag URL. Nobody finds it by chance, but do not treat it as a secret.

Check it worked

Bash
curl -s "$CANARY_URL"
curl -s "$URL"

The first reply contains "version":"2". The second has no version field, because users are still on the stable revision. gcloud run services describe hello-run --region europe-west1 shows hello-run-v2 with the tag canary and 0% of traffic.

Step 3 · Test the new revision at its own URL

Use the tag URL to try the new revision as a user would, before any user does. Run your usual checks against $CANARY_URL: the pages or API calls that matter most, a sign-in if your app has one, and any request that writes data.

Send a burst of requests too, so the revision starts instances and you see how it behaves cold and warm:

Bash
for i in $(seq 100); do
  curl -s -o /dev/null -w '%{http_code}\n' "$CANARY_URL"
done | sort | uniq -c

Each line of output is a count followed by a status code, so 100 requests that all succeeded print a single line, 100 200. Any other code gets its own line, so nothing is hidden.

To see the cold start, print the total time in seconds for a few requests in a row:

Bash
for i in $(seq 5); do
  curl -s -o /dev/null -w '%{time_total}\n' "$CANARY_URL"
done

If the revision had no running instance, the first time is noticeably longer than the rest while an instance starts.

Think about what happens when both versions run at once, because during the canary they will. Cloud Run picks a revision for each request, so one user can get version 1, then version 2, then version 1 again. That is harmless here. In a real app it means a database change must work with both versions, and a page built by one version must not load scripts that only the other version serves.

Check it worked

The status loop prints one line, 100 200. If any line shows 500 or 503, stop here: read the revision's logs with gcloud run services logs read hello-run --region europe-west1, fix the code, and deploy a new revision with the same --no-traffic --tag canary flags and a new suffix such as v2b. No user has seen the problem.

Step 4 · Send 10 per cent of traffic to the canary

Give the tagged revision a tenth of real traffic:

Bash
gcloud run services update-traffic hello-run --region europe-west1 --to-tags canary=10

You name only the canary. Because the total you set is less than 100, Cloud Run gives the remaining 90 per cent to the revision already serving, which is your stable one.

Send some traffic to the main URL and count how much reaches version 2:

Bash
for i in $(seq 200); do curl -s "$URL"; done | grep -c '"version":"2"'

The split is per request and random, so the number varies around 20 out of 200. Anything from about 10 to 30 is normal.

Check it worked

gcloud run services describe hello-run --region europe-west1 shows 90% on your stable revision and 10% on hello-run-v2 with the tag canary. The grep -c count is roughly a tenth of the requests you sent.

Step 5 · Compare error rate and latency for each revision

Now judge the canary against the rules you wrote down. Cloud Run records two metrics per revision that answer the question: run.googleapis.com/request_count, with a response_code_class label such as 2xx or 5xx, and run.googleapis.com/request_latencies. New data takes up to two minutes to appear, so wait a few minutes after the traffic change.

In the Google Cloud console, open Monitoring, then Metrics Explorer, and build two charts:

  1. Errors: the metric Request Count for the resource Cloud Run Revision. Filter service_name to hello-run, and group by revision_name and response_code_class. You get a line per revision per status class. Divide each revision's 5xx line by its total to get its error rate.
  2. Latency: the metric Request Latencies for the same resource and filter. Group by revision_name and choose the 95th percentile as the aggregator. You get one latency line per revision.

The service's own Metrics tab in Cloud Run gives a quick overall view of the service, but the per-revision comparison is what tells you whether the canary is worse.

To see the failing requests themselves from the command line, read the request logs for the canary revision that returned a server error:

Bash
gcloud logging read \
  'resource.type="cloud_run_revision" AND resource.labels.revision_name="hello-run-v2" AND httpRequest.status>=500' \
  --limit 20

Two limits of these numbers are worth knowing. The latency metric starts when a request reaches your container, so it leaves out the network between the user and Google. Users on slow mobile connections, common on prepaid data plans across much of Africa, can have a worse experience than the chart shows; if you collect timings in the browser or the app, compare those per version too. And at 10 per cent of a quiet service, the canary may see very few requests. If it has not reached your volume rule, wait longer rather than deciding early.

Check it worked

You have two charts, each with a separate line for your stable revision and for hello-run-v2. Write the three numbers for each revision next to your rules: requests, 5xx share and 95th percentile latency. Each canary number is within its limit, or you have a clear reason to roll back.

Step 6 · Widen to 50 then 100 per cent

When the canary passes all three rules at 10 per cent, move to half:

Bash
gcloud run services update-traffic hello-run --region europe-west1 --to-tags canary=50

Wait the same time and check the same charts again. Problems that only appear under more load, such as running out of memory or hitting a database connection limit, often show up here and not at 10 per cent. When the numbers still pass, send everything to the new revision:

Bash
gcloud run services update-traffic hello-run --region europe-west1 --to-tags canary=100

Keep the stable revision for at least a day afterwards. It costs nothing with no traffic, and it is your fastest way back if a problem shows up late, such as a scheduled job that only runs at night.

When you are sure, make version 2 the normal state: send traffic to the latest revision again, and remove the tag so the canary URL stops working:

Bash
gcloud run services update-traffic hello-run --region europe-west1 --to-latest
gcloud run services update-traffic hello-run --region europe-west1 --remove-tags canary

Check it worked

After the canary=100 command, curl -s "$URL" always returns "version":"2"; run the counting loop from the 10 per cent step and the count is 200 out of 200. After the last two commands, describe shows 100% LATEST pointing at hello-run-v2, with no canary tag.

Step 7 · Roll back in one command

Practise the rollback now, while nothing is on fire, so it is familiar when you need it. At any stage of a release, one command sends every request back to the stable revision:

Bash
gcloud run services update-traffic hello-run --region europe-west1 --to-revisions="$STABLE=100"

This takes effect within seconds, because the stable revision already exists and only the routing changes. Nothing is rebuilt. If the stable revision had scaled to zero, the first few requests wait while its instances start. Traffic tags do not allocate minimum instances to a tagged revision, so a tagged revision with 0 per cent of traffic can still start cold when you send traffic back to it.

If you opened a new shell and lost $STABLE, the revision list from the first step shows the name again.

After rolling back, return to version 2 with --to-revisions=hello-run-v2=100, or to the latest with --to-latest.

Check it worked

Straight after the rollback, curl -s "$URL" returns no version field, and the counting loop prints 0. describe shows 100% on your stable revision. Return to version 2 and check that the loop prints 200 again.

Step 8 · Describe the release in a service YAML file

The gcloud flags work, but they leave no record of the split you intended. A service YAML file does: it holds the whole service, including the traffic split, and you can review it and keep it in Git. Export the current service:

Bash
gcloud run services describe hello-run --region europe-west1 --format export > service.yaml

Open service.yaml in your editor. Release version 3 as a canary by changing three things.

First, under spec:, then template:, then metadata:, set the name of the revision this file will create. Use a name you have not used before:

YAML
      name: hello-run-v3

Second, in the same template, find the container's env list and change the GREETING value, so version 3 differs from version 2:

YAML
        - name: GREETING
          value: hello again

Third, replace the whole traffic: list under spec: with this one. It keeps 90 per cent on version 2, sends 10 per cent to version 3 and tags it canary:

YAML
  traffic:
  - revisionName: hello-run-v2
    percent: 90
  - revisionName: hello-run-v3
    percent: 10
    tag: canary

The percentages in the file must add up to 100.

Before you apply it, know that this replaces the service's whole configuration with the file. Any setting someone changed in the console or with gcloud since you exported is overwritten, so export again before each release instead of reusing an old file. Apply the file:

Bash
gcloud run services replace service.yaml --region europe-west1

To widen the canary, change the two percent values to 50 and 50, then to 0 and 100, and apply the file each time. To roll back, set version 2 to 100 and version 3 to 0.

Check it worked

curl -s "$URL" mostly returns "message":"hello from Cloud Run" and about one reply in ten returns "message":"hello again from Cloud Run". describe shows hello-run-v3 with the tag canary and 10%. The traffic block in your service.yaml is a readable record of the release that you can commit.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.