CDN Caching Strategies
Table of Contents + â
You already know a CDN keeps copies of your files close to users so pages load fast. But hereâs the catch:
- A CDN is only useful if it caches the right things.
- And itâs only useful if those copies stay fresh, not stale and old.
- Cache the wrong stuff, or hold onto an old copy too long, and the CDN actually hurts you.
So in this lesson we go one level deeper than the intro. Weâll look at how a CDN decides what to keep, how long to keep it, and how you tell it âhey, this changed, drop the old copy.â Thatâs what CDN caching strategies are all about.
đŻ The Two Hard Questions
Every caching decision on a CDN comes down to two questions, and once you see them clearly the rest is easy.
- What should I cache? Some content is safe to copy everywhere, like your logo. Some content is dangerous to copy, like a userâs private account page. Getting this wrong leaks data or wastes the cache.
- When should I refresh it? A cached copy can go out of date the moment you change the original. So you need a plan for how long a copy lives and how you replace it.
So keep these two in your head the whole way down. Everything else, TTL, purging, cache busting, push vs pull, is just a tool for answering one of them.
âł TTL and Cache-Control
Letâs start with freshness, because thatâs where most people get bitten. The main tool here is something called TTL.
- TTL stands for Time To Live. Itâs just how long an edge server is allowed to keep a copy before it has to check the origin again.
- A short TTL means fresher content, but more trips back to the origin. A long TTL means faster serving, but the copy might drift out of date.
So how does the edge know the TTL for a file? The origin tells it, using a special HTTP header.
- A header is just a little piece of extra info attached to a response, like a label on a package.
- The header that controls caching is called
Cache-Control. When the origin sends a file, it attaches this header to say how long the file may be cached.
Hereâs what that header looks like coming back from the origin.
Cache-Control: max-age=86400Letâs read that one line:
max-ageis the TTL in seconds. So86400seconds is one full day.- This tells every edge server, âyou can keep this copy for a day before you bother me again.â
There are a few other values on Cache-Control worth knowing, because youâll use them all the time:
no-storemeans âdonât cache this at all, ever.â You use this for private, sensitive responses.no-cacheis a bit of a trap name. It doesnât mean âdonât cache.â It means âyou can keep a copy, but check with me before serving it.âprivatemeans âonly the userâs own browser may cache this, not the shared CDN.â Good for personalized pages.publicmeans âanyone, including the CDN, may cache this.â Good for shared static files.
So when a request hits the edge, the edge runs a simple check. Is my copy still fresh, meaning still inside its TTL? If yes, serve it. If no, go ask the origin. Hereâs that decision as a picture.
The origin is the boss of caching
Notice that the origin decides the rules by sending Cache-Control. The CDN just obeys what the header says. So if your files are caching wrong, the fix usually isnât in the CDN dashboard. Itâs in the headers your origin server sends.
đ Cache Invalidation and Purging
TTL handles the normal case where a copy just slowly expires. But sometimes you change a file right now and canât wait for the TTL to run out. Thatâs where invalidation comes in.
- Cache invalidation means telling the CDN to drop a cached copy before its TTL is up, because the content changed.
- The action of forcing the edge to throw away a copy is called a purge. People say âpurge itâ and âinvalidate itâ to mean the same thing.
So the flow is simple:
- You deploy a change to the origin, say you fixed a wrong price on a page.
- You purge that file on the CDN.
- The edge drops its old copy. The next request becomes a miss, so the edge fetches the fresh version and starts serving that.
Now hereâs the trade-off, and itâs the part interviewers like to poke at:
- Purging works, but itâs not free. Right after a purge, lots of users hit the edge and find nothing there, so a flood of misses goes back to the origin all at once.
- If you purge constantly, youâre basically defeating the cache. The origin gets overloaded, which is the exact thing the CDN was supposed to prevent.
So purging is a tool for the occasional âoops, this really changedâ moment, not your everyday update plan. For everyday updates thereâs a cleaner trick, which is next.
đˇď¸ Cache Busting
Purging is you chasing the CDN saying âforget the old copy.â Cache busting flips it around so you never have to chase at all.
- Cache busting means changing the fileâs URL or name whenever its contents change.
- A new name looks like a brand new file to the CDN, so the edge has never seen it and fetches the fresh one automatically. No purge needed.
So instead of editing app.js in place and hoping the cache lets go, you ship a new name:
<!-- Old version --><script src="/app.v1.js"></script>
<!-- After a change, the name changes too --><script src="/app.v2.js"></script>Letâs see why this is so clean:
- The HTML page points to
app.v2.jsnow. Thatâs a name the CDN has never cached, so the edge grabs the new file straight away. - The old
app.v1.jscan keep a super long TTL, because it will never change again. A file that never changes is the perfect thing to cache forever. - In real build tools the version is usually a hash of the fileâs contents, like
app.3f9a2c.js, so the name changes automatically every time the file does.
So cache busting lets you have the best of both worlds. You cache static files aggressively with a long TTL, and updates show up instantly, because an update is just a new filename.
âŹď¸âŹď¸ Push vs Pull CDNs
So far weâve assumed the CDN gets content from the origin only when someone asks for it. Thatâs actually just one of two models, and knowing both is a common interview point.
- A pull CDN fetches content from your origin on the first request, then caches it. Itâs lazy. It only grabs a file the moment someone actually wants it.
- A push CDN is the opposite. You upload your content to the CDN ahead of time, before any user asks for it. You push it up there yourself.
Hereâs the easy way to feel the difference:
- With pull, the first user to request a file pays a small price, because itâs a miss and the edge has to go fetch it. After that everyone gets a fast hit. You barely manage anything.
- With push, you do the work upfront, so even the very first user gets a hit. But now you are responsible for putting files there and updating them.
| Pull CDN | Push CDN | |
|---|---|---|
| How content arrives | Edge fetches from origin on first request | You upload it to the CDN ahead of time |
| First request | A miss, so itâs a little slow once | Already there, so itâs fast |
| Who manages it | Mostly automatic, easy setup | You manage uploads and updates |
| Best for | Lots of small files, sites that change often | Large files like big videos or downloads |
So which do you pick? Pull is the default for most websites because itâs simple and self-managing. Push makes sense when you have a few big files, like a giant video or installer, where you donât want any user stuck waiting on that first slow miss.
đ§Š Static vs Dynamic Content
Now back to the other hard question: what should you even cache? This comes down to whether content is static or dynamic.
- Static content is the same for everyone and doesnât change often. Images, CSS, JavaScript, fonts, videos. The same logo goes to every single user.
- Dynamic content is different per user or changes constantly. A personal account page, a shopping cart, a live score. Everyone needs a different answer.
So the rule of thumb splits cleanly:
- Cache static content aggressively. Long TTL, cache busting for updates. Thereâs no risk, because the file is identical for everyone.
- Be careful with dynamic content. Either use a very short TTL, or donât cache it at all with
no-store.
But hereâs a nice middle ground people forget about:
- Some dynamic content is the same for everyone but still changes a bit, like a news homepage that updates every minute. You can cache that for a short TTL, say 30 or 60 seconds. Even one minute of caching takes a huge load off the origin during a spike.
- Truly personalized content, the stuff unique to one logged-in user, should stay off the shared cache. Mark it
privateorno-store.
Never cache one user's private page on the shared CDN
If the edge accidentally caches Alexâs account page and then serves that same copy to another user, youâve just leaked Alexâs private data to a stranger. Always mark personalized responses as private or no-store so the shared CDN never holds them.
⥠Good Defaults
If you remember nothing else, remember this pair of defaults. They cover the vast majority of real sites.
- For static assets (images, CSS, JS, fonts): use a long TTL, like a year, plus cache busting via versioned filenames. The file gets cached everywhere forever, and updates ship as new names. Fast and always correct.
- For dynamic or personalized content: use a short TTL or no caching at all. Mark per-user responses
privateorno-storeso the shared cache never touches them.
So a typical pair of headers looks like this. A long-lived versioned asset says keep me forever:
Cache-Control: public, max-age=31536000And a private user response says donât cache me at all:
Cache-Control: private, no-storeThatâs 31536000 seconds, which is a year, for the safe static file, and a flat âdonât store this anywhere sharedâ for the private one. Start from these two defaults and youâll rarely go wrong.
â ď¸ Common Mistakes and Misconceptions
A few caching habits cause real outages and data leaks. Letâs clear them up.
- âJust cache everything forever.â Sounds fast, but the moment you update anything, every edge keeps serving the old copy and you canât easily fix it. Long TTLs are only safe with cache busting.
- âNever cache anything, to be safe.â Now every request goes all the way to the origin, so the CDN does nothing and your site is slow and your origin is overloaded. Youâve thrown away the whole point.
- âMy deploy is live, so users see the new version.â Not unless you purged or used a new filename. The edge happily serves the old cached copy until its TTL expires. Forgetting to purge after a deploy is the classic âwhy am I still seeing the old siteâ bug.
- âItâs fine to cache the logged-in homepage.â No. If itâs personalized, the CDN might serve one userâs page to another. Personalized pages must be
privateorno-store.
đ ď¸ Design Challenge
Try this one yourself to lock the ideas in.
Alex runs a news site that gets sudden traffic spikes when a big story breaks. Design the caching strategy:
- The site logo, CSS, and JavaScript bundles? Static, so cache them a year with versioned filenames.
- The article pages, which are the same for every reader but get edited as news develops? Shared but changing, so a short TTL like 60 seconds works, and a purge when a major correction goes out.
- A logged-in readerâs saved-articles list and account settings? Personalized, so mark them
privateorno-storeand keep them off the shared cache.
Then ask yourself: when Alex pushes a corrected article, how do they make readers stop seeing the wrong version fast? Walk through the short TTL, a purge, and why they wouldnât just purge on every tiny edit. That reasoning is exactly what a system design interview wants to hear.
đ§Š What Youâve Learned
You can now explain how a CDN decides what to cache and how to keep it fresh. Hereâs what youâve picked up.
- â
TTL is how long the edge keeps a copy, and the origin sets it with the
Cache-Controlheader (max-age,no-store,private,public). - â Cache invalidation, or purging, drops a cached copy on demand, but purging too often hammers the origin with misses.
- â Cache busting changes a fileâs name when it changes, so updates ship instantly with no purge needed.
- â A pull CDN fetches from the origin on first request; a push CDN holds content you upload ahead of time.
- â Cache static content aggressively with long TTLs and cache busting, and treat dynamic or personalized content with short TTLs or no caching.
- â Good defaults: long TTL plus cache busting for static assets, and short or no caching for dynamic and private responses.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does TTL control on a CDN, and who sets it?
Why: TTL is how long the edge keeps a copy before re-checking, and the origin sets it by sending the Cache-Control header.
- 2
What is the main difference between purging and cache busting?
Why: Purging forces the edge to drop a copy right away, while cache busting changes the file's name so the CDN fetches it as a new file with no purge needed.
- 3
Why is purging too often a bad idea?
Why: Right after a purge, many requests become misses and hit the origin together, so purging constantly overloads the origin and defeats the cache.
- 4
How should you handle a logged-in user's private account page on a shared CDN?
Why: Personalized pages must be marked private or no-store, or the shared CDN might serve one user's page to another user.
đ Whatâs Next?
Youâve now got the deeper caching picture, so letâs connect it back to the bigger story.
- CDN Explained is the intro this lesson builds on, covering edge servers, hits and misses, and why CDNs exist.
- Introduction to Caching zooms out to the caching idea that powers CDNs, browsers, databases, and a whole lot more.
Get those two down and youâll have a solid grip on the caching and CDN fundamentals every system design interview leans on.