Nov 18, 20267 min read

Cloud Run vs Compute Engine: Containers or VMs on Google Cloud

When to run a service on Cloud Run and when on Compute Engine virtual machines, covering scaling, billing, request limits, background work, networking and what you operate yourself.

Illustration for Cloud Run vs Compute Engine: Containers or VMs on Google Cloud

Cloud Run and Compute Engine both run your code on Google Cloud, and they put the line between you and the platform in different places. Cloud Run takes a container image and gives you an HTTPS endpoint that scales with requests, down to zero. Compute Engine gives you virtual machines with an OS you control, which run whether or not traffic arrives.

For a typical HTTP API, the deciding factors are how traffic arrives, what runs between requests, and what the service needs from the machine. Cloud Run is built for request-driven, stateless services. Compute Engine is the answer when a workload needs a long-lived process, local disk, a non-HTTP protocol, a specific kernel, or steady load at the lowest unit cost.

Cloud Run vs Compute Engine at a glance

Cloud RunCompute Engine
UnitContainer imageVirtual machine
ScalingBy concurrent requests, including to zeroManaged instance group autoscaler, or fixed
BillingRequest-based by default, or instance-basedPer running VM, with sustained and committed use discounts
Request limitsTimeout up to 60 minutes, up to 1,000 concurrent requests per instanceNone beyond your own server
ProtocolsHTTP/1, HTTP/2, gRPC, WebSocketsAnything, including raw TCP and UDP
StateStateless, in-memory file systemPersistent disks, local SSD
OperationsGoogle manages machines, TLS and scalingYou patch the OS, configure load balancing and rollouts

When is Cloud Run the better fit?

Cloud Run fits HTTP services and event handlers whose load varies: APIs, webhooks, Pub/Sub push consumers, internal tools. One gcloud run deploy produces an HTTPS endpoint with a managed certificate, revisions with traffic splitting, and scaling based on concurrent requests per instance, which defaults to 80 times the number of vCPUs and can go up to 1,000. With request-based billing, an idle service with no minimum instances costs nothing.

The constraints come from that model. The request timeout defaults to five minutes and maxes out at 60. Under request-based billing, CPU is only allocated while requests are processed, so work that continues after the response is written stalls. The file system is in memory, instances can be stopped at any time, and cold starts add latency to the first request after scaling up unless you keep minimum instances warm.

When is Compute Engine the better fit?

Compute Engine fits workloads that do not look like requests: a game server on UDP, a long-running stream processor, a database you operate yourself, software licensed per machine, or anything that needs a GPU type, kernel module or local SSD that a managed container platform does not offer. For load that is steady around the clock, VMs with committed use discounts also tend to cost less per vCPU-hour than paying for the same compute on a per-request basis.

The trade is that everything Cloud Run does becomes your configuration. A production setup needs an instance template, a regional managed instance group with an autoscaler and health checks, an external Application Load Balancer with a certificate, OS patching, log and metric agents, and a rollout process for new versions.

How do you deploy a service to Cloud Run with Encore?

Encore

Encore is a backend framework with built-in infrastructure. On GCP it deploys each environment to Cloud Run or GKE in your own project, and Encore provisions an IAM role for the service, or a service account on GCP. Secrets come from Secret Manager, along with the Pub/Sub topics, Cloud SQL databases and buckets your code declares. It does not manage standalone Compute Engine VMs.

Application code passes through Encore and infrastructure guardrails to resources provisioned in your own AWS or GCP account.

Set up the app

Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

~
$ encore app create shipping --example=hello-world$ cd shipping && mkdir shipping$ encore secret set --type dev,local CarrierWebhookKey

Answer fast, work in the background

A carrier webhook is a good Cloud Run workload if the handler respects the platform: verify, queue and return, so nothing has to run after the response. The webhook is a raw endpoint, and the key is an Encore secret:

// Service shipping receives carrier webhooks and processes them in the background. package shipping import ( "crypto/hmac" "crypto/sha256" "encoding/hex" "encoding/json" "io" "net/http" "encore.dev/pubsub" ) var secrets struct { CarrierWebhookKey string } type ShipmentEvent struct { TrackingID string `json:"tracking_id"` Status string `json:"status"` } var shipmentEvents = pubsub.NewTopic[*ShipmentEvent]("shipment-events", pubsub.TopicConfig{ DeliveryGuarantee: pubsub.AtLeastOnce, }) // Webhook verifies the signature, queues the event and answers within milliseconds. // The slow part runs in the subscription, not after the response is written. // //encore:api public raw method=POST path=/webhooks/carrier func Webhook(w http.ResponseWriter, req *http.Request) { body, err := io.ReadAll(io.LimitReader(req.Body, 1<<20)) if err != nil { http.Error(w, "bad body", http.StatusBadRequest) return } mac := hmac.New(sha256.New, []byte(secrets.CarrierWebhookKey)) mac.Write(body) want := hex.EncodeToString(mac.Sum(nil)) if !hmac.Equal([]byte(want), []byte(req.Header.Get("X-Signature"))) { http.Error(w, "bad signature", http.StatusUnauthorized) return } var ev ShipmentEvent if err := json.Unmarshal(body, &ev); err != nil { http.Error(w, "bad json", http.StatusBadRequest) return } if _, err := shipmentEvents.Publish(req.Context(), &ev); err != nil { http.Error(w, "try again", http.StatusServiceUnavailable) return } w.WriteHeader(http.StatusAccepted) }

