Frontend System Design · Lesson 28 of 86 · 4 min
CDN Cache Behavior and Invalidation
In an interview, you need to prove you can control the cache, not just use a CDN. This means talking specifics about HTTP headers and invalidation trade-offs.
When a user requests an asset, your CDN has to decide: serve a cached copy or fetch a new one from your origin server? You control that decision. Your job is to configure caching to be as aggressive as possible for performance, while still ensuring users get updates promptly.
How to Control the Cache with HTTP Headers
You instruct CDNs and browsers on how to behave using HTTP response headers set by your origin server. The most important one is Cache-Control.
Cache-Control is a directive to all caches between your server and the user.
public: Allows any cache (CDN, proxy, browser) to store the response. This is essential for CDN caching.max-age=<seconds>: The time a response is considered fresh for all caches, including the user's browser.s-maxage=<seconds>: Same asmax-age, but only for shared caches (like a CDN). It overridesmax-agefor the CDN, letting you set different policies for the CDN and the browser.immutable: A hint that the file will never change. Use this only on versioned assets (cache-busted files). It prevents the browser from even trying to revalidate.
Other useful headers include ETag and Last-Modified. These are validators. When a cache holds a stale asset, it can send the ETag value back to the origin. If the asset hasn't changed, the origin replies with a 304 Not Modified status and an empty body, saving bandwidth.
This flow determines whether a request is a "hit" or a "miss".
Here is how you'd set these headers in a Node.js/Express app serving static files. Notice the different strategies for versioned assets versus the main index.html.
// server.ts (using Express)
import express from 'express';
import path from 'path';
const app = express();
const staticOptions = {
setHeaders: (res, filePath) => {
// Versioned assets get a long cache time.
// The 'immutable' directive tells browsers the content will never change.
if (/\.[0-9a-f]{8,}\./.test(filePath)) { // Matches files like 'app.a1b2c3d4.js'
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
} else if (filePath.endsWith('index.html')) {
// The HTML file should be revalidated more often to pick up new asset URLs.
// 's-maxage' lets the CDN cache for 1 hour, but browsers must revalidate.
res.setHeader('Cache-Control', 'public, max-age=0, must-revalidate, s-maxage=3600');
}
}
};
app.use(express.static(path.join(__dirname, 'public'), staticOptions));
Choosing Your Invalidation Strategy
When you deploy a change, you need to force the CDN to serve the new version. You have three ways to do this, but one is clearly superior for frontend assets.
1. Versioned URLs (Cache Busting)
This is the best practice and should be your default. Your build process generates a unique filename for each asset based on its content, like main.d9e8f7a0.js. The URL is now different, so the CDN sees it as a brand-new asset and fetches it from the origin.
This strategy is bulletproof. You can set extremely long cache times (max-age=31536000) because you know the content at a given URL will never change. The old asset is left to expire naturally.
This is the answer interviewers want to hear for standard asset updates. It demonstrates you understand the build pipeline and prefer reliable, automated solutions over manual intervention.
2. Explicit Purging Most CDNs provide an API to forcibly remove a file from their cache globally. This is also called invalidation.
Use this for emergencies only. For example, if you accidentally deploy a critical bug or expose sensitive data. Purging is not a deployment strategy. Triggering a purge for every deploy is a sign of a broken process.
The trap: Purging can cause a "thundering herd" problem. When the asset is removed everywhere, the next wave of user requests all miss the cache and hit your origin server simultaneously, potentially overloading it.
3. TTL Expiration
This is the passive approach. You simply wait for the max-age or s-maxage timer to run out. On the next request, the CDN will revalidate the asset with your origin.
This is too slow and unpredictable for deploying updates to your application code. It's only acceptable for content where freshness isn't critical, like a company logo that changes once a year.
The index.html Dilemma
Your index.html file is a special case. It's the entry point to your single-page application and contains the references to your versioned JavaScript and CSS files.
If you cache index.html for a year, users will never get the new asset URLs when you deploy. If you don't cache it at all, every single navigation to your site hits your origin server, defeating a major purpose of the CDN.
This is a classic trade-off question. Here’s a solid strategy:
- For the CDN: Use a moderate TTL.
s-maxage=3600(1 hour) is a reasonable starting point. This ensures most users get a cachedindex.htmlfrom the CDN edge. - For the Browser: Force revalidation on every visit.
max-age=0, must-revalidatetells the browser it can't use its local copy without checking with the CDN first.
This combination ensures that when a user visits your site, their browser asks the CDN for index.html. The CDN likely has a copy less than an hour old and serves it. When you deploy, you just have to wait for the CDN's cache (1 hour in this case) to expire, or you can run a targeted purge for just index.html. This one-file purge is acceptable and avoids the thundering herd problem.
Try it
Open the network tab in your browser's developer tools. Visit a major web application like GitHub, Stripe, or Vercel. Find the main JS bundle and the initial HTML document. Inspect their response headers and identify the Cache-Control policy. Can you explain why they chose those specific directives?
Next: We'll discuss the trade-offs between Global State vs Local Component State.
Lock it in — apply this lesson under interview conditions.