What is time to live (TTL)?

In the context of a CDN like Fastly, the time to live (TTL) value is the amount of time a specific object will remain in the cache. TTL is also sometimes known as an object’s freshness lifetime because once the TTL is met or exceeded, the object becomes stale, meaning there’s likely a newer, “fresher” version of the object available to serve to customers. 

How does TTL work?

Every object contains HTTP headers that are used to pass information between the client and origin server on the request and the response. You can set certain HTTP headers on the response to control which content is cached and its TTL. Headers like Cache-Control and Expires let you determine the maximum amount of time the object in cache is used to answer requests without consulting the origin server. After the TTL expires, the object may be kept in storage, but it won't be used to answer requests until it can be revalidated with the origin.

Why does the TTL matter?

Delivering content incurs a cost, both in terms of your egress cost, which is the cost hosting providers charge to move content out of storage, and the cost to your customers in latency, the time spent waiting for your content. 

Being strategic with your TTL can help you manage both costs. For example, you might choose to give static content a higher TTL, because that content typically remains relatively unchanged and there won’t be a newer version in your origin. Think images, CSS, or JavaScript files. By keeping this content in cache longer, you can make fewer trips to your origin, reducing egress costs. And, because the content is being served from cache, the latency time to your customers is reduced.

On the other hand, dynamic content, or content that changes at an unpredictable interval, should likely have a lower TTL. This ensures the most up-to-date information gets served. For example, things like breaking news, user-generated content, and current store item inventory are all types of content that change unpredictably. Setting the right TTL helps you balance the needs of your users with the cost of your business.  

TTL best practices

Setting the right TTL helps you balance performance, costs, and availability. 

  • A higher TTL means fewer trips to your origin for “fresh” content.

  • A lower TTL means the cached object expires quicker, and updates from cache occur more frequently. 

Use a higher TTL for static content unlikely to incur any big changes - images, CSS, JavaScript files. Use a lower TTL for dynamic content changing at unpredictable intervals. This ensures your users always have fresh content. 

Because CDNs like Fastly can't invalidate an end user's web browser cache, purging objects from cache is generally easier, and more in your control, than clearing the caches of users' web browsers. For that reason, it can make sense to set different TTLs for content in the cache versus users' web browsers. 

You can set different TTLs for  cache vs web browsers through Surrogate-Control HTTP headers defined by the W3C. Use these HTTP headers to set a long TTL for items in cache, but a shorter TTL for users viewing that object in a web browser.

TTL in Fastly 

There's no one-size-fits-all approach to caching – everyone's needs can be slightly different. In a real world scenario, your caching requirements will depend on a variety of factors, and you want the flexibility to choose how long to cache static content vs. dynamic content. 

Because it’s so easy to purge a Fastly service – whether for a single URL, a group of tagged resources, or an entire service cache – you can get away with setting a long TTL when saving things into the Fastly cache. A long TTL increases your cache hit ratio and the responsiveness of your site for end users, and when you do need to purge for new content, it typically takes only a few seconds.

When you need variable control for your TTL, Fastly provides several options including:

What’s next

Check out our Caching best practices guide to learn more about how different HTTP headers affect TTL and other caching behavior. 

Learn about Fastly CDN

Demander une démonstration