Progressive Cloud Run rollouts and safe migrations
Smoke-test a tagged Cloud Run revision, shift traffic gradually, watch release metrics and order schema migrations so rollback stays safe.
By Invicly Tech, , in Google Cloud Engineering and Services. About 9 minutes to read.
In short
A progressive rollout puts a new Cloud Run revision live with no traffic, tests it, then sends a small share of users to it before the rest. If error rate or latency rises, you send traffic back. Database changes must be ordered so the old and new code both work during the rollout.
Explained simply
Releasing to everyone at once is risky. If the new version has a bug, every user sees it. A progressive rollout shows the new version to a few users first, and widens only if things look healthy.
Cloud Run makes this easy. Each deploy creates a revision, a snapshot of your code and settings. You can deploy a revision that gets no traffic at all, and give it a tag. The tag gives it its own URL, so you can test it while users stay on the old version.
When the smoke test passes, you send a small share of traffic, such as 10 percent. Then watch two numbers: the error rate and the latency. If both stay normal, move to 50 percent and then to 100.
If something goes wrong, send traffic back to the old revision. This takes seconds because that revision is still there. That is why you keep it.
Databases are the tricky part. During the rollout, both the old and new code run at once, so the database must work with both. Change it in small steps: add, move, then remove.
Press Play on a diagram to walk through it one step at a time.
- 1Deploy, no traffic
New revision gets a tag URL only
- 2Smoke test
Call the tag URL
- 310 percent
Watch errors and latency
- 450 percent
Hold if metrics stay healthy
- 5100 percent
Old revision kept for rollback
- 1Expand
Add new column, keep the old one
- 2Deploy code
Works with both shapes
- 3Migrate data
Backfill in batches
- 4Contract
Drop the old column later
- Back to step 1
- 1Tagged smoke test
Finds obvious breaks before users do
- 2Small traffic share
Limits who is affected
- 3Release metrics
Shows errors and latency by revision
- 4Old revision kept
Instant way back
- 5Backward compatible schema
Makes the way back safe
In depth
Deploy with no traffic and test by tag
By default, a new deploy takes all traffic. Add --no-traffic to prevent that, and --tag to name the revision. Cloud Run then serves it at a URL of the form https://TAG---SERVICE-HASH-REGION.a.run.app. Call a health endpoint and a few key paths. The tag URL goes through the same service, so it uses the same service account and settings. Remove tags you no longer need.
- --no-traffic keeps users on the old revision
- --tag creates a test URL
- Smoke test health and one or two key paths
- Remove unused tags
Split traffic and watch the right signals
Use gcloud run services update-traffic with --to-tags or --to-revisions and percentages that add up to 100. After each step, wait long enough to see real requests, then check Cloud Monitoring for the new revision: request count, 5xx rate and latency percentiles such as p95. Decide the limits before you start, such as no more than 1 percent errors. Small traffic shares give few samples, so low-traffic services need longer waits.
- Percentages must total 100
- Compare new revision with old
- Use p95 or p99 latency, not only the average
- Agree rollback limits in advance
- Engineer to Cloud Run: Send 10 percent to new revision1Send 10 percent to new revision
- Monitoring to Engineer: Errors normal, latency normal2Errors normal, latency normal
- Engineer to Cloud Run: Send 50 percent3Send 50 percent
- Monitoring to Engineer: Error rate rising4Error rate rising
- Engineer to Cloud Run: Send 100 percent to old revision5Send 100 percent to old revision
- Monitoring to Engineer: Back to normal6Back to normal
Expand, migrate, contract
Say you want to rename a column from name to full_name. Step one, expand: add full_name, keep name, and have the app write to both. Step two, release code that reads full_name, falling back to name. Step three, migrate: backfill old rows in small batches. Step four, contract: in a later release, stop writing name and drop it. At every step the previous code still works, so a rollback is safe. Avoid long locks and big single transactions on busy tables.
- Add before you remove
- Write to both during the change
- Backfill in small batches
- Drop old columns in a later release
Learn it in 5 short steps
About 35m each. Track them with a timer in your plan.
- 1Deploy a revision with no traffic~35m
- 2Smoke-test through a revision tag~35m
- 3Split traffic gradually~35m
- 4Watch error rate and latency~35m
- 5Expand, migrate, then contract the schema~35m
Key points
- gcloud run deploy with --no-traffic creates a revision that receives no requests. Add --tag to give it its own URL.
- Test through the tag URL, then use gcloud run services update-traffic to split traffic by percent.
- Watch request count, error rate (5xx) and latency per revision in Cloud Monitoring while you shift traffic.
- Rollback is sending 100 percent back to the previous revision. It takes seconds if the old revision still works.
- Change the schema in three steps: expand, migrate, contract. Never drop or rename in the same release that needs it.
Key terms
Tap a card to flip it. 0/5 known
Examples
gcloud run deploy orders --image=IMAGE_URL --no-traffic --tag=canary --region=us-central1
curl https://canary---orders-HASH-uc.a.run.app/healthz
gcloud run services update-traffic orders --region=us-central1 \
--to-tags=canary=10
# rollback
gcloud run services update-traffic orders --region=us-central1 \
--to-revisions=orders-00041-abc=100Placeholders for service, image and revision names.
Canary a revision on Cloud Run
Prerequisites: a deployed Cloud Run service named orders and a new image. Steps: deploy with no traffic and a tag, call the tag URL, send 10 percent, check metrics, then 100 percent. Expected evidence: gcloud run services describe shows the traffic split, and the tag URL returns healthy. Cleanup: remove the tag with --remove-tags. Cloud Run charges only for use, so a short test is cheap.
gcloud run deploy orders --image=IMAGE_URL --region=us-central1 \
--no-traffic --tag=canary
curl -s https://canary---orders-HASH-uc.a.run.app/healthz
gcloud run services update-traffic orders --region=us-central1 \
--to-tags=canary=10
gcloud run services describe orders --region=us-central1 \
--format="value(status.traffic)"
# cleanup
gcloud run services update-traffic orders --region=us-central1 \
--remove-tags=canaryRoll back after a bad release
At 50 percent the 5xx rate for the new revision jumps from under 1 percent to 8 percent. You pass your agreed limit, so you roll back at once. List revisions, pick the previous good one, and send it 100 percent. Expected evidence: error rate falls back and describe shows the old revision with 100 percent. Afterwards, find the cause in logs before trying again.
gcloud run revisions list --service=orders --region=us-central1
gcloud run services update-traffic orders --region=us-central1 \
--to-revisions=orders-00041-abc=100
gcloud run services describe orders --region=us-central1 \
--format="value(status.traffic)"Quick reference
- --no-traffic deploys a revision that serves nobody
- --tag=NAME gives a revision a test URL
- update-traffic --to-tags or --to-revisions splits traffic
- Percentages must add up to 100
- Watch 5xx rate and p95 latency per revision
- Keep the previous revision until the release is proven
- Schema: expand, migrate, contract
- Never drop a column in the release that stops using it
Common mistakes
- Deleting the old revision too early, so there is nothing to roll back to.
- Dropping a column in the same release that stops using it. Old instances still running will fail.
- Shifting traffic without watching metrics for each revision.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write the command to deploy a revision with no traffic and tag it canary.
- Exercise 2medium
Write commands to send 20 percent to the canary tag and keep 80 percent on a named stable revision.
- Exercise 3hard
Plan the release steps and schema steps to rename orders.name to orders.full_name with a safe rollback at every step.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Can Cloud Run split traffic by user?
- Cloud Run splits requests by percentage, not by user. For user-based rollouts you need logic in your app, such as a feature flag.
- How long should I wait at each step?
- Long enough to see enough requests to trust the numbers. Busy services need minutes; quiet ones need longer.
- Is a tag URL public?
- It follows the service's access settings, so a private service needs authentication for the tag URL too.