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 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 old version of the object.

Built-in caching for Cloud Storage

Cloud Storage behaves like a Content Delivery Network (CDN) with no work on your part because publicly readable objects are cached in the Cloud Storage network by default.

The Cache-Control metadata for a publicly accessible object determines how long browser and internet caches are allowed to cache your object. By default, public objects may be cached for up to 3600 seconds (1 hour).

If you revoke public access to an object, the object may still be accessible through an internet cache, depending on the Cache-Control setting. If max-age is set to the default of 3600 seconds or longer, the object may persist in a cache for that duration.

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

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 external HTTP(S) Load Balancing 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.

Objects that are cached through Cloud CDN are not cached in the Cloud Storage cache.

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 5TiB (if origin server supports byte range requests)
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 egress 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 HTTP(S) 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 egress 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 HTTP(S) Load Balancing and Cloud CDN along with Cloud Storage.

What's next