TTL (Time To Live) (TTL)
How long a cached response is considered fresh before it must be revalidated or re-fetched. Set via Cache-Control max-age or s-maxage. The core concept behind cache freshness.
Full Explanation
TTL is the timer on every cached object. When it expires, the cache either revalidates (conditional request with ETag/If-Modified-Since) or fetches a fresh copy from upstream. Getting TTLs right is one of the most impactful things you can do for CDN performance.
Short TTLs (seconds to minutes) mean fresher content but more origin traffic. Long TTLs (hours to days) mean better cache hit ratios but harder to push updates. The sweet spot depends on how often your content changes and how much staleness you can tolerate.
For static assets with content hashes in the filename (style.a1b2c3.css), set TTL to a year—the hash changes when the content changes. For HTML pages, shorter TTLs (minutes) with stale-while-revalidate give you freshness without sacrificing speed. For APIs, it depends entirely on your data.
See the interactive TTL Countdown animation in the course to watch content freshness expire in real time and understand what happens next.
Interactive Animation
Examples
# Set TTL via Cache-Control
Cache-Control: public, max-age=3600 # 1 hour for all caches
Cache-Control: public, s-maxage=86400 # 1 day on CDN, default for browsers
Cache-Control: public, max-age=31536000 # 1 year (immutable assets)
# Nginx: set TTL per location
location /static/ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location /api/ {
add_header Cache-Control "public, max-age=60, s-maxage=300";
}
# Check remaining TTL from CDN response
$ curl -sI https://cdn.example.com/style.css | grep -i 'cache-control\|age'
Cache-Control: public, max-age=3600
Age: 1247
# Remaining TTL = 3600 - 1247 = 2353 seconds
Video Explanation
Frequently Asked Questions
How long a cached response is considered fresh before it must be revalidated or re-fetched. Set via Cache-Control max-age or s-maxage. The core concept behind cache freshness.
# Set TTL via Cache-Control
Cache-Control: public, max-age=3600 # 1 hour for all caches
Cache-Control: public, s-maxage=86400 # 1 day on CDN, default for browsers
Cache-Control: public, max-age=31536000 # 1 year (immutable assets)
# Nginx: set TTL per location
location /static/ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location /api/ {
add_header Cache-Control "public, max-age=60, s-maxage=300";
}
# Check remaining TTL from CDN response
$ curl -sI https://cdn.example.com/style.css | grep -i 'cache-control\|age'
Cache-Control: public, max-age=3600
Age: 1247
# Remaining TTL = 3600 - 1247 = 2353 seconds
Related CDN concepts include:
- Age Header — A response header added by caches indicating how long (in seconds) the response has been …
- max-age — Specifies the maximum time in seconds that a response is considered fresh. Applies to all …
- s-maxage — Stands for "shared max-age". Overrides max-age specifically for shared caches (CDNs, reverse proxies). Browser caches …
- stale-while-revalidate — Allows caches to serve stale content immediately while fetching a fresh copy in the background. …
- Cache-Control — The primary HTTP header for controlling caching behavior. Tells browsers, CDNs, and proxies whether to …
- ETag — An HTTP response header containing a unique identifier for a specific version of a resource. …