Skip to content

Consumers on Cloud Run

Cloud Run services can consume from lowtide the same ways they consume from Pub/Sub. Push is the better fit. Pull works when the consumer has CPU all the time.

A push subscription sends each message as an HTTPS request to your service. Cloud Run starts instances for the requests and scales to zero when they stop, so you pay only while messages arrive.

  1. Deploy the service with authentication required (--no-allow-unauthenticated). The gateway calls its run.app URL like any HTTPS endpoint.
  2. Create the subscription with the service URL as the push endpoint. To call a service that requires authentication, add a push service account. lowtide signs an OIDC token for it, as Pub/Sub does. The gateway’s Google service account needs roles/iam.serviceAccountTokenCreator on that account, and the account needs roles/run.invoker on the service.
Terminal window
gcloud pubsub subscriptions create orders-push \
--topic=orders \
--push-endpoint=https://orders-consumer-abc123-uc.a.run.app/ \
--push-auth-service-account=orders-push@my-project.iam.gserviceaccount.com

Return a 2xx status to acknowledge. Any other status, or a timeout, is retried with backoff.

Scale to zero needs no special handling. Messages that arrive while the service has no instance wait in lowtide. The first request starts an instance and waits for it, so the first messages after a quiet period arrive later, by the service’s start-up time (under a second for a small Python service in our tests). Requests that fail during a cold start are retried.

Push requests come from the gateway pods in both zones and go to Cloud Run’s front end, not to a zone, so there is no cross-zone delivery to set.

A pull consumer holds a streaming pull open and processes messages between requests. On Cloud Run that only works when the instance has CPU outside requests:

  • a worker pool, which runs instances without requests, or
  • a service with instance-based billing (CPU always allocated) and at least one minimum instance.

With request-based billing, Cloud Run throttles the CPU of an instance that is not handling a request. Messages then sit unacknowledged until their deadline passes and are delivered again, and the backlog grows.

Connect through the VPC: give the service or worker pool Direct VPC egress to the cluster’s network, and point the client at the internal endpoint, as in Client libraries. Set the client’s TLS server name to one in the gateway’s certificate.

Cloud Run places instances in zones it chooses and does not report them, and zonal affinity on the load balancer is documented for VM clients. Expect a Cloud Run consumer to reach gateway pods in either zone, and all of a worker pool’s instances may reach the same zone. The other zone’s messages are then read across zones. Run at least two instances, and consider the Lowest cost setting for these subscriptions (Cross-zone delivery).

A worker pool or service scaled to zero has no open streams. Messages wait in lowtide, as they would on Pub/Sub, and nothing is lost. When an instance starts again, its streaming pull receives the backlog. Messages that were delivered but not acknowledged before the instances stopped are delivered again after their ack deadline. Stop instances gracefully (Cloud Run sends SIGTERM first) so the client can acknowledge what it has processed.