Proxy cache basics in Nginx - Time & Space Complexity
When using nginx as a proxy cache, it is important to understand how the time to serve requests changes as more cached items are stored.
We want to know how the caching process scales when handling many requests and cache entries.
Analyze the time complexity of the following nginx proxy cache configuration snippet.
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;
server {
location / {
proxy_cache my_cache;
proxy_pass http://backend;
proxy_cache_valid 200 302 10m;
}
}
This snippet sets up a proxy cache with a specific directory structure and cache zone, then uses it to cache responses from a backend server.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Searching the cache directory structure for a matching cached file.
- How many times: Once per incoming request to check if a cached response exists.
As the number of cached items grows, nginx looks up cached files using a directory hierarchy to keep lookups fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Few file lookups in shallow directories |
| 100 | More lookups but still quick due to directory levels |
| 1000 | Still efficient because of structured cache levels |
Pattern observation: Lookup time grows slowly because the cache is organized to avoid searching many files in one place.
Time Complexity: O(1)
This means nginx can find cached responses quickly regardless of how many items are stored, thanks to its directory structure.
[X] Wrong: "The cache lookup time grows linearly as more items are cached."
[OK] Correct: nginx uses a multi-level directory structure to avoid scanning large directories, so lookup stays fast even with many cached files.
Understanding how nginx proxy cache scales helps you explain real-world caching performance and shows you can reason about system efficiency.
"What if we removed the 'levels' directive and stored all cached files in one directory? How would the time complexity change?"
Practice
proxy_cache in nginx?Solution
Step 1: Understand proxy_cache role
Theproxy_cachedirective tells nginx to save copies of responses from the backend server.Step 2: Identify purpose of caching
Storing these copies helps nginx deliver content faster to users without asking the backend every time.Final Answer:
To store copies of web responses for faster delivery -> Option AQuick Check:
proxy_cache = store copies for speed [OK]
- Confusing proxy_cache with load balancing
- Thinking proxy_cache encrypts traffic
- Assuming proxy_cache blocks requests
Solution
Step 1: Recall correct order of proxy_cache_path
The correct syntax starts with the cache directory path, then options like levels and keys_zone.Step 2: Match syntax with options order
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m; correctly places the path first, followed by levels and keys_zone with proper separators and semicolon.Final Answer:
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m; -> Option BQuick Check:
Path first, then options = proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m; [OK]
- Placing keys_zone before path
- Missing semicolon at end
- Wrong order of parameters
proxy_cache_path /tmp/cache keys_zone=mycache:10m;
server {
location / {
proxy_cache mycache;
proxy_cache_valid 200 10m;
proxy_pass http://backend;
}
}What happens when a client requests a page for the first time?
Solution
Step 1: Understand first request behavior
On the first request, cache is empty, so nginx must get the response from the backend server.Step 2: Recognize caching after fetch
After fetching, nginx stores the response in cache for future requests within 10 minutes.Final Answer:
nginx fetches from backend and caches the response -> Option AQuick Check:
First request = fetch then cache [OK]
- Assuming cache serves before first fetch
- Thinking nginx errors if cache empty
- Believing cache is ignored always
proxy_cache_valid 200 5m; but cached responses are never used. What is a likely cause?Solution
Step 1: Check proxy_cache usage
Withoutproxy_cacheenabled in the location, caching won't happen even ifproxy_cache_validis set.Step 2: Understand effect of missing proxy_cache
Missing this directive means nginx never stores or serves cached responses, causing cache to be unused.Final Answer:
Theproxy_cachedirective is missing in the location block -> Option DQuick Check:
Missing proxy_cache disables caching [OK]
- Assuming cache path missing causes no cache use
- Thinking backend status unrelated
- Believing keys_zone size stops caching
Solution
Step 1: Recall proxy_cache_valid syntax
The correct syntax isproxy_cache_valid [status] [time];for each status separately.Step 2: Identify correct multiple directives
Using two separateproxy_cache_validlines for 200 and 404 with their times is valid and clear.Final Answer:
proxy_cache_valid 200 10m; proxy_cache_valid 404 1m; -> Option CQuick Check:
Separate lines for each status = proxy_cache_valid 200 10m; proxy_cache_valid 404 1m; [OK]
- Combining statuses in one line incorrectly
- Using commas instead of semicolons
- Reversing time and status order
Ready to prove it?
Pass the test and this pattern moves unknown
