What is Nginx caching and how does proxy_cache work?
Understand Nginx caching and how proxy_cache works: cache zones, keys, TTLs, HIT vs MISS and stampede prevention, with config and interview questions.
Expected Interview Answer
Nginx caching stores upstream responses on disk so repeated requests are served directly by Nginx without hitting the backend; proxy_cache does this for a reverse proxy by keying responses, honoring cache headers, and returning cached copies until they expire.
You define a shared cache zone with proxy_cache_path (disk location, memory zone for keys, size limits, and eviction) and activate it in a location with proxy_cache. Nginx builds a cache key (default $scheme$proxy_host$request_uri), and on a MISS it fetches from upstream, stores the response per proxy_cache_valid or the response's Cache-Control/Expires, then serves subsequent HITs from disk. Features like proxy_cache_use_stale, proxy_cache_lock, and proxy_cache_background_update improve resilience and prevent stampedes when many clients request an expired key at once.
- Dramatically reduces backend load
- Lower latency for repeated requests
- Serves stale content during upstream outages
- Prevents cache stampedes with locking
- Fine-grained control via cache keys and TTLs
AI Mentor Explanation
proxy_cache is like a scoreboard operator keeping the latest computed run rate on the board instead of recalculating for every spectator who asks. The first query computes it (a MISS to the scorers), the result is posted for a set number of overs (TTL), and everyone else reads the board (HITs). If the scorers are momentarily unreachable, the last posted figure still stands (serve-stale).
Step-by-Step Explanation
Step 1
Define a cache zone
Use proxy_cache_path to set the disk path, keys_zone memory area, max_size, and inactive eviction time.
Step 2
Activate caching
In the proxy location, set proxy_cache <zone> and optionally proxy_cache_key to control what identifies a cached entry.
Step 3
Set validity periods
Use proxy_cache_valid per status code, or let Cache-Control/Expires from upstream drive TTLs.
Step 4
Handle MISS vs HIT
On a MISS Nginx fetches and stores the response; on a HIT it serves from disk. Add X-Cache-Status to observe it.
Step 5
Add resilience
Enable proxy_cache_use_stale, proxy_cache_lock, and background_update to survive outages and prevent stampedes.
What Interviewer Expects
- Understanding proxy_cache_path and keys_zone
- Knowing the default cache key
- Distinguishing MISS, HIT and stale states
- Using proxy_cache_valid vs upstream headers
- Explaining cache stampede prevention with locking
Common Mistakes
- Caching responses with cookies or auth without bypass rules
- Ignoring Cache-Control headers from upstream
- Not setting proxy_cache_key for varied content
- Forgetting max_size, filling the disk
- No stampede protection under high traffic
Best Answer (HR Friendly)
“Nginx caching saves copies of responses so it can answer repeat visitors itself instead of asking the backend every time. proxy_cache stores these copies on disk with a time limit, which makes the site much faster and lightens the load on the application servers.”
Code Example
http {
proxy_cache_path /var/cache/nginx keys_zone=my_cache:10m
max_size=1g inactive=60m use_temp_path=off;
server {
listen 80;
location / {
proxy_pass http://app_backend;
proxy_cache my_cache;
proxy_cache_key $scheme$proxy_host$request_uri;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
# resilience
proxy_cache_lock on;
proxy_cache_use_stale error timeout updating;
add_header X-Cache-Status $upstream_cache_status;
}
}
}Follow-up Questions
- What is the default proxy_cache_key and when should you change it?
- How does proxy_cache_lock prevent a cache stampede?
- How do you bypass the cache for logged-in users?
- What does proxy_cache_use_stale do during an outage?
- How do you purge or invalidate a cached entry?
MCQ Practice
1. Which directive defines the on-disk cache location and shared memory zone?
proxy_cache_path sets the disk directory, the keys_zone memory area, size limits and eviction; proxy_cache just activates a defined zone.
2. What does proxy_cache_lock on do?
It ensures a single request fills an expired key while others wait, preventing many simultaneous upstream fetches (a stampede).
3. How can you serve cached content when the upstream is down?
proxy_cache_use_stale lets Nginx return a stale cached response on upstream errors or timeouts, improving resilience.
Flash Cards
What is the default proxy_cache_key? — $scheme$proxy_host$request_uri — override it to vary cache by headers, args, or user state.
What does proxy_cache_path do? — Defines the cache's disk path, keys_zone memory, max_size, and inactive eviction window.
MISS vs HIT? — MISS: Nginx fetched from upstream and stored it. HIT: Nginx served the response from cache.
How to prevent a cache stampede? — proxy_cache_lock so one request repopulates an expired key while others wait or get stale content.