Purging cached content in Nginx - Time & Space Complexity
When nginx purges cached content, it removes specific stored files to serve fresh data. Understanding the time complexity helps us manage performance.
We want to know how the work needed changes (or not) as the cache size grows.
Analyze the time complexity of the following nginx cache purge configuration snippet.
location /purge/ {
allow 127.0.0.1;
deny all;
proxy_cache_purge my_cache $request_uri;
}
This snippet allows purging cached content by matching the request URI and removing the corresponding cache entry.
Look for repeated actions in the purge process.
- Primary operation: Computing the hash of the request URI to directly locate and delete the cache file.
- How many times: Once per purge request. The location is computed deterministically, independent of cache size.
As the cache grows, purging a specific entry takes constant time because nginx uses a hash-based directory structure to compute the exact file path directly.
| Input Size (cache entries) | Approx. Operations |
|---|---|
| 10 | 1 file access |
| 100 | 1 file access |
| 1000 | 1 file access |
Pattern observation: The work is constant, independent of the number of cached files.
Time Complexity: O(1)
This means the time to purge is constant, independent of the number of cached items.
[X] Wrong: "Purging one cache entry takes longer as the cache grows because nginx searches through files."
[OK] Correct: nginx computes the exact file path using a hash of the key, allowing direct access without scanning the cache.
Understanding how cache purge time behaves helps you explain system behavior and troubleshoot performance in real setups. This skill shows you think about efficiency in practical DevOps tasks.
"nginx uses a hash-based structure for locating cache files. What is the resulting time complexity for purge?"
Practice
Solution
Step 1: Understand caching in nginx
Caching stores copies of content to serve faster responses.Step 2: Understand purging purpose
Purging removes specific cached files to update content without restarting nginx.Final Answer:
To remove specific cached files so fresh content is served -> Option AQuick Check:
Purging = Remove cached files [OK]
- Confusing purging with disabling cache
- Thinking purging restarts nginx
- Assuming purging increases cache size
Solution
Step 1: Recall nginx directive syntax
Directives use the format: directive_name value; with zone name for cache purging.Step 2: Identify correct value for enabling purge
Enabling uses zone name 'mycache' not 'enable', 'true', or 'off'.Final Answer:
proxy_cache_purge mycache; -> Option DQuick Check:
Enable purge = proxy_cache_purge mycache; [OK]
- Using 'enable' or 'true' instead of the zone name
- Forgetting the semicolon at the end
- Setting proxy_cache_purge to 'off' by mistake
location /purge/ {
allow 192.168.1.0/24;
deny all;
proxy_cache_purge mycache;
}What happens when a client from IP 192.168.1.10 sends a PURGE request to
/purge/page1?Solution
Step 1: Check IP access rules
Allow 192.168.1.0/24 means IP 192.168.1.10 is allowed; others denied.Step 2: Understand proxy_cache_purge mycache
Enables purging cached content for matching requests.Final Answer:
The cached content for /purge/page1 is removed -> Option AQuick Check:
Allowed IP + PURGE request = cache removed [OK]
- Assuming all IPs can purge
- Thinking PURGE clears entire cache
- Confusing PURGE with GET request
location /purge/ {
deny all;
proxy_cache_purge mycache;
}But PURGE requests still return 403 Forbidden. What is the likely cause?
Solution
Step 1: Understand default access control
Without 'allow', nginx denies all due to 'deny all;' for security.Step 2: Check purge access rules
To allow PURGE requests, you must explicitly allow trusted IPs.Final Answer:
Missing allow/deny rules to permit purge requests -> Option CQuick Check:
deny all without allow = 403 Forbidden on PURGE [OK]
- Assuming PURGE method is unsupported
- Ignoring access control rules
- Thinking empty cache causes 403
/images/ and restrict purge access to IP 10.0.0.5. Which nginx config snippet correctly achieves this?Solution
Step 1: Identify purge location for /images/ URLs
Purging usually uses a separate location like /purge/images/ to trigger cache removal.Step 2: Check access control and purge directive
Allow only IP 10.0.0.5 and deny others with proxy_cache_purge mycache enabled.Final Answer:
location /purge/images/ { proxy_cache_purge mycache; allow 10.0.0.5; deny all; } -> Option BQuick Check:
Correct purge path + access control = location /purge/images/ { proxy_cache_purge mycache; allow 10.0.0.5; deny all; } [OK]
- Enabling purge in wrong location
- Using proxy_cache instead of proxy_cache_purge
- Allowing all IPs to purge
Ready to prove it?
Pass the test and this pattern moves unknown
