CDN Time-To-Live (TTL)

Balancing server load against serving hopelessly outdated data.

The idea

A CDN edge node stores a copy of your files so it can serve them quickly to users nearby. But it needs to know when to delete that copy and ask your server for a fresh one. This duration is called the TTL (Time-To-Live). If the TTL is 1 year, your server does no work, but users might see a 1-year-old homepage. If the TTL is 0 seconds, users always see fresh data, but your server gets hammered.

Step 1: A user requests the homepage. The CDN fetches it and caches it with a TTL of 10s.

How it works (Cache-Control Headers)

Your origin server dictates the TTL to the CDN by returning HTTP headers (specifically Cache-Control). The CDN reads this header and starts a countdown timer for that specific file.

# Setting TTLs in a web server response

@app.route('/index.html')
def homepage():
    response = make_response(render_template('index.html'))
    # Cache for 60 seconds. High traffic, but updates quickly.
    response.headers['Cache-Control'] = 'public, max-age=60'
    return response

@app.route('/logo.png')
def logo():
    response = send_file('logo.png')
    # Cache for 1 year! (31536000 seconds)
    # We will never change the logo without changing its filename (Cache Busting)
    response.headers['Cache-Control'] = 'public, max-age=31536000, immutable'
    return response

Cost

Choosing a TTL is a pure trade-off between Freshness and Origin Load. A 5-minute TTL on an API endpoint might reduce your database costs by 95%, but means users won't see their own profile updates for 5 minutes.

Watch out for