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:
- A service is the long-lived thing with a name (
iamravi-site) and an address. - A revision is an immutable snapshot of the service: one image plus one set of settings. Every deploy, and every settings change, creates a new revision.
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:
- deploy without traffic (
--no-traffic) and test the new revision at its own tagged address first; - split traffic, for example 90 per cent old and 10 per cent new, to try a change on a slice of visitors;
- move traffic back to any older revision instantly (next-but-one chapter).
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:
- keep start-up work small; load slow things lazily, after the server is listening. On this site, the photo list from Cloud Storage refreshes after start-up rather than blocking it;
- set a minimum number of instances (
--min-instances=1) to keep one container warm, at the cost of paying for it while idle.
Instances are disposable
Any container can be stopped at any time and replaced. Two consequences:
- Never keep important state in memory or on the container's disk. It disappears with the container. Anything that must survive goes to Firestore or Cloud Storage.
- Anything kept per instance is per instance. This site's rate limiter keeps its counts in memory. With one instance that is exact; with several, each keeps its own count. If traffic ever needs more instances, the limiter must move to a shared store. That is written down as a known limit rather than discovered during an incident.
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.