Build and Deploy · Chapter 2 of 4 · 4 min

Cloud Run: Services and Revisions

How Cloud Run runs a container: services, immutable revisions, traffic, scaling to zero, cold starts, and the settings worth knowing.

What Cloud Run gives you

Cloud Run takes a container image and turns it into a web service with an HTTPS address. You do not manage servers, operating systems or load balancers. It starts containers when requests arrive, adds more when traffic grows, and removes them when traffic stops. You pay for what runs.

For a site like this one (a single server process, modest and uneven traffic), it is close to ideal.

Service and revision

Two words to keep apart:

Revisions are never edited. Changing an environment variable creates a new revision just as a new image does. That is what makes rollback trivial: the previous revision still exists, exactly as it was.

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

Traffic

The service decides which revisions receive requests. By default, a deploy sends all traffic to the new revision as soon as it is ready. You can also:

Readiness

Cloud Run waits for the new container to start listening on its port before sending it traffic. If the server crashes at start-up, for example because a required setting is missing, the new revision never becomes ready and traffic stays on the old one. A broken start is therefore safe. A server that starts but behaves wrongly is not: that is what monitoring is for.

Scaling and cold starts

When no requests arrive for a while, Cloud Run can scale to zero containers. The next request then waits while a container starts: a cold start. For a Node server with modest start-up work this is typically a second or two, enough to show up as a slow first page.

Ways to soften cold starts:

Instances are disposable

Any container can be stopped at any time and replaced. Two consequences:

Behind a proxy

Requests reach the container through Google's front end, so the client address arrives in the X-Forwarded-For header. Express must be told to trust exactly one proxy hop (app.set('trust proxy', 1)). Without it, every visitor appears to come from the same address and shares one rate-limit bucket. Trusting the header blindly is worse: anyone can then fake their address. A test on this site guards both sides.

Settings worth knowing

Setting What it controls
--region Where it runs; here asia-south1, close to most readers
--memory, --cpu Size of each instance
--concurrency Requests one instance handles at once
--min-instances, --max-instances Warm floor, and a cost ceiling
--set-env-vars, --update-env-vars Configuration (replace all, or change some)
--update-secrets Secrets from Secret Manager

To see the current values:

gcloud run services describe iamravi-site --region asia-south1 --project=PROJECT_ID

Try it yourself

List the revisions of a service you run and find the one that served traffic yesterday. If you cannot tell which commit each revision contains, the next chapter is for you.