Cache validity rules in Nginx - Time & Space Complexity
When nginx checks if a cached response is still good to use, it runs some rules to decide.
We want to know how the time it takes to check these rules grows as the number of cache entries grows.
Analyze the time complexity of the following nginx cache validity check snippet.
proxy_cache_path /data/nginx/cache keys_zone=mycache:10m;
server {
location / {
proxy_cache mycache;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout updating;
}
}
This snippet sets rules for how long cached responses stay valid and when nginx can use stale cache.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking cache entries against validity rules for each incoming request.
- How many times: Once per request, nginx looks up the cache key and checks timestamps and status codes.
Each request triggers a cache lookup and validity check, which depends on the cache key, not the total cache size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 cache entries | 1 lookup and validity check |
| 100 cache entries | 1 lookup and validity check |
| 1000 cache entries | 1 lookup and validity check |
Pattern observation: The time to check cache validity stays about the same no matter how many entries exist.
Time Complexity: O(1)
This means nginx checks cache validity in constant time for each request, regardless of cache size.
[X] Wrong: "Checking cache validity takes longer as the cache grows because nginx scans all entries."
[OK] Correct: nginx uses a fast lookup by cache key, so it does not scan all entries but directly finds the needed one.
Understanding how cache validity checks scale helps you explain efficient caching in real systems.
"What if nginx had to scan multiple cache keys for a request? How would the time complexity change?"
Practice
proxy_cache_valid directive in nginx control?Solution
Step 1: Understand the purpose of
This directive sets the time duration for which cached responses are considered fresh and can be served without revalidation.proxy_cache_validStep 2: Differentiate from other cache settings
It does not control cache size, access permissions, or file formats, only the validity time of cached content.Final Answer:
How long cached content is considered fresh and valid -> Option DQuick Check:
Cache validity = duration freshness [OK]
- Confusing cache size with cache validity
- Thinking it controls access permissions
- Assuming it changes file storage format
Solution
Step 1: Recall the syntax format
The correct syntax isproxy_cache_valid <code> <time>;where status code comes first, then time.Step 2: Check each option
proxy_cache_valid 200 10m; matches the correct order: status code 200 followed by 10 minutes (10m). Others have wrong order or extra symbols.Final Answer:
proxy_cache_valid 200 10m; -> Option CQuick Check:
Status code then time = correct syntax [OK]
- Swapping time and status code order
- Adding extra symbols like '='
- Omitting status code when required
proxy_cache_valid 200 5m;
proxy_cache_valid 404 1m;
What happens when a cached 404 response is requested after 2 minutes?
Solution
Step 1: Understand cache validity times
404 responses are cached for 1 minute. After 2 minutes, the cached 404 is expired.Step 2: Determine behavior after expiration
Expired cache triggers revalidation or fetching fresh response, so cached 404 is not served directly.Final Answer:
The cached 404 is considered expired and revalidated -> Option BQuick Check:
Cache expired after 1m, so revalidation needed [OK]
- Assuming cache serves expired content
- Thinking cache clears automatically after expiry
- Confusing 404 cache with 200 cache time
proxy_cache_valid 200 10m; but cached responses are never served from cache. What is the likely error?Solution
Step 1: Check cache zone setup
Without defining and enabling a cache zone, caching won't work even ifproxy_cache_validis set.Step 2: Validate other options
Time format '10m' is valid, status code 200 is correct, and directive can be inside server or location blocks too.Final Answer:
The cache zone is not defined or enabled -> Option AQuick Check:
Cache zone missing disables caching [OK]
- Ignoring cache zone definition
- Misreading time format as invalid
- Assuming directive location is restricted
Solution
Step 1: Set specific status codes first
HTTP 200 and 404 have explicit cache times set correctly in all options.Step 2: Use no status code to match all other status codes
In nginx,proxy_cache_valid time;without a status code applies to all responses not matched by prior specific directives.Step 3: Evaluate options
proxy_cache_valid 200 15m; proxy_cache_valid 404 2m; proxy_cache_valid 5m;(proxy_cache_valid 200 15m; proxy_cache_valid 404 2m; proxy_cache_valid 5m;) is correct as the last line sets default for unmatched codes. proxy_cache_valid 200 15m; proxy_cache_valid 404 2m; proxy_cache_valid 0 5m;'s '0' doesn't match valid statuses. 'any' (B) and '1' (C) are invalid for default.Final Answer:
proxy_cache_valid 200 15m;\nproxy_cache_valid 404 2m;\nproxy_cache_valid 5m; -> Option AQuick Check:
proxy_cache_valid time; = default for others [OK]
- Using '0' for default (doesn't match statuses)
- Using invalid keywords like 'any'
- Using incorrect codes like '1' for default
Ready to prove it?
Pass the test and this pattern moves unknown
