Lesson 4 of 9 · 27m
Caching at the Edge Without Serving the Wrong Thing
Design cache keys, respect origin intent, use stale content deliberately, and debug hit/miss behavior with evidence rather than hope.
Caching is where web server administration becomes product behavior. A good cache turns an origin outage into a slightly stale website. A bad cache serves one customer's account page to another customer and ends your week. The rule is simple: cacheability is a property of the response and the identity of the request, not a property of how badly you want performance.
Cache only after classifying responses
Before enabling anything, classify routes:
public static assets cache aggressively, versioned filenames
public anonymous pages cache briefly, purge on publish
authenticated pages normally private, never shared
API reads cache only with explicit semantics
mutations and auth never cacheThe dangerous cases are mixed pages: public shell with private fragments, cart counts, CSRF tokens, feature flags, locale negotiation, and A/B experiments. If response identity depends on a cookie, header, or token, it belongs in the cache key or it does not belong in a shared cache.
A disciplined Nginx cache
proxy_cache_path /var/cache/nginx/app levels=1:2 keys_zone=appcache:100m max_size=10g inactive=60m use_temp_path=off;
server {
location /static/ {
proxy_pass http://app_backend;
proxy_cache appcache;
proxy_cache_valid 200 1h;
proxy_cache_valid 404 1m;
proxy_cache_key $scheme$host$uri;
add_header X-Cache $upstream_cache_status always;
}
}$upstream_cache_status is your ground truth: MISS, HIT, STALE, BYPASS, EXPIRED, or UPDATING. Add it temporarily during debugging, then decide whether exposing it publicly is acceptable for your threat model.
Respect origin headers first
Cache-Control exists because the application knows semantics the proxy cannot guess. private, no-store, no-cache, max-age, s-maxage, Vary, and Set-Cookie are not decorative. Nginx will bypass or refuse cache in several unsafe-by-default cases, but overrides like proxy_ignore_headers can disable that safety. Use them only when you can explain exactly which footgun you are accepting.
Stale content is an availability feature
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
proxy_cache_lock on;proxy_cache_lock collapses concurrent misses so one request refreshes the object while others wait. Background update avoids making a user pay the refresh cost. This is how caches absorb deploys and origin hiccups. It is not appropriate for rapidly changing authenticated data, because availability does not trump correctness.
Apache caching is explicit and module-driven
Apache commonly uses mod_cache plus mod_cache_disk:
CacheEnable disk /static/
CacheRoot /var/cache/apache2/mod_cache_disk
CacheDirLevels 2
CacheDirLength 1
CacheIgnoreCacheControl OffDo not enable it globally because a few routes are expensive. In Apache especially, broad caching plus per-directory authorization rules can become difficult to reason about. Start narrow, measure hit ratio and invalidation correctness, then expand.
Purging is part of the design
Every cache entry needs an eviction story: TTL, explicit purge, deploy version change, or object key change. If publishing new content requires an operator to remember a curl command, the cache will eventually serve stale news during the one moment everyone is watching. Prefer versioned asset URLs and short HTML lifetimes over heroic purging.
Field notes
Vary: Cookiemakes shared caching nearly useless for broad cookies; narrow the varying dimensions instead of accepting fragmentation.- Never cache responses containing CSRF tokens unless the token is deliberately identical for all users, which is usually another bug.
- A 200 is not proof of correctness; compare cached response body against the intended tenant and locale.
- Disk caches need inode and disk monitoring. A full cache partition tends to fail at the worst possible traffic peak.
Was this lesson helpful?