Best Practices for Content Delivery

This page provides best practices for optimizing and accelerating content delivery with Google Cloud CDN.

Cloud CDN uses HTTP(S) load balancing as the origin for cacheable content. An HTTP(S) load balancer can deliver a mix of static and dynamically-created content to users through one global IP address from Google Compute Engine virtual machine (VM) instance groups, Google Cloud Storage buckets, or both.

Because of the seamless integration with Google Cloud Platform, you have a several options for deploying Cloud CDN and managing content. Use the best practices listed here to plan and refine your deployment. For more information, see Using Cloud CDN.

Setting the expiration for time-sensitive content

Each piece of content in a Cloud CDN cache has an associated expiration time, and it's important to set an expiration that is appropriate for your use case. Because origin servers must resend content that expires on cache servers, you need to choose the expiration carefully.

One method for choosing the expiration is to categorize content based on how often you update the content, for example:

  1. Near real-time updates, for example live feeds for sporting events or traffic
  2. Frequent updates, such as weekly, daily, or hourly weather information or front-page news images
  3. Infrequent updates, such as a website logo or CSS or JavaScript files

Then, choose the expiration by content category. For example, a 5-second expiration might be appropriate for near real-time sports scores, and a one-hour expiration could be used for weather updates. For content stored in Cloud Storage, set the expiration times by using custom metadata. When content is served by Compute Engine, you control expiration times by configuring your web server software.

Expiration times are specified by the max-age and s-maxage values in the Cache-Control header. This header is defined by the HTTP specification. For example, the following Cache-Control makes the associated content publicly readable and cacheable with a cache expiration of 72 hours (259200 seconds):

 Cache-Control: public, max-age=259200

To maximize caching, follow the guidelines in Caching Details. Remember that max-age and s-maxage values in the Cache-Control metadata field work together in the following ways:

  • The max-age and s-maxage values are measured in seconds.
  • The s-maxage value applies only to shared caches, not browser caches.
  • The max-age value applies to all caches unless s-maxage overrides it.

For content that changes infrequently or that must change along with related content, it's often appropriate to use a long expiration time in combination with versioned URLs.

Using custom cache keys to improve cache hit ratio

By default, Cloud CDN uses the complete request URL to build the cache key. For performance and scalability, it’s important to optimize cache hit ratio. To help optimize your cache hit ratio, you can use custom cache keys .

Cloud CDN key-value store
Cloud CDN key-value store

You can customize cache keys to include or omit any combination of protocol, host, and query string. For example, suppose you have two websites on different domains that use the same logo. To show the logo, use custom cache keys, as follows:

  • The website content is different, but you use the same company logo on both domains. When you turn on Cloud CDN and customize the cache keys for the backend service that holds the logo, deselect the Host checkbox so that the cache ignores the domain but caches the logo.
  • The logo needs to be cached whether displayed through HTTP or HTTPS. When you customize the cache keys for the backend service that holds the logo, deselect the Protocol checkbox so that requests through HTTP and HTTPS count as matches for the logo's cache entry.

To learn how to customize cache keys, see Using Cache Keys.

Using versioned URLs to update content

Versioning content serves a different version of the same content, effectively removing it by showing users new content before the cache entry expires. Because versioning is free and easy to use, we recommend you use versioning as the default approach for updating cacheable content. To version content, add a parameter to the URL, such as a version number. There are various ways to include parameters in URLs, such as:

  • Add a query string: file.ext?v=100
  • Alter the file name: file.1.0.0.ext or file_v100.ext
  • Alter the file path: /v100/file.ext

When you add the parameter, you change the name of the file and the URL. This change forces the cache to ignore any existing cache entry.

Using invalidation sparingly to remove content

Invalidation removes content from the Cloud CDN distributed cache servers before the cache entry expires. Invalidation is eventually consistent.

We recommend that you use invalidation sparingly and only as a last resort. For example, invalidation is useful when you must remove content for legal reasons or because of an accidental upload. Otherwise, we recommend that you use versioning whenever possible or wait until the content expires normally. Cloud CDN cache servers evict rarely used content automatically and remove unaccessed content in 30 days.

In contrast to setting expiration times and using versioned URLs, you are charged for each cache invalidation you initiate, and cache invalidations are rate limited.

To learn more about invalidation, see Cache Invalidation Overview.

Reviewing third-party performance tests

Review reports from third-party providers, such as the availability, latency, and throughput reports that the Cedexis Radar Community provides.

Send feedback about...

Cloud CDN Documentation