General development tips (original) (raw)

This guide provides best practices for designing, implementing, testing, and deploying a Cloud Run service. For more tips, seeMigrating an Existing Service.

Write effective services

This section describes general best practices for designing and implementing a Cloud Run service.

Background activity

Background activity is anything that happens after your HTTP response has been delivered. To determine whether there is background activity in your service that is not readily apparent, check your logs for anything that is logged after the entry for the HTTP request.

Configure instance-based billing to use background activities

If you want to support background activities in your Cloud Run service, set your Cloud Run service toinstance-based billing so you can run background activities outside of requests and still have CPU access.

Avoid background activities if using request-based billing

If you need to set your service torequest-based billing, when the Cloud Run service finishes handling a request, the instance's access to CPU will be disabled or severely limited. You shouldn't start background threads or routines that run outside the scope of the request handlers if you use this type of billing.

Review your code to make sure all asynchronous operations finish before you deliver your response.

Running background threads with request-based billing enabled can result in unexpected behavior because any subsequent request to the same container instance resumes any suspended background activity.

Delete temporary files

In the Cloud Run environment, disk storage is an in-memory filesystem. Files written to disk consume memory otherwise available to your service, and can persist between invocations. Failing to delete these files can eventually lead to an out-of-memory error and a subsequent slow container startup times.

Report errors

Handle all exceptions and do not let your service crash on errors. A crash leads to a slow container startup while traffic is queued for a replacement instance.

See the Error reporting guide for information on how to properly report errors.

Optimize performance

This section describes best practices for optimizing performance.

Start containers quickly

Because instances are scaled as needed, their startup time has impact on the latency of your service. Cloud Run de-couples instance startup and request processing, so in some cases a request must wait for a new instance to start before the request is processed. This commonly happens when a service scales from zero.

The startup routine consists of:

Optimizing for container startup speed minimizes the request processing latency.

Use startup CPU boost to reduce startup latency

You can enable startup CPU boost to temporarily increase CPU allocation during instance startup in order to reduce startup latency.

Use minimum instances to reduce container startup times

You can configure minimum instances andconcurrency to minimize container startup times. For example, using a minimum instances of 1 means that your service is ready to receive up to the number of concurrent requests configured for your service without needing to start a new instance. When using minimum instances, avoid using system exits that will shut down an instance and potentially increase cold starts.

Note that a request waiting for an instance to start will be kept pending in a queue as follows:

Requests will pend for up to 3.5 times average startup time of container instances of this service, or 10 seconds, whichever is greater.

Use dependencies wisely

If you use a dynamic language with dependent libraries, such as importing modules in Node.js, the load time for those modules adds to the startup latency.

Reduce startup latency in these ways:

Use global variables

In Cloud Run, you cannot assume that service state is preserved between requests. However, Cloud Run does reuse individual instances to serve ongoing traffic, so you can declare a variable in global scope to allow its value to be reused in subsequent invocations. Whether any individual request receives the benefit of this reuse cannot be known ahead of time.

You can also cache objects in memory if they are expensive to recreate on each service request. Moving this from the request logic to global scope results in better performance.

Perform lazy initialization of global variables

The initialization of global variables always occurs during startup, which increases container startup time. Use lazy initialization for infrequently used objects to defer the time cost and decrease container startup times.

One drawback of lazy initialization is an increased latency for first requests to new instances. This can cause overscaling and dropped requests when you deploy a new revision of a service that is actively handling many requests.

Use a different execution environment

You may experience faster startup times by using adifferent execution environment.

Optimize concurrency

Cloud Run instances can serve multiple requests simultaneously, "concurrently", up to a configurable maximum concurrency.

Cloud Run automatically adjusts the concurrency up to the configured maximum.

The default maximum concurrency of 80 is a good fit for many container images. However, you should:

Tune concurrency for your service

The number of concurrent requests that each instance can serve can be limited by the technology stack and the use of shared resources such as variables and database connections.

To optimize your service for maximum stable concurrency:

  1. Optimize your service performance.
  2. Set your expected level of concurrency support in any code-level concurrency configuration. Not all technology stacks require such a setting.
  3. Deploy your service.
  4. Set Cloud Run concurrency for your service equal or less than any code-level configuration. If there is no code-level configuration, use your expected concurrency.
  5. Use load testingtools that support a configurable concurrency. You need to confirm that your service remains stable under expected load and concurrency.
  6. If the service does poorly, go to step 1 to improve the service or step 2 to reduce the concurrency. If the service does well, go back to step 2 and increase the concurrency.

Continue iterating until you find the maximum stable concurrency.

Match memory to concurrency

Each request your service handles requires some amount of additional memory. So, when you adjust concurrency up or down, make sure you adjust your memory limit as well.

Avoid mutable global state

If you want to leverage mutable global state in a concurrent context, take extra steps in your code to ensure this is done safely. Minimize contention by limiting global variables to one-time initialization and reuse as described above underPerformance.

If you use mutable global variables in a service that serves multiple requests at the same time, make sure to use locks or mutexes to prevent race conditions.

Throughput versus latency versus cost tradeoffs

Tuning the maximum concurrent requests setting can help balance the tradeoff between throughput, latency, and cost for your service.

In general, a lower maximum concurrent requests setting results in lower latency and lower throughput per instance. With lower maximimum concurrent requests, fewer requests compete for resources inside each instance and each request achieves better performance. But because each instance can serve fewer requests at once, the per instance throughput is lower and the service needs more instances to serve the same traffic.

In the opposite direction, a higher maximum concurrent requests setting generally results in higher latency and higher throughput per instance. Requests might need to wait for access to resources like CPU, GPU, and memory bandwidth inside the instance, which leads to increased latency. But each instance can process more requests at once such that the service needs less instances overall to process the same traffic.

Cost considerations

Cloud Run pricing is per instance time. If you setinstance-based billing, the instance time is the total lifetime of each instance. If you set request-based billing, the instance time is the time each instance spends processing at least one request.

The impact of maximum concurrent requests on billing depends on your traffic pattern. Lowering maximum concurrent requests can result in a lower bill _if_the lower setting leads to

But the opposite is also possible: lowering maximum concurrent requests can_increase_ billing if the increase in number of instances is not outweighed by the reduction in time that each instance is running, due to the improved latency.

The best way to optimize billing is through load testingusing different maximum concurrent requests settings to identify the setting that results in the lowest billable instance time, as seen in thecontainer/billable_instance_time monitoring metric.

Container security

Many general purpose software security practices apply to containerized services. There are some practices that are either specific to containers or that align with the philosophy and architecture of containers.

To improve container security:

Automate security scanning

Enable vulnerability scanningfor security scanning of container images stored in Artifact Registry.

Build minimal container images

Large container images likely increase security vulnerabilities because they contain more than what the code needs.

Because of Cloud Run's container image streaming technology, the size of your container image does not affect container startup times or request processing time. The container image size also does not count towards the available memoryof your container.

To build a minimal container, consider working from a lean base image such as:

Ubuntu is larger in size, but is a commonly used base image with a more complete out-of-box server environment.

If your service has a tool-heavy build process consider usingmulti-stage buildsto keep your container light at run time.

These resources provide further information on creating lean container images: