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 full caching period of a day but only for 5 minutes. To override default caching we add a cache object and then creation conditions for it.

Creating the new Cache Object

  1. Log in to the Fastly application.
  2. Click the configure tab to access the control panel.

    the Configure tab

  3. Select the appropriate service from the Service menu.

  4. Click the blue Configure button to the right of the service name.

  5. Click the Settings section.

    the Settings section

  6. In the Cache Settings area, click New button. The New Cache Settings window appears.

    the New Cache Settings window

  7. Fill out the New Cache Settings window as follows:

    • In the Name field, type a descriptive name for the new cache settings.
    • In the TTL field, type the amount of time, in seconds, to cache the objects (e.g., 300).
    • In the Stale TTL field type the amount of time to serve stale or expired responses, in seconds, should the backend become unavailable (e.g., 300).
    • From the Action menu, select Deliver.
  8. Click the Create button. A new cache object appears in the Cache Settings area of the Settings section.

Creating an Override Condition for the new Cache Object

Once the object has been created you will need to add a condition to it.

  1. Click the gear icon to the right of the object and select Conditions.

    Conditions selection when editing cache conditions

    The Choose Condition window appears with nothing selected.

    the Choose Condition window

  2. Click New at the options at the top of the window. The New Condition window appears. the New Condition window

  3. Fill out the New Condition window as follows:

    • In the Name field, type a descriptive name for the new condition.
    • In the Apply If field, type an appropriate backend response header to specify when the condition will be applied. For example, beresp.status == 404 || beresp.status == 403
    • Leave the Priority field set at its default value.
  4. Click Create to create the condition.

Once you create the condition, remember to assign it, then deploy the new config version and you are done.

Other notes

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

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

Back to Top