Cloud Run scale-to-zero exhibits latency spikes after idle periods
0 reputation · 19 Sept 2022, 12:15 UTC
0 reputation · 19 Sept 2022, 12:15 UTC
Goal: Determine the minimum number of instances (minInstances) to configure for a Cloud Run service handling low‑traffic, sporadic requests so that total compute cost remains low while request latency stays within an acceptable threshold.
Constraints: Cold start latency varies with container image size, language runtime, and VPC connector usage, making the latency impact of setting minInstances = 0 unpredictable. Changing minInstances after deployment creates a new revision and may cause temporary traffic splitting, which can affect latency during rollout. The pricing model charges only for active instance time, so any increase in minInstances raises baseline cost proportionally.
Questions: What minInstances value yields the lowest expected cost for a workload with an average request rate of N per hour? How does the observed distribution of cold start latency influence the threshold at which keeping an instance warm becomes cost‑effective? Which Cloud Monitoring metrics should be tracked to validate that the chosen setting maintains the desired latency‑cost balance over time?
33775 reputation · 19 Sept 2022, 14:08 UTC
For a workload with an average request rate of N requests per hour, the cost‑effective minInstances is either 0 or 1. Choose minInstances = 1 only when the expected latency penalty of a cold start (based on the observed distribution) exceeds the latency threshold you can tolerate; otherwise keep minInstances = 0 to minimise cost.
minInstances = k reserves k instances continuously, adding a baseline cost of k × price_per_instance_second × 3600 per hour.minInstances = 0, each request that arrives after an idle period incurs a cold start. The latency of that cold start equals the time to pull the container image, initialize the runtime, and load the application code.minInstances creates a new revision; traffic is split during rollout, which can temporarily affect latency.container_start_time metric (or compute latency as request_timestamp - container_start_timestamp).minInstances = 0.minInstances = 1.run.googleapis.com/request_count to verify the average request rate stays near N.run.googleapis.com/container/start_latency (or the derived cold‑start latency) to ensure spikes remain below the threshold.run.googleapis.com/instance_count to confirm the configured minInstances is respected.run.googleapis.com/request_count – requests per hour (to confirm N).run.googleapis.com/container/start_latency – latency of each container start (cold‑start proxy).run.googleapis.com/instance_count – number of live instances (ensures minInstances is honored).run.googleapis.com/request_latencies – overall request latency distribution (to verify end‑user latency stays within target).Use comments to ask for clarification. Post a solution as an answer.
33,775 reputation · 19 Sept 2022, 17:31 UTC
When evaluating minInstances, use the Cloud Run metric run.googleapis.com/instance/startup_latency (or compute request_timestamp - container_start_timestamp) to isolate true cold‑start latency, excluding requests that hit warm instances. Additionally, note that setting minInstances > 0 does not guarantee zero latency spikes if the concurrency limit is low; burst traffic can cause additional instances to start, increasing cost and latency. Adjust concurrency or maxInstances accordingly to keep the instance count stable.