Caching

This page discusses the options you have to control how your Cloud Storage objects are cached. This page focuses on the Cloud Storage built-in cache and Cloud CDN, but Cloud Storage is also compatible with third-party CDNs.

Overview

When a Cloud Storage object is cached, copies of the object data are stored in a Google or internet cache so your object can be served faster in future requests. While caching can improve performance, you also risk serving stale content if you make updates to your object but a cache continues to serve the earlier version of the object.

Built-in caching for Cloud Storage

Cloud Storage can behave like a Content Delivery Network (CDN) with no work on your part, because an object's data is cached in the Cloud Storage network if its Cache-Control metadata is set to allow caching and the following criteria are met:

Cloud Storage respects standard values for Cache-Control, such as the following:

  • public: the object can be cached.

  • private: the object won't be cached by Cloud Storage, but can be cached in a requester's local cache.

  • no-cache: the object can be cached, but cannot be used to satisfy future requests unless first validated by Cloud Storage.

  • no-store: the object can't be cached.

  • max-age=TIME_IN_SECONDS: the length of time an object can be cached before it's considered stale. You can set max-age to any length of time. Stale objects are not served from caches, except in special circumstances.

To set the Cache-Control metadata for an object, see Editing object metadata.

Built-in caching behavior with IAM Deny policies

When there's an organization-level IAM Deny policy that restricts read access for an object from the principal identifier allUsers, built-in caching is disabled for the object, even if there's a bucket-level IAM policy that grants read access for the object to allUsers. However, if the IAM Deny policy only restricts individual users, built-in caching remains enabled for the object.

Performance considerations

Performance can be much better for publicly cacheable objects. If you have an object being used to control many clients and thus want to disable caching to provide the latest data:

  • Consider instead setting the object's Cache-Control metadata to public with max-age of 15-60 seconds. Most applications can tolerate having an object be out of date for a few seconds, in exchange for performance improvements.

  • Use Cache-Control: no-store for an object to indicate that the object must not be cached for subsequent requests in any cache.

Cloud Storage with Cloud CDN

For the best performance when delivering content to users, we recommend using Cloud Storage with Cloud CDN.

To use Cloud CDN, you must use an external Application Load Balancer with your Cloud Storage buckets as a backend. For a tutorial on setting up an HTTP(S) load balancer with a Cloud Storage bucket, see Hosting a static website.

Cloud CDN cache modes allow you to apply a unified caching configuration across all your objects. Cloud CDN uses the Cache-Control metadata set on your objects to determine how they should be cached, unless you override the Cache-Control metadata using a cache mode or TTL limit.

When choosing between Cloud Storage built-in caching and Cloud CDN, consider the following:

Feature Cloud Storage Cloud CDN
Max cacheable file size 10 MiB 5 TiB
Default cache expiration 1 hour 1 hour (configurable)
Support for custom domains over HTTPS No Yes
Cache invalidation No Yes

Pricing considerations

In terms of pricing, the choice between Cloud Storage built-in caching and Cloud CDN depends on how much data you serve every month, which determines the amount of networking costs you incur.

  • If you serve less than a few GiB of cacheable data a month, it may be cheaper overall for you to rely on Cloud Storage built-in caching. Cloud Storage caching may incur higher networking costs than Cloud CDN, since cached and uncached objects are charged the same outbound data transfer cost (which means you pay full price for cache hits). However, you only pay for data storage and operations usage costs associated with Cloud Storage, instead of the combination of Cloud Storage, Cloud CDN, and Cloud Load Balancing.

  • If you regularly serve 100GiB or more of cacheable data a month, or need to use per-request logging and custom headers, it may be cheaper overall for you to rely on Cloud CDN. You incur Cloud Storage outbound data transfer and Cloud CDN cache fill costs for cache fill, and Cloud CDN networking prices apply after the cache is full. The networking cost savings you gain from using Cloud CDN may be worth the higher operating costs associated with maintaining the external Application Load Balancer and Cloud CDN along with Cloud Storage.

What's next