Using cache keys

Overview

This page explains how to customize Cloud CDN cache keys.

Changing the cache key configuration may result in a sudden drop in cache hit rate if new requests start using cache keys that are different from the old cache key entries. Similarly, changing the cache key creator does not necessarily invalidate cached entries if new requests use the same key as old cache keys. To invalidate existing cached entries see Invalidating Cached Content.

Before you begin

This page assumes an understanding of Cloud CDN, Cloud CDN cache keys, and load balanced backend services. Please review those pages before proceeding.

Turn on Cloud CDN for a backend service and customize the cache keys

These instructions activate CDN for a load balanced backend service and customize the cache key by excluding one or more components. If you don't already have a load balancer to use as an origin, see the HTTP(S) load balancing documentation for instructions on creating one.

Console

  1. Go to the Cloud CDN page in the Google Cloud Platform Console.
    Go to the Cloud CDN page
  2. Click Add origin.
  3. In the Origin pull-down menu, click Select origin.
  4. Select the origin for which you want to turn on CDN.
  5. Click Configure in the row of the origin.
  6. Deselect the checkboxes for any fields you want to omit from the cache key for this backend service.
  7. Click Save.
  8. Click Add.

gcloud

Specify one or more of the optional flags to exclude that parameter from the cache keys for this backend service.

gcloud compute backend-services update [BACKEND_SERVICE] \
    --enable-cdn

Optional flags:

  • --no-cache-key-include-protocol
  • --no-cache-key-include-host
  • --no-cache-key-include-query-string

Updating cache keys to re-add protocol, host, and query string

By default, Cloud CDN includes all components of the request URI in cache keys. If you'd previously indicated one or more components should be excluded, you can use the following steps to re-include them.

These instructions re-add the protocol, host, and query string to the cache key for an existing backend service that already has CDN enabled.

Console

  1. Go to the Cloud CDN page in the Google Cloud Platform Console.
    Go to the Cloud CDN page
  2. Click the three-dot menu button in your load balancer's row.
  3. Select Edit from the menu.
  4. Click Configure in the row of the backend service you want to modify.
  5. Open the Cache key menu and select Custom.
  6. Select the Protocol, Host, and Query string checkboxes.
  7. You can leave the Query string parameters field blank.
  8. Click Save.
  9. Click Update.

gcloud

To re-add one component only, just specify that flag alone. Any unspecified flag are left unchanged.

gcloud compute backend-services update [BACKEND_SERVICE] \
    --cache-key-include-protocol \
    --cache-key-include-host \
    --cache-key-include-query-string

Updating cache keys to use a whitelist or blacklist of query strings

These instructions set CDN cache keys to use a whitelist or blacklist with query string parameters.

Console

  1. Go to the Cloud CDN page in the Google Cloud Platform Console.
    Go to the Cloud CDN page
  2. Click the three-dot menu button in your load balancer's row.
  3. Select Edit from the menu.
  4. Click Configure in the row of the backend service you want to modify.
  5. Open the Cache key menu and select Custom.
  6. Confirm that the Query string checkbox is selected.
  7. Select Only these parameters (whitelist) if you want to specify query string parameters that should be part of the cache key. Select All but these parameters (blacklist) if want to specify that all query string parameters except the ones you list are included in the cache key.
  8. Enter your comma-separated list of strings in the Query string parameters field.
  9. Click Save.
  10. Click Update.

gcloud

Use this command to set the string user to be the whitelist.

gcloud compute backend-services update [BACKEND_SERVICE] \
    --cache-key-include-query-string \
    --cache-key-query-string-whitelist user

Use this command to add the string user to a blacklist.

gcloud compute backend-services update [BACKEND_SERVICE] \
    --cache-key-include-query-string \
    --cache-key-query-string-blacklist user

What's next

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Cloud CDN Documentation