CDN caching rules in Azure - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to apply CDN caching rules changes as we add more rules.
Specifically, how does the system handle more rules when deciding what to cache?
Analyze the time complexity of the following Azure CDN caching rules setup.
// Create CDN endpoint
az cdn endpoint create --name myEndpoint --profile-name myProfile --resource-group myResourceGroup --origin myOrigin
// Add caching rules
az cdn endpoint rule add --endpoint-name myEndpoint --profile-name myProfile --resource-group myResourceGroup --order 1 --action CacheExpiration --cache-behavior Override --cache-duration 3600
az cdn endpoint rule add --endpoint-name myEndpoint --profile-name myProfile --resource-group myResourceGroup --order 2 --action CacheExpiration --cache-behavior BypassCache
This sequence creates a CDN endpoint and adds caching rules that control how content is cached or bypassed.
Look at what happens repeatedly when the CDN processes requests with caching rules.
- Primary operation: Checking each caching rule in order to decide cache behavior.
- How many times: Once per incoming request, checking rules sequentially until a match is found.
As the number of caching rules increases, the CDN checks more rules for each request.
| Input Size (n rules) | Approx. Rule Checks per Request |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows directly with the number of rules.
Time Complexity: O(n)
This means the time to decide caching behavior grows linearly with the number of caching rules.
[X] Wrong: "Adding more caching rules does not affect request processing time."
[OK] Correct: Each request must check rules in order, so more rules mean more checks and longer processing time.
Understanding how rule checks scale helps you design efficient CDN configurations and shows you can think about system performance clearly.
"What if the CDN used a hash map to find the matching caching rule instead of checking rules in order? How would the time complexity change?"
Practice
Solution
Step 1: Understand CDN caching rules
CDN caching rules define how content is cached and delivered by CDN servers.Step 2: Identify the main goal
The main goal is to improve speed and reduce load by controlling content storage and delivery.Final Answer:
To control how content is stored and served from CDN servers -> Option CQuick Check:
CDN caching rules = control content caching [OK]
- Confusing caching with encryption
- Thinking caching manages authentication
- Assuming caching monitors server CPU
Solution
Step 1: Identify correct cache-control syntax
Azure CDN uses 'cache-control: max-age=seconds' to set cache duration.Step 2: Match correct file patterns
Images are matched by extensions like '*.jpg, *.png' not 'images/*'.Final Answer:
cache-control: max-age=3600; match: *.jpg, *.png-> Option BQuick Check:
cache-control max-age + correct pattern =cache-control: max-age=3600; match: *.jpg, *.png[OK]
- Using 'cache-duration' instead of 'cache-control'
- Using folder pattern 'images/*' instead of file extensions
- Mixing syntax keywords incorrectly
cache-control: max-age=0; match: /api/*What will happen when a user requests
/api/data?Solution
Step 1: Interpret max-age=0 meaning
max-age=0 means no caching; content is always fresh.Step 2: Apply rule to /api/* path
Requests to /api/data match /api/*, so caching is disabled.Final Answer:
Content will be cached for 0 seconds, forcing CDN to fetch fresh data every time -> Option DQuick Check:
max-age=0 disables caching = Content will be cached for 0 seconds, forcing CDN to fetch fresh data every time [OK]
- Assuming max-age=0 caches content indefinitely
- Confusing path matching syntax
- Thinking content is blocked
cache-control max-age=600; match: *.cssBut it does not work as expected. What is the error?
Solution
Step 1: Check syntax for cache-control
The correct syntax requires a colon after 'cache-control', like 'cache-control: max-age=600'.Step 2: Validate other parts
File extension '*.css' is correct; max-age=600 is valid; match pattern does not require full URL.Final Answer:
Missing colon after 'cache-control' -> Option AQuick Check:
Syntax error: missing colon = Missing colon after 'cache-control' [OK]
- Omitting colon after 'cache-control'
- Using wrong file patterns
- Thinking max-age value causes failure
Solution
Step 1: Understand content types and caching needs
Static images benefit from long caching (86400 seconds = 1 day). Dynamic API data needs fresh content, so max-age=0.Step 2: Match rules to content paths
Apply long cache to image extensions (*.jpg, *.png) and no cache to API paths (/api/*).Final Answer:
Setcache-control: max-age=86400for images (*.jpg, *.png) andcache-control: max-age=0for API (/api/*) -> Option AQuick Check:
Static long cache + dynamic no cache = Setcache-control: max-age=86400for images (*.jpg, *.png) andcache-control: max-age=0for API (/api/*) [OK]
- Caching dynamic API data too long
- Not differentiating static and dynamic content
- Reversing cache durations for content types
