Cloud Run can allocate CPU only while a request is being handled or for the whole life of the instance. How each mode behaves, what breaks with background work under request-based allocation, how to switch with gcloud, and how to choose.

A Cloud Run service that works in testing and drops background work in production has usually run into CPU allocation. Cloud Run decides whether your container gets CPU only during requests or all the time, and the default is the first.
This guide explains the two modes and what stops working under request-based allocation. It then covers how to switch and which to choose, with a Go example of work structured to survive either mode.
Cloud Run's CPU allocation setting has two values: with request-based allocation, the default for services, CPU is allocated while the instance processes requests, and during startup and shutdown. Outside that, CPU is throttled, so the container is effectively paused between requests. With instance-based allocation, CPU is available for the whole life of the instance, including when it is idle, and you can run background work freely.
Billing follows the mode, and request-based billing charges for the time spent handling requests plus a per-request fee, so an idle service costs nothing for CPU. Instance-based billing charges for the full instance lifetime without the request fee. Check Cloud Run pricing for current rates before deciding, since the break-even depends on utilization.
Anything that expects to run between requests stops making progress: a goroutine that finishes work after the handler returns, a setInterval that refreshes a cache, a background thread flushing metrics, or a message pull loop all lose their CPU once the last request ends. The symptoms are sporadic: work completes if another request arrives soon, and stalls otherwise, so logs show tasks finishing minutes late or only when traffic returns.
Work the response depends on should happen inside the request. Work that can finish later should go on a queue such as Pub/Sub push or Cloud Tasks, so a new request delivers it. Work that must run continuously, such as a pull-based consumer or in-memory state that needs refreshing, needs instance-based allocation.
With gcloud:
# Instance-based: CPU always allocated
gcloud run services update my-service --no-cpu-throttling
# Request-based: back to the default
gcloud run services update my-service --cpu-throttling
In Terraform it is the run.googleapis.com/cpu-throttling annotation on the service template. Instance-based allocation pairs naturally with a minimum instance count, since otherwise instances still scale to zero when idle.
Use request-based for APIs and push-delivered workers where all work happens inside requests and traffic is bursty or low, since you pay only while serving. Use instance-based when you run background work, hold WebSocket connections, or have steady traffic high enough that instance billing is cheaper per request. Cold starts are a separate issue, covered in Cloud Run cold starts.
Encore is a backend framework with built-in infrastructure that deploys services to Cloud Run in your GCP project. Work in an Encore service arrives as requests, either API calls or Pub/Sub deliveries, so structuring background work as messages keeps it inside a request, the shape request-based allocation expects.
Run in your terminal to get started locally.
// signup/signup.go
package signup
import (
"context"
"encore.dev/pubsub"
"encore.dev/rlog"
)
type SignupParams struct {
Email string `json:"email"`
}
type Welcome struct {
Email string `json:"email"`
}
var WelcomeEmails = pubsub.NewTopic[*Welcome]("welcome-emails", pubsub.TopicConfig{
DeliveryGuarantee: pubsub.AtLeastOnce,
})
// Signup responds quickly and hands the slow part to a queue.
// A goroutine started here could stall once the response is sent.
//
//encore:api public method=POST path=/signup
func Signup(ctx context.Context, p *SignupParams) error {
// Create the account here, then:
_, err := WelcomeEmails.Publish(ctx, &Welcome{Email: p.Email})
return err
}
var _ = pubsub.NewSubscription(WelcomeEmails, "send-welcome", pubsub.SubscriptionConfig[*Welcome]{
Handler: func(ctx context.Context, w *Welcome) error {
rlog.Info("sending welcome email", "email", w.Email)
return nil // call the email provider here; the work happens inside this delivery
},
})
The Signup endpoint returns as soon as the message is published, and the email is sent inside a Pub/Sub delivery, which on GCP reaches the service as an HTTP request, so the instance has CPU for the whole operation, and a failure is retried instead of lost. A trace links the signup request to the delivery that sent the email. If you need continuous background work in an Encore service, check the CPU allocation setting of the Cloud Run service in your project and review it against the guide above.
A coding agent can move a fire-and-forget goroutine into a topic and subscription in one change and test the flow locally with encore run.
Best fit: Go teams on Cloud Run who want slow work to survive request-based CPU allocation by moving it onto a queue declared in the service code.
With request-based allocation, CPU is available only while the instance is handling a request, plus during startup and shutdown, and is throttled to near zero otherwise. With instance-based allocation, CPU is available for the entire lifetime of the instance, so background threads and timers keep running between requests.
Under request-based allocation the CPU is throttled once the last request on an instance finishes, so goroutines, timers and async tasks that outlive the response make little or no progress. Either keep the work inside the request, move it to a queue, or switch the service to instance-based allocation.
Run gcloud run services update SERVICE --no-cpu-throttling for instance-based allocation, or --cpu-throttling to return to request-based. The same setting is in the console under the service's CPU allocation and pricing options, and in the service YAML as the run.googleapis.com/cpu-throttling annotation.
Encore deploys services to Cloud Run in your GCP project, and work in an Encore service arrives as requests: API calls and Pub/Sub deliveries. Publishing work to a Pub/Sub topic instead of starting a goroutine keeps it inside a request, which is the pattern request-based allocation expects. CPU allocation is a setting on the Cloud Run service in your project, not a declaration in Encore code.
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.