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 earlier 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 typically cached in the Cloud Storage network by default.
The Cache-Control
metadata for an object determines how long browser and
internet caches are allowed to cache your object. The Cloud Storage
network only caches publicly readable objects, but proxies or browsers might
cache objects based on the Cache-Control
metadata even for objects that are
not publicly readable. Additionally, if you revoke public access to an object,
the object can still be served from a cache, depending on when it was last
accessed and its Cache-Control
setting. For example, if your object was served
with a Cache-Control
of public, max-age=3600
, it can persist in a cache for
an hour.
To set the Cache-Control
metadata, 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 principle 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 topublic
withmax-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 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 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 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 the external Application Load Balancer and Cloud CDN along with Cloud Storage.
What's next
- Read the caching overview for Cloud CDN.
- Learn how to create an external HTTP(S) load balancer to serve requests from your Cloud Storage bucket.
- Read the pricing details for external Application Load Balancers and Cloud CDN.
- Read more about the
Cache-Control
metadata. - Learn more about the RFC
Cache-Control
directives.