Balancing server load against serving hopelessly outdated data.
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.
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
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.
Cache-Control: public on a page that contains private user data (like /my-account). The CDN will cache Alice's bank details and serve them to Bob! Use Cache-Control: private so only the user's local browser caches it, not the CDN.index.html will permanently break your website. You will not be able to deploy new code to users until you figure out how to run a manual CDN invalidation.