Lesson 3 of 9 · 26m
Vary, Cache Keys, Cookies, and CDN Behavior
Model what actually distinguishes one response from another across browsers, shared proxies, CDNs, compression, and authenticated traffic.
Most cache poisoning stories are really cache-key stories. The response was correct for someone, under some encoding, language, device type, cookie state, or tenant. Then a cache reused it for someone else because the key did not represent those inputs. If you cannot write down the cache key, you do not understand what you are caching.
The real key is bigger than the URL
A URL is only the visible part. Depending on configuration, a cache key may also include host, query parameters, selected headers, cookies, compression support, and whether the request was authenticated. CDNs often normalize or ignore query strings unless told otherwise.
host: www.sudosecurity.org
path: /pricing
query: plan=team
accept-encoding: br
accept-language: en-US
cookie: session=...Should all of those distinguish the object? Almost certainly not. But whichever ones should must be explicit, stable, and reflected in behavior.
Vary is an instruction, not a safety net
Vary tells caches which request headers influence the selected representation:
vary: accept-encoding, accept-languageIf content differs by language but Vary: Accept-Language is absent, a shared cache may serve French content to an English user. If Vary is too broad, hit ratio collapses. Vary: User-Agent is notorious because every browser version and crawler can create a separate cache entry.
Cookies poison shared caching by default
Many CDNs and reverse proxies conservatively bypass cache when a Cookie or Authorization header exists. Others require explicit rules. Both behaviors can be correct; silent disagreement is the problem. Define separate paths:
location /assets/ {
proxy_cache static_cache;
proxy_ignore_headers Set-Cookie;
}
location /api/account/ {
proxy_cache off;
add_header Cache-Control "private, no-store" always;
}The first block can cache public immutable assets. The second refuses to cache account state. Do not let a wildcard rule accidentally span both.
CDN configuration is part of application design
At a CDN edge, decide deliberately:
static content include URL path, ignore cookies, long TTL
public API include query whitelist, no auth, short TTL
private API bypass shared cache
HTML shell often no-cache or short TTL with revalidationPurge capability matters too. A cache you cannot invalidate quickly is a liability during incident response. Know whether purges are by URL, tag, host, prefix, or surrogate key, and how long propagation really takes.
Query strings and cache-busting
Cache-busting with random query parameters works because it changes the key. It also destroys reuse when overused. Content-hashed filenames are better:
<script src="/assets/app.9f21c7.js"></script>The file name changes only when bytes change. That gives you immutable long-lived caching without lying to caches.
Field notes
- Test with compressed and uncompressed clients;
Accept-Encodingbehavior still causes real-world cache splits. - Never cache a response solely because the request had no cookie. New cookies can appear later under the same URL.
Vary: Cookieis usually a hit-ratio disaster and often a sign the route design is wrong.- Query parameter ordering and casing may or may not be normalized. Do not assume.
- Review CDN cache rules after every routing refactor; path boundaries are security boundaries.
Was this lesson helpful?