Overriding caching defaults based on a backend response

In certain situations you may want to conditionally apply a different caching policy based on a backend response. In this particular case we have backend that on occasion returns 404 errors (e.g., document not found). We don't want those responses to be cached for the full caching period of a day but only for 5 minutes. To override default caching we add a cache object and then create conditions for it.

Creating the new Cache Object

  1. Log in to the Fastly web interface.
  2. From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain. You can also click Compute services or CDN services to access a list of services by type.
  3. Click Edit configuration and then select the option to clone the active version.
  4. Click Settings.
  5. Click Create cache setting.

    the Cache settings page

  6. Fill out the Create a cache setting fields as follows:

    • In the Name field, enter a descriptive name for the new cache settings.
    • In the TTL (seconds) field, enter the amount of time, in seconds, to cache the objects (e.g., 300).
    • From the Action menu, select Deliver.
    • In the Stale TTL (seconds) field, enter the amount of time to serve stale or expired responses, in seconds, should the backend become unavailable (e.g., 300).
  7. Click Create.

Creating an Override Condition for the new Cache Object

Once the object is created, add a condition to it.

  1. Click Attach a condition to the right of the object.

  2. Click Create cache setting.

    the Create new cache condition page

  3. Fill out the Create a new cache setting fields as follows:

    • In the Name field, enter a descriptive name for the new condition. For example, Override cache default.
    • In the Apply if field, enter an appropriate backend response header to specify when the condition will be applied. For example, beresp.status == 404.
  4. Click Save and apply to.

  5. Click Activate to deploy your configuration changes.

Other notes

You can use any backend response header in the Apply if field to make decisions on caching.

For example, beresp.http.Content-Type ~ "^text/html" can be used to specify different caching rules for HTML documents.

Was this guide helpful?

Do not use this form to send sensitive information. If you need assistance, contact support. This form is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.