The work happens in a Pub/Sub subscription, which retries on error and runs as its own unit of work:

package shipping import ( "context" "encore.dev/pubsub" "encore.dev/rlog" ) var _ = pubsub.NewSubscription(shipmentEvents, "update-orders", pubsub.SubscriptionConfig[*ShipmentEvent]{Handler: updateOrder}) func updateOrder(ctx context.Context, ev *ShipmentEvent) error { // Update the order, notify the customer, and so on. An error retries the event. rlog.Info("shipment update", "tracking_id", ev.TrackingID, "status", ev.Status) return nil }

We ran this app with encore run: a correctly signed webhook returned 202, the subscription logged the delivered status for tracking ID 1Z999, and a request with a wrong signature returned 401.

The same code deploys to Cloud Run on GCP or to Fargate on AWS, and switching an environment to GKE does not change it. A coding agent that adds a second carrier writes a second raw endpoint and publishes to the same topic, without touching a Cloud Run service definition or a load balancer.

Best fit: HTTP APIs and event-driven services on GCP that want Cloud Run, Pub/Sub, Cloud SQL and secrets set up from the code, in their own project.

Cloud Run and Compute Engine by hand

On Cloud Run, the deploy is one command once the image is in Artifact Registry and the service account and secret exist:

~
# Cloud Run: one command from a container image to an HTTPS endpoint.gcloud run deploy shipping --image=europe-west1-docker.pkg.dev/my-project/apps/shipping:v42 --region=europe-west1 --concurrency=80 --timeout=60s --min-instances=1 --max-instances=20 --service-account=shipping@my-project.iam.gserviceaccount.com --set-secrets=CARRIER_WEBHOOK_KEY=carrier-webhook-key:latest

On Compute Engine, the same service needs a template that installs and starts it, a managed instance group and an autoscaler, and then the load balancer pieces in front:

~
# Compute Engine: a template, a regional managed instance group, autoscaling.gcloud compute instance-templates create shipping-v42 --machine-type=e2-standard-2 --image-family=debian-12 --image-project=debian-cloud --service-account=shipping@my-project.iam.gserviceaccount.com --scopes=cloud-platform --metadata-from-file=startup-script=startup.sh gcloud compute instance-groups managed create shipping-mig --region=europe-west1 --template=shipping-v42 --size=2 gcloud compute instance-groups managed set-autoscaling shipping-mig --region=europe-west1 --min-num-replicas=2 --max-num-replicas=10 --target-cpu-utilization=0.6 # Still to do: health check, backend service, URL map, HTTPS proxy,# managed certificate, forwarding rule, firewall rules, rolling updates.

The Pub/Sub topic and subscription, the database, the IAM bindings for the service account and the per-environment copies of all of it are separate in both cases, usually in Terraform.

Best fit: Cloud Run by hand for single services on GCP; Compute Engine for workloads that need VMs.

How to choose

Start on Cloud Run for HTTP and event-driven services, and keep handlers request-scoped so request-based billing does not stall background work. Move to Compute Engine when you can name what Cloud Run cannot give you, such as UDP, local state, a long-lived process or the lowest cost for flat 24-hour load. Pick Encore when you want Cloud Run and the Pub/Sub, Cloud SQL and secrets around it provisioned from your Go or TypeScript code in your own GCP project.

Frequently asked questions

What is the difference between Cloud Run and Compute Engine?

Cloud Run runs stateless containers that Google scales by request load, including to zero, and you never manage the machines. Compute Engine gives you virtual machines that run until you stop them, with full control over the OS, disks and network, and you handle scaling, patching and load balancing.

Is Cloud Run cheaper than Compute Engine?

Yes. For spiky or low traffic, Cloud Run with request-based billing charges only while instances handle requests, start or shut down, and can scale to zero. For steady high load around the clock, a right-sized VM with committed use discounts can be the better fit. Compare against your traffic pattern.

Can Cloud Run run background jobs?

Yes. With instance-based billing, a Cloud Run job, or by moving the work to a queue such as Pub/Sub so it arrives as its own request. With request-based billing, CPU is only allocated while a request is being processed, so goroutines or threads that keep working after the response stall.

Does Encore deploy to Cloud Run or Compute Engine?

On GCP, Encore deploys services to Cloud Run or to GKE, which you choose per environment, in your own project. It does not manage standalone Compute Engine VMs, though encore build docker produces an image you can run on a VM yourself.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders