LOG IN SIGN UP
Documentation

Caching configuration best practices

  Last updated June 08, 2017

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.

Check your cache hit ratio

The number of requests delivered by a cache server, divided by the total number of requests, 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. Your origin server sets the TTL for a variety of objects automatically. When no TTL exists for an object, however, you can specifically set a fallback TTL (sometimes called a "default TTL").

By default, we set your fallback TTL to 3600 seconds (60 minutes). This gives you time to diagnose the root cause of unexpected errors and then purge information from cache once those issues are resolved. You can update that TTL at any time as follows:

  1. Log in to the Fastly web interface and click the Configure link.
  2. From the service menu, select the appropriate service.
  3. Click the Configuration button and then select Clone active. The Domains page appears.
  4. Click the Settings link. The Settings page appears.
  5. In the Fallback TTL area, click the pencil icon next to the TTL setting.
  6. In the Change the Fallback TTL field, type the new TTL in seconds.
  7. Click Save to save your changes.

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:

  1. Log in to the Fastly web interface and click the Configure link.
  2. From the service menu, select the appropriate service.
  3. Click the Configuration button and then select Clone active. The Domains page appears.
  4. Click the Origins link. The Origins page appears.
  5. In the Hosts area, find your origin server and click the pencil icon to edit the host. The Edit this host page appears.
  6. Click the Advanced options link at the bottom of the page. The Advanced options controls appear.
  7. In the First byte timeout field, type the new first byte timeout in milliseconds. Approximately 15000 milliseconds is a good default to start with.
  8. Click Update to save your 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 guide to changing caching times on backend headers describes this in more detail.

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:

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.


Back to Top