How to Configure CDN Origin Pull Behavior
Set s-maxage and stale-while-revalidate for shared caches, add an origin shield, purge by fingerprint, and verify hits with Age headers.
On this page
Introduction
A CDN sits between users and the origin, but it only helps if the origin tells it, in headers it understands, what may be cached and for how long. The default behavior without those headers is the worst of both worlds: the CDN may apply an arbitrary default TTL, cache something private, or skip caching something perfectly static. Origin pull is the moment the CDN decides a miss must go to the origin, and every miss costs a full round trip plus origin load. Configuring pull behavior is therefore three linked decisions: what TTL each class of content gets in shared caches, how the CDN refills expiring content without stampeding the origin, and how changes become visible quickly without manual purging. This tutorial walks through the directives that matter for shared caches, the shield tier that concentrates refill traffic, the purge-versus-fingerprint tradeoff, and the headers that prove it all works.
Classify content before writing policy
Cache policy fails when it is written per endpoint under deadline. Start by putting every route into one of three classes. Immutable assets: JavaScript bundles, CSS, fonts, and images whose URLs contain a content fingerprint. These get public, max-age=31536000, immutable, and never change in place. Cacheable documents: HTML pages and public API responses that are the same for every user. These get a modest shared TTL with revalidation. Dynamic or private responses: anything personalized or containing Set-Cookie. These get private, no-store or no-cache, and must bypass the CDN entirely. The classification, not the framework, drives the headers. A route that mixes classes, such as a public page with a personalized greeting, is the classic leak: either the personalization moves to a client-side fetch, or the page joins the dynamic class and gives up caching.
Directives that shape shared caches
Shared caches read the same Cache-Control header as browsers but honor additional directives. s-maxage overrides max-age for shared caches only, which enables a split policy: a document can carry max-age=60 for browsers and s-maxage=600 for the CDN, refreshing edges ten times less often than browsers. stale-while-revalidate lets the cache serve an expired copy while it refetches in the background, which turns expiry cliffs into smooth refreshes and removes the latency spike on popular objects. stale-if-error keeps serving stale content when the origin is down, which is the cheapest origin-failure resilience available.
HTTP/1.1 200 OK
Cache-Control: public, max-age=60, s-maxage=600, stale-while-revalidate=600, stale-if-error=86400
Vary: Accept-Encoding
One caution: some CDNs support these directives only partially or behind settings, so verify against the specific provider rather than assuming RFC 9111 compliance. And remember that a response carrying Set-Cookie is often treated as uncacheable regardless of what Cache-Control says, which is a frequent surprise for pages that set analytics cookies at the origin.
Add an origin shield
Without a shield, every edge POP that expires a popular object independently refetches it from the origin. Ten POPs means ten refetches for one logical object expiry, and a traffic spike multiplies further. An origin shield designates one POP as the parent for a region: edges fetch from the shield, and only the shield pulls from the origin. Refill traffic collapses to one request per object expiry region-wide, and origin load becomes predictable even as the CDN footprint grows. Most providers expose this as a single setting with a region choice; place the shield near the origin, since the shield-to-origin hop is the one that matters for latency. The shield also concentrates the effectiveness of stale-while-revalidate: the shield serves stale while it refetches once, so edges never see the miss at all. Enable it before optimizing anything else, because it changes the math of every other cache decision.
Purge versus fingerprint
Making changes visible is the other half of cache policy. The fragile approach is purging: on every deploy, purge the CDN for changed paths. It works, but it depends on the deploy process remembering, on purge API latency, and on rate limits; and a missed purge leaves stale content until TTL expiry. The robust approach is fingerprinting: the asset URL embeds a content hash, index.a3f9c2.js, and the HTML that references it changes. Old URLs stay cached forever, which is harmless because nothing will request them again, and new URLs are cold exactly once. Reserve purge for emergencies: removing sensitive content, fixing a bad cached error page, or forcing a document refresh ahead of schedule. When purge is genuinely needed, prefer purging by URL or tag rather than purge-everything, which evicts the entire cache and converts one small change into a full-origin stampede.
Verify with response headers
The cache tells you what it did in response headers, and reading them should become a reflex. Age reports how long the object has been in the cache; a large Age with a hit means the TTL policy is doing its job. Most providers add a proprietary header such as X-Cache, Cf-Cache-Status, or X-Cache-Status whose values distinguish HIT, MISS, EXPIRED, BYPASS, and REVALIDATED.
for i in 1 2 3; do
curl -s -o /dev/null -w 'run=%{time_total}s ' \\
-D - https://example.com/api/catalog | grep -iE '^(x-cache|cf-cache-status|age):'
done
A healthy sequence shows MISS then HIT with the Age growing between requests. EXPIRED followed by a fast response indicates stale-while-revalidate working. BYPASS on something you expected cached means a rule, a cookie, or a missing header disqualified the response, and that is the first thing to investigate. Run this check from multiple regions, because cache state is per POP and a single-region test proves nothing about global behavior.
Monitor hit ratio, not just uptime
CDN caching degrades quietly: a framework upgrade adds a cookie to responses, a middleware change drops a Vary header, and hit ratio slides over weeks while origin CPU climbs. Monitor cache hit ratio as a first-class metric, segmented by route class, with an alert on sustained decline. Monitor origin request rate as the mirror image: any change that increases origin traffic is either a real traffic increase or a cache regression, and correlating the two distinguishes them. Track upstream latency separately from edge latency, because users stay fast while the origin quietly absorbs the difference, until the day it cannot. These three metrics, trended, give early warning of nearly every caching failure mode this tutorial describes, weeks before users open tickets.
Common pitfalls
- One TTL for everything. A single max-age forces a compromise between stale assets and overloaded origins; classify content instead.
- Set-Cookie on cacheable routes. Many caches refuse to store the response no matter what Cache-Control says.
- Purge-everything as a deploy step. It converts each release into a full-origin stampede; fingerprint instead.
- No shield with many POPs. Each edge refetches independently and origin load scales with CDN footprint.
- Testing from one region. Cache state is per POP; verify from several or verify nothing.
- Assuming directive support. stale-while-revalidate and stale-if-error support varies by provider; confirm behavior on your CDN, not the RFC.
Conclusion
Origin pull behavior is configured at the origin, not at the CDN dashboard. Classify routes into immutable, shared-cacheable, and private, and emit s-maxage with stale-while-revalidate and stale-if-error for the middle class so the CDN refreshes smoothly and survives origin failures. Add an origin shield so refill traffic concentrates instead of multiplying with your CDN footprint. Prefer fingerprinted URLs over routine purging, and keep purge for emergencies by URL or tag. Verify with Age and cache-status headers from multiple regions, and monitor hit ratio with origin request rate as its mirror. The result is a CDN that offloads the origin predictably and fails gracefully when the origin has a bad day.
Sources: RFC 9111: HTTP caching, Cloudflare: Cache-Control behavior, MDN: Cache-Control header.
FAQ
- What is the difference between max-age and s-maxage?
- max-age applies to every cache including the browser; s-maxage applies only to shared caches such as CDNs. Setting s-maxage higher than max-age lets the CDN serve a copy longer while browsers refetch sooner.
- Should I purge on every deploy?
- Purge-only workflows are fragile because they rely on remembering. Prefer fingerprinted URLs for assets that change, and reserve purge for emergencies such as removing sensitive content quickly.
- Why does my CDN ignore my Cache-Control header?
- Most CDNs apply their own default TTL when the origin header is missing or when a rule overrides it. Also verify nothing strips the header on the path, and that the response is not disqualified by Set-Cookie, which many caches refuse to store.
Report a problem with this articleOpens a pre-filled issue on GitHub.