Scrivio Interview library
ConceptFoundations7 min read · updated 13 November 2026

A content delivery network (CDN) caches responses at edge locations close to users, so most requests never reach the origin servers. Teams use it for static assets and increasingly for HTML and API responses.

What decides whether a CDN serves a request from its cache, how long it keeps an object, and how do you remove an object early?

A CDN serves from its cache when it holds a fresh object for the request's cache key, usually host, path and query string plus any headers or cookies we choose; anything else that changes the response must be in the key or listed in Vary. How long it keeps an object comes from the origin's Cache-Control (s-maxage for shared caches, max-age for all) or Expires, unless the CDN configuration overrides them, and private or no-store responses are not stored. Stale objects are revalidated with ETag or Last-Modified, and early removal is a purge by URL, prefix or surrogate key, while versioned URLs avoid purges for assets.

Why this question is asked

A CDN is the outermost and often the most effective cache in a system, yet many teams configure it by trial and error and are surprised when users see someone else's language, old content after a release, or a hit ratio far below what they expected. Interviewers ask this to check that we understand the few rules that decide CDN behaviour. In this answer, we will recap what a CDN does, then look at the cache key and Vary, freshness headers, revalidation, purging, and the origin shield that protects the origin.

1A Quick Recap of What a CDN Does

A content delivery network runs many edge locations, points of presence, around the world. A user's request goes to a nearby edge, which answers from its own cache if it can and otherwise forwards the request to our servers, the origin, and usually stores the response for the next user. A hit at the edge saves a long network trip for the user and a request for the origin.

Three questions decide its behaviour: is this request the same as one we have cached (the cache key), is the cached copy still good to use (freshness), and how do we get rid of a copy we know is wrong (purging).

2The Cache Key and Vary

The cache key is what the CDN uses to decide whether two requests are the same. By default it is usually the host, the path and the query string. Two requests with the same key get the same cached response, so anything that changes the response must be part of the key. Most CDNs let us add selected headers or cookies to it, such as a language cookie or a device type.

The standard HTTP way to say this is the Vary response header. Vary: Accept-Encoding tells caches to keep separate copies for gzip and Brotli clients. If the origin returns English or German based on Accept-Language but the key does not include it and the response does not vary on it, the first user's language is served to everyone. The opposite mistake is just as costly: adding the full Cookie header to the key makes nearly every user's request unique, and the hit ratio collapses.

3How Long the CDN Keeps an Object

Freshness comes from the origin's response headers unless the CDN is configured to override them. Cache-Control: max-age applies to every cache, including browsers, while s-maxage applies only to shared caches such as CDNs and takes precedence there. Expires is the older form of the same idea with an absolute date. Responses marked private are meant for the user's own browser only, and no-store must not be stored anywhere, so a CDN that respects origin headers will not cache them.

YAML
Cache-Control: public, max-age=60, s-maxage=600
# browsers keep it 60 s, the CDN keeps it 600 s
Cache-Control: private, max-age=300
# user-specific: the browser may cache it, the CDN must not
Cache-Control: public, max-age=31536000, immutable
# a versioned asset such as /static/app.3f9a1c.js: cache for a year
Headers that decide CDN caching
HeaderEffect at the CDN
Cache-Control: s-maxage=600Fresh for 600 s in shared caches only
Cache-Control: max-age=60Fresh for 60 s everywhere, unless s-maxage is set
Cache-Control: private / no-storeNot stored at the CDN
Vary: Accept-EncodingSeparate copies per encoding
ETag / Last-ModifiedEnables cheap revalidation with 304
Surrogate-Key / Cache-TagLets one purge remove all tagged objects

Splitting max-age and s-maxage is a useful pattern: the CDN can be purged when content changes, but browsers cannot, so browsers get a short lifetime and the CDN a long one.

4Revalidating Stale Objects

When a cached object's lifetime ends, the CDN does not have to download it again. If the origin sent a validator, an ETag (a version identifier for the content) or a Last-Modified date, the CDN asks the origin a conditional question: has this changed?

YAML
GET /products/5001 HTTP/1.1
If-None-Match: "v17"
HTTP/1.1 304 Not Modified # no body: the cached copy is fresh again
ETag: "v17"
Cache-Control: public, s-maxage=600

