Google Cloud Tutorial · Compute · Lesson 12 of 27

Cloud Run

Running containers without servers: services, jobs, concurrency, scaling, cold starts and pricing.

Members can save this page, take notes and track progress. See membership · Sign in

Services and jobs

Cloud Run services run a container that listens for HTTP requests (or gRPC, WebSockets) and scale with traffic, down to zero. Cloud Run jobs run containers to completion: batch processing, migrations, scheduled tasks.

gcloud run deploy orders-api \
  --image europe-west1-docker.pkg.dev/acme-orders-prod/apps/orders-api:3f2c9e1 \
  --region europe-west1 --service-account [email protected] \
  --concurrency 80 --min-instances 1 --max-instances 50 --no-allow-unauthenticated

Concurrency and scaling

Each instance handles up to concurrency requests at the same time (80 by default, up to 1,000). Cloud Run adds instances when the current ones are busy and removes idle ones. Useful settings:

  • min instances keep warm instances to avoid cold starts (the delay of starting a new container);
  • max instances protect databases and downstream services from too many connections;
  • startup CPU boost speeds up start-up.
How many Cloud Run instances a workload needsTry it yourself
import math

def instances_needed(requests_per_second, seconds_per_request, concurrency):
    in_flight = requests_per_second * seconds_per_request      # Little's law
    return math.ceil(in_flight / concurrency)

for rps, latency, conc in [(50, 0.2, 80), (500, 0.2, 80), (500, 0.2, 1), (2000, 0.5, 250)]:
    print(f"{rps:5} req/s x {latency}s latency, concurrency {conc:3}: "
          f"{instances_needed(rps, latency, conc)} instance(s)")

Output

   50 req/s x 0.2s latency, concurrency  80: 1 instance(s)
  500 req/s x 0.2s latency, concurrency  80: 2 instance(s)
  500 req/s x 0.2s latency, concurrency   1: 100 instance(s)
 2000 req/s x 0.5s latency, concurrency 250: 4 instance(s)

Pricing

With request-based billing you pay mostly while requests are being handled, so idle services cost little; with instance-based billing you pay for the instance's whole lifetime, which suits background processing.

Test yourself

Why would you set max instances on a Cloud Run service?

  1. To make it scale to zero
  2. To stop it opening too many connections to a database during a traffic spike
  3. To reduce cold starts
  4. To enable HTTPS
Show answer

Answer: To stop it opening too many connections to a database during a traffic spike

Each instance holds connections; capping instances protects downstream systems.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake