Skip to content

Lesson 5 of 9 · 26m

Caching at the Edge Without Serving Stale Reality

Designing proxy_cache keys, bypass rules, lifetime policy, invalidation, and observability so caching helps instead of becoming a rumor database.

Caching is not a performance feature you turn on. It is a correctness contract you negotiate with your application. nginx can cache aggressively and correctly, but only when key design, bypass conditions, and backend cache headers align. Otherwise you get the classic incident: one user sees another user's account page because somebody cached a personalized response.

The cache key is the security boundary

A cache entry is shared by every request producing the same key. If the response varies by cookie, authorization, tenant, or A/B experiment and that input is absent from the key, you have built a data leak.

proxy_cache_path /var/cache/nginx/app levels=1:2 keys_zone=appcache:100m max_size=2g inactive=60m use_temp_path=off;
proxy_cache_key "$scheme$request_method$host$request_uri";

Do not casually include cookies in the key unless you truly want per-user cache fragmentation. Prefer bypassing personalized traffic and caching only anonymous-safe responses. Vary-aware behavior in nginx depends on backend discipline: Vary can reduce cacheability and needs testing.

Honor origin intent, then define edge policy

location /assets/ {
    proxy_pass http://static_upstream;
    include snippets/proxy-headers.conf;
    proxy_cache appcache;
    proxy_cache_valid 200 302 30m;
    proxy_cache_valid 404 1m;
    proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
    proxy_cache_lock on;
    add_header X-Cache-Status $upstream_cache_status always;
}

proxy_cache_valid applies when the origin does not provide usable cache-control information. It does not authorize you to ignore explicit private or no-store responses. nginx respects many origin cache controls, but custom bypass logic can accidentally override that intent; review all conditions together.

proxy_cache_lock is one of the highest-value directives in a hot-key outage: while one request populates the cache, others wait instead of stampeding upstream. Combined with proxy_cache_use_stale updating, a popular object can remain available while exactly one refresh happens.

Bypass deliberately, not accidentally

map $http_authorization $skip_cache {
    default 1;
    ''      0;
}

location /api/ {
    proxy_pass http://api_upstream;
    proxy_cache appcache;
    proxy_cache_bypass $skip_cache;
    proxy_no_cache $skip_cache;
    proxy_cache_methods GET HEAD;
}

proxy_cache_bypass controls whether nginx reads an existing entry. proxy_no_cache controls whether the response is written. Confusing them produces the puzzling state where entries populate but never hit, or hits happen but nothing new is stored.

Invalidation is the hard part

Path-based deletion with find is not a cache invalidation strategy. Prefer versioned asset URLs (app.8f31ac.js) and short TTLs for HTML. If hard purging is required, design for it explicitly with a separate cache layout or a controlled purge mechanism in your nginx distribution. Operational reality favors objects that expire quickly and names that change when content changes.

Field notes

  • Expose $upstream_cache_status in a response header during rollout: MISS, HIT, BYPASS, EXPIRED, and UPDATING tell you what really happened.
  • Do not cache Set-Cookie responses for shared users unless you have a precise reason.
  • Test with curl -I twice: first request should populate or miss, second should hit if policy works.

Was this lesson helpful?

Sign in to rate