A 304 response has no body, so revalidation costs the origin a cheap check rather than rendering and sending the whole page. If the content did change, the origin answers 200 with the new body and a new ETag, and the CDN replaces its copy.

5Removing an Object Early

Sometimes we know a cached copy is wrong before it expires. CDNs offer purges by exact URL, by prefix or wildcard, and by surrogate key, also called a cache tag: the origin attaches tags such as product-5001 to responses, and one purge of that tag removes every object carrying it, at every edge. That is the best tool for HTML and API responses that depend on several entities.

Purges are not instant; they take from under a second to several seconds to reach every edge, depending on the provider, and providers rate-limit purge APIs. For static assets there is a better approach: put a content hash or version in the file name, app.3f9a1c.js, and reference the new name from the page. The old file never needs purging, because nobody asks for it anymore, and the new one is a different key.

6Protecting the Origin With a Shield

Each edge location has its own cache, so when an object expires or is purged, hundreds of edges may ask the origin for it at the same moment. An origin shield is a designated mid-tier cache between the edges and the origin: edges ask the shield, and the shield asks the origin once per object. Combined with request collapsing, where a location sends one request upstream no matter how many users are waiting for the same object, it turns an expiry or a purge into a single origin request.

Step throughA purge with an origin shield
UsersEdge locationsOrigin shieldOrigin
thousands of requests for /event/123 after a purge
collapse: one request per location
a few hundred requests, one per location
one request for /event/123
200 OK, s-maxage=60
cached copy to every location
served from the edge

7 messages in order. Play them, or step through them one at a time.

7Follow-up questions

Why do query parameters such as tracking codes hurt the hit ratio?

Because the query string is part of the default cache key. A campaign link with utm_source=newsletter and another with utm_source=social are different keys, even though the page is identical, so each variant misses and fills its own copy. With many campaigns and click ids, a popular page can exist in thousands of copies, each with few hits. The fix is to normalise the key: ignore or strip parameters that do not change the response, or better, list the parameters that do matter and drop the rest from the key.

Should a CDN cache API responses?

Yes, for responses that are the same for many users and tolerate some staleness, such as a product catalogue or event details, with s-maxage of seconds to minutes and stale-while-revalidate. Responses that depend on the user must be private or vary on a small, explicit key, never on the full set of cookies.

What is the difference between a purge and an invalidation?

The words are used loosely, but some providers distinguish removing the object entirely, so the next request goes to the origin, from marking it stale, so the CDN can still revalidate or serve it stale while refreshing. Marking stale is gentler on the origin; check what your provider's purge actually does.

8Conclusion

In this answer, we saw that a CDN serves from cache when it holds a fresh object for the request's cache key, normally host, path and query string, so everything that changes the response must be in the key or in Vary. Freshness comes from Cache-Control, with s-maxage for shared caches and max-age for all, or Expires, unless the CDN overrides them, and private or no-store responses are not cached. Stale objects are revalidated cheaply with ETag or Last-Modified, early removal uses purges by URL, prefix or surrogate key, versioned URLs avoid purges for assets, and an origin shield collapses misses into one origin request per object.

What to say in the interviewAbout 90 seconds aloud

Open with

Three things decide it. First the cache key: by default host, path and query string, and we can add selected headers or cookies. Anything else that changes the response has to be in the key or in a Vary header, or users get the wrong variant, for example the wrong language; but adding all cookies makes every request unique and kills the hit ratio.

Then, if they want more

Second, freshness comes from the origin's Cache-Control, unless the CDN configuration overrides it. s-maxage applies only to shared caches like the CDN, max-age to everyone, Expires is the older absolute form, and private or no-store responses are not stored at the CDN.

When an object goes stale, the CDN revalidates with If-None-Match or If-Modified-Since using the ETag or Last-Modified, and the origin can answer 304 with no body. Third, to remove something early, we purge by URL, by prefix, or by surrogate key, which is a tag the origin attached, so one purge removes every page that shows product 5001.

Purges take seconds to propagate and are rate-limited, so for static assets I prefer versioned file names that never need purging. And an origin shield, a mid-tier cache, plus request collapsing makes sure that after an expiry or purge the origin gets one request per object, not one per edge location.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What decides whether a CDN serves a request from its cache, how long it keeps an object...": https://scrivio-interview-library.netlify.app/architecture/caching-strategies/dda9769bcf/