Proxy cache path configuration in Nginx - Time & Space Complexity
When nginx handles many web requests, it uses proxy cache to speed up responses.
We want to understand how the time to find cached files grows as the cache size grows.
Analyze the time complexity of this proxy cache path setup.
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m max_size=1g inactive=60m use_temp_path=off;
This config sets where cached files are stored and how nginx organizes them in folders.
Look at how nginx finds cached files on disk.
- Primary operation: Searching for a cached file in nested directories based on key hash.
- How many times: Once per request needing cache lookup.
As cache size grows, more files exist but nginx uses folder levels to keep lookups fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files | Few folder checks, very fast lookup |
| 1000 files | Still few folder checks due to directory levels |
| 100000 files | Lookup remains quick because files are spread across many folders |
Pattern observation: Lookup time stays almost the same even as cache grows large.
Time Complexity: O(1)
This means nginx finds cached files in about the same time no matter how many files are cached.
[X] Wrong: "More cached files always mean slower lookups because nginx must check every file."
[OK] Correct: nginx uses a folder structure to spread files, so it does not check all files but goes directly to the right folder.
Understanding how caching scales helps you explain real-world server speed and efficiency.
"What if we removed the 'levels' setting and stored all cached files in one folder? How would the time complexity change?"
Practice
proxy_cache_path directive in nginx?Solution
Step 1: Understand the role of proxy_cache_path
Theproxy_cache_pathdirective tells nginx where to save cached files and how to manage them.Step 2: Compare with other options
Options A, C, and D relate to connections, SSL, and backend servers, not caching.Final Answer:
To define where and how cached files are stored -> Option DQuick Check:
proxy_cache_path = cache storage location [OK]
- Confusing proxy_cache_path with proxy_pass
- Thinking it sets connection limits
- Mixing it with SSL configuration
Solution
Step 1: Check syntax requirements for proxy_cache_path
The directive requires a semicolon at the end and correct key-value pairs with colons where needed.Step 2: Identify correct option
proxy_cache_path /data/cache keys_zone=mycache:10m max_size=1g inactive=60m; has correct syntax with colon in keys_zone, key-value pairs, and ends with a semicolon.Final Answer:
proxy_cache_path /data/cache keys_zone=mycache:10m max_size=1g inactive=60m; -> Option AQuick Check:
Correct syntax ends with semicolon and uses colons properly [OK]
- Missing semicolon at the end
- Using space instead of colon in keys_zone
- Omitting equal signs in parameters
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=mycache:10m max_size=500m inactive=30m;
What happens when a cached file is not accessed for 40 minutes?
Solution
Step 1: Understand the inactive parameter
Theinactive=30mmeans cached files not accessed for 30 minutes are removed.Step 2: Apply to 40 minutes inactivity
Since 40 minutes > 30 minutes, the cached file will be removed.Final Answer:
The cached file is removed because it is inactive longer than 30 minutes -> Option CQuick Check:
inactive=30m means remove after 30 minutes inactivity [OK]
- Confusing max_size with inactive time
- Assuming cache refresh happens automatically
- Thinking cache files move zones
proxy_cache_path /cache keys_zone=mycache 10m max_size=1g inactive=60m;
nginx fails to start. What is the error?
Solution
Step 1: Check keys_zone syntax
The keys_zone requires a colon between the zone name and size, e.g., mycache:10m.Step 2: Identify missing colon
The config useskeys_zone=mycache 10mmissing the colon, causing syntax error.Final Answer:
Missing colon between keys_zone name and size -> Option AQuick Check:
keys_zone syntax requires colon like mycache:10m [OK]
- Forgetting colon in keys_zone
- Assuming inactive needs seconds
- Ignoring path existence errors
/var/cache/nginx with a 20MB keys zone, max cache size 2GB, and remove files inactive for 15 minutes. Which is the correct proxy_cache_path directive?Solution
Step 1: Verify keys_zone size and path
Keys zone size is 20m and path is /var/cache/nginx, both correct in all options.Step 2: Check max_size and inactive syntax
Max size should be 2g (gigabytes), inactive time 15m (minutes). proxy_cache_path /var/cache/nginx keys_zone=mycache:20m max_size=2g inactive=15m; uses correct units and syntax.Step 3: Identify errors in other options
proxy_cache_path /var/cache/nginx keys_zone=mycache:20m max_size=2gb inactive=15m; uses '2gb' (invalid unit), C uses 1024m (1GB not 2GB), D uses inactive=900 (seconds, but default is minutes and needs 's').Final Answer:
proxy_cache_path /var/cache/nginx keys_zone=mycache:20m max_size=2g inactive=15m; -> Option BQuick Check:
Use correct units: m for MB, g for GB, and time units with m for minutes [OK]
- Using invalid units like 'gb' instead of 'g'
- Confusing inactive time units
- Using seconds without 's' suffix
Ready to prove it?
Pass the test and this pattern moves unknown
