CDN Cache Headers: A Practical Primer
CDN cache headers are the instructions you send with each HTTP response that tell the CDN and the browser how to cache the response. Cache-Control is the primary header. ETag and Last-Modified support validation. Vary controls cache key. A few patterns cover most cases. The wrong headers either prevent caching or cache stale data. The right headers make the difference between a fast page and a slow one.
Written by Yashveer Singh, founder of Yashveer Labs.
What you actually need to know
- Cache-Control is the primary header. Pick the right directives for the content.
- Static hashed assets get aggressive caching with immutable.
- Public API responses can use s-maxage with stale-while-revalidate.
- Per user responses must be private or no-store.
- Vary controls what affects the cache key.
| Content type | Recommended header |
|---|---|
| Hashed static asset | public, max-age=31536000, immutable |
| HTML page (static) | public, s-maxage=3600, stale-while-revalidate=600 |
| Public API response | public, s-maxage=60, stale-while-revalidate=300 |
| Per user API response | private, max-age=0, must-revalidate |
| Sensitive endpoint | no-store |
| Image with content hash | public, max-age=31536000, immutable |
| Unhashed asset | public, max-age=600, stale-while-revalidate=3600 |
The core argument
Cache headers are one of those areas of web engineering where the documentation is dense, the directives are confusing, and the consequences of getting it wrong are slow page loads or cached private data leaking to the wrong users. Most teams never go deep on the topic. They use a framework default and hope it works. Sometimes it does. Often it does not.
The fix is a practical mental model. The static content gets aggressive caching because the URL changes when the content changes. The dynamic content gets short cache or no cache, with the choice depending on whether the response is public or private. The grey area in between uses stale-while-revalidate to give the user fast content with fresh data fetched in the background.
The directives that matter are smaller than the spec suggests. Cache-Control is the primary. ETag or Last-Modified support efficient revalidation. Vary controls the cache key. Surrogate-Control gives CDN specific behavior when needed. Most production traffic fits into a handful of patterns. The patterns are worth memorizing.
The cost of getting this wrong is real. A static asset with no cache header is downloaded on every request. A per user response with public cache leaks to other users. A public response with no cache hits the origin every time. Each is a real production bug. Each is fixable with the right header.
The patterns that cover most cases
| Pattern | Use case | Cache-Control directive |
|---|---|---|
| Immutable hashed asset | JS, CSS, image with content hash in filename | public, max-age=31536000, immutable |
| Static HTML | Marketing pages, blog posts | public, s-maxage=3600, stale-while-revalidate=600 |
| Cacheable API | Public data endpoints | public, s-maxage=60, stale-while-revalidate=300 |
| Private fast | Per user data that can refresh | private, max-age=60 |
| Private no cache | Per user data that must be fresh | private, max-age=0, must-revalidate |
| No store | Tokens, sensitive responses | no-store |
How much does this cost
The cost of getting cache headers right is engineering time, not money. A few hours per surface to pick the right pattern. Some CI testing to confirm the headers are correctly applied. The savings show up in CDN hits, origin load, and user perceived performance.
Features the header strategy must have
- A documented policy for each content type.
- Helper functions in the framework that apply the right headers.
- CI tests that verify the headers on critical endpoints.
- Monitoring for cache hit rate at the CDN.
- A clear path to bust the cache when needed.
- An audit of the Vary headers across the application.
- A plan for revalidation with ETag where appropriate.
Expert opinion
Cache headers are the most underused performance lever in most stacks. The teams that get them right pay a fraction of the origin egress and CDN load that the teams that get them wrong pay. The difference between a fast site and a slow site is often just three lines of header configuration applied with intention. The cost of learning the patterns is one afternoon. The return compounds for the life of the product.
>
Yashveer Singh, founder of Yashveer Labs
How this played out on a real project
A client's marketing site was getting unexpected origin load from a CDN that should have been caching aggressively. Investigation showed the framework was sending Cache-Control: private, max-age=0 by default on all pages because of a server side data fetch. The CDN was treating every request as uncacheable.
We changed the headers. Marketing pages got public, s-maxage=3600, stale-while-revalidate=600. The CDN cache hit rate went from near zero to over ninety percent. The origin load dropped by an order of magnitude. The CDN bill grew slightly because of the increased hits. The origin bill dropped much more. Net cost dropped.
The user perceived performance improved on every page. The stale-while-revalidate meant the user always saw fast content, even when the cache was refreshing in the background.
For more on the related work, see the HTTP caching strategy that most teams get wrong and CDN strategy for a global SaaS in 2026.
Common mistakes teams make
- Using framework defaults without checking what they emit.
- Confusing no-cache with no-store.
- Aggressive cache on per user data. Privacy bug.
- No cache on static hashed assets. Wasted bandwidth.
- Missing Vary headers. Cache key fragmentation or leaks.
- No ETag on revalidating responses. More work than needed.
- No monitoring on cache hit rate at the CDN.
- Treating cache as set and forget. Reality drifts.
A 30 day plan to audit and fix headers
- Week one. Audit current headers on the top endpoints. Identify the issues.
- Week two. Document the policy. Pick patterns for each content type.
- Week three. Apply the headers. Add CI tests.
- Week four. Monitor the CDN. Adjust based on hit rate and freshness.
For more on the related work, read the HTTP caching strategy that most teams get wrong and caching strategies for growing SaaS from none to multi layer. On the broader CDN side, CDN strategy for a global SaaS in 2026 is the natural next read.
Frequently asked
Why this is the work I do
The work in this article is not theoretical for me. It is what I shipped last quarter, last month, and this week. Yashveer Singh, founder of Yashveer Labs. I do not write about things I have not done. I do not pretend to expertise I do not have. If the topic here is the topic you are dealing with, I am the person who has dealt with it. Multiple times. Recently.
Posts that line up with this one.
- Performance Optimization
The HTTP Caching Strategy That Most Teams Get Wrong
The specific HTTP caching headers that control browser and CDN caching -- why most teams misconfigure them and the performance and correctness implications.
- Performance Optimization
The Caching Hierarchy: Browser, CDN, Edge, Application, Database
Every web application has five caching layers. Understanding which one to use for which data is how fast applications stay fast at scale.
- Performance Optimization
The Cost of Over-Caching: Stale Data Stories
Caching solves performance problems. Over-caching creates correctness problems. Here is the taxonomy of stale data bugs and how to prevent them.
- Performance Optimization
Image Optimization at Scale: AVIF, WebP, Responsive Images
Images are the largest contributor to page weight on most web products. Here is the format selection, responsive image, and delivery strategy that cuts load time without manual work.