Caching configuration best practices
Last updated 2021-04-23
To ensure optimum origin performance during times of increased demand or during scheduled downtime for your servers, consider the following best practices for your service's caching configurations.
Integrate Fastly with your application platform
You can optimize caching with Fastly by customizing your application platform settings. For instructions, see our documentation on integrating third-party services and configuring web server software. We also provide a variety of plugins to help you directly integrate Fastly with your content management system.
Check your cache hit ratio
The number of requests delivered by a cache server, divided by the number of cacheable requests (hits + misses), is called the cache hit ratio. A high cache hit ratio means you've kept request traffic from hitting your origin unnecessarily. Requests come from cache instead. In general, you want your cache hit ratio as high as possible, usually in excess of 90%. You can check your hit ratio by viewing the Stats page for your service.
Set a fallback TTL
The amount of time information can be retained in cache memory is considered its time to live or TTL. TTL is set based on the cache related headers information returned from your origin server. You can specifically set a fallback TTL (sometimes called a default TTL).
If you're using custom VCL, the fallback TTL will be specified in the VCL boilerplate and the fallback TTL configured via the web interface or the API will not be applied. See our documentation on cache freshness and TTLs for more information.
If there's no other source of freshness in the response, setting the fallback TTL to 0 seconds in the web interface will set return(pass)
in vcl_fetch
.
We set a default fallback TTL that you can update at any time as follows:
- Log in to the Fastly web interface.
- From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.
- Click the Edit configuration button and then select the option to clone the active version. The Domains page appears.
- Click the Settings link. The Settings page appears.
-
In the Fallback TTL area, click the pencil icon next to the TTL setting.
- In the Fallback TTL (sec) field, enter the new TTL in seconds.
- Click Save to save your changes.
- Click the Activate button to deploy your configuration changes.
See our Google Cloud Storage instructions if you're changing the default TTL for a GCS bucket.
Configure Fastly to temporarily serve stale content
If your origin becomes unavailable for an extended period of time (for example, being taken offline for maintenance purposes), temporarily serving stale content may help you. Serving stale content can also benefit you if your site's static content is updated or published quite frequently.
You can instruct Fastly to serve stale content by adding a stale-while-revalidate
or stale-if-error
statement on your Cache-Control
or Surrogate-Control
headers. Our guide to serving stale content describes this in more detail.
Decrease your first byte timeout time
After you have configured Fastly to temporarily serve stale, decreasing your first byte timeout time will cause stale content to be served to the requestor faster while fetching fresh content from the origin. Decreasing your first byte timeout time as well as serving stale will reduce unnecessary 503 first byte timeout errors. Decrease the first byte timeout time to your origin as follows:
- Log in to the Fastly web interface.
- From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.
- Click the Edit configuration button and then select the option to clone the active version. The Domains page appears.
- Click the Origins link. The Origins page appears.
- In the Hosts area, find your origin server and click the pencil icon to edit the host. The Edit this host page appears.
- Click the Advanced options link at the bottom of the page. The Advanced options controls appear.
- In the First byte timeout field, enter the new first byte timeout in milliseconds. Approximately 15000 milliseconds is a good default to start with.
- Click Update to save your changes.
- Click the Activate button to deploy your configuration changes.
Increase Cache-Control header times
During times of increased demand, you can instruct Fastly to keep objects in cache as long as possible by increasing the times you set on your Cache-Control headers. Consider changing the max-age
on your Cache-Control
or Surrogate-Control
headers. Our documentation on cache freshness describes this in more detail.
Configure caching actions for specific workflows
When you create a cache setting, the Action setting determines how the request will be handled. The action settings and the most common workflow for each are described below.
-
Do nothing now - Selecting this option will apply the request setting options, but won't force a lookup or a pass action.
-
Pass (do not cache) - Selecting this option will make the request and subsequent response bypass the cache and go straight to origin. Use this option if you need to conditionally prevent pages from caching or when using conditions
-
Restart processing (not common) - Selecting this option will restart processing of the request. Use this option if you need to check multiple backends for a single request
-
Deliver (not common) - Selecting this option will deliver the object to the client. Use this option if you need to create an override condition.
Consider custom error handling
When downtime can't be avoided, standard error messages might not ensure the best user experience. Consider creating custom error messages that include information specific to the request being made and pertinent to the user. Our guide to creating error pages with custom responses provides more detail.
Inform Fastly Customer Support
We like to be sure we're readily available for assistance during customer events. When you know in advance that an event is forthcoming, contact support with details. Be sure to include details about:
- the date and time of the event
- the type of event happening
- how long you expect it to last (if it's planned)
- the Fastly services that might be affected
If the event you're planning is designed to validate the security of your service behind Fastly, be sure to read our guide to penetration testing first.