Bird
Raised Fist0
Azurecloud~5 mins

CDN caching rules in Azure - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
CDN caching rules help store copies of your website files closer to users to make loading faster. They decide how long content stays saved on servers before checking for updates.
When you want your website images to load quickly for visitors worldwide.
When you need to reduce traffic to your main server by serving cached content.
When you want to control how often your website updates appear to users.
When you want to improve user experience by speeding up page load times.
When you want to save bandwidth costs by serving cached files from the CDN.
Config File - cdn-caching-rules.json
cdn-caching-rules.json
{
  "name": "exampleCachingRule",
  "order": 1,
  "conditions": [
    {
      "name": "UrlFileExtension",
      "parameters": {
        "operator": "Equal",
        "matchValues": ["jpg", "png", "css", "js"],
        "negateCondition": false,
        "transforms": []
      }
    }
  ],
  "actions": [
    {
      "name": "CacheExpiration",
      "parameters": {
        "cacheBehavior": "Override",
        "cacheDuration": "1.00:00:00"
      }
    }
  ]
}

This JSON defines a caching rule named exampleCachingRule.

order sets the priority of the rule.

conditions specify which file types the rule applies to (images and stylesheets).

actions tell the CDN to cache these files for 1 day, overriding default settings.

Commands
This command adds a caching rule to the Azure CDN endpoint. It sets the CDN to cache jpg, png, css, and js files for 1 day, overriding default cache settings.
Terminal
az cdn endpoint rule add --resource-group exampleResourceGroup --profile-name exampleProfile --endpoint-name exampleEndpoint --rule-name exampleCachingRule --order 1 --action-name CacheExpiration --cache-behavior Override --cache-duration 1.00:00:00 --match-variable UrlFileExtension --operator Equal --match-values jpg png css js
Expected OutputExpected
{ "name": "exampleCachingRule", "order": 1, "actions": [ { "name": "CacheExpiration", "parameters": { "cacheBehavior": "Override", "cacheDuration": "1.00:00:00" } } ], "conditions": [ { "name": "UrlFileExtension", "parameters": { "operator": "Equal", "matchValues": [ "jpg", "png", "css", "js" ], "negateCondition": false, "transforms": [] } } ] }
→
--cache-behavior - Sets how the CDN caches content (Override means use this rule's duration)
→
--cache-duration - Specifies how long content is cached (1 day here)
→
--match-values - Defines which file extensions this rule applies to
This command lists all caching rules applied to the CDN endpoint so you can verify your new rule is active.
Terminal
az cdn endpoint rule list --resource-group exampleResourceGroup --profile-name exampleProfile --endpoint-name exampleEndpoint
Expected OutputExpected
[ { "name": "exampleCachingRule", "order": 1, "actions": [ { "name": "CacheExpiration", "parameters": { "cacheBehavior": "Override", "cacheDuration": "1.00:00:00" } } ], "conditions": [ { "name": "UrlFileExtension", "parameters": { "operator": "Equal", "matchValues": [ "jpg", "png", "css", "js" ], "negateCondition": false, "transforms": [] } } ] } ]
Key Concept

If you remember nothing else from this pattern, remember: CDN caching rules control how long and for which files your content is saved on servers close to users to speed up loading.

Common Mistakes
Setting cache duration too short or too long without considering content update frequency.
This causes users to see outdated content or forces the CDN to fetch from origin too often, slowing performance.
Choose cache durations that balance freshness and speed based on how often your content changes.
Not specifying file types in conditions, causing all content to be cached the same way.
Different files need different caching; for example, images can be cached longer than HTML pages.
Use conditions to target specific file extensions for appropriate caching rules.
Summary
Use the Azure CLI to add caching rules that specify which file types to cache and for how long.
Verify caching rules by listing them on the CDN endpoint to ensure they are applied correctly.
Caching rules help speed up content delivery by storing files closer to users for a set time.

Practice

(1/5)
1. What is the primary purpose of CDN caching rules in Azure CDN?
easy
A. To monitor server CPU usage
B. To encrypt data between client and server
C. To control how content is stored and served from CDN servers
D. To manage user authentication

Solution

  1. Step 1: Understand CDN caching rules

    CDN caching rules define how content is cached and delivered by CDN servers.
  2. Step 2: Identify the main goal

    The main goal is to improve speed and reduce load by controlling content storage and delivery.
  3. Final Answer:

    To control how content is stored and served from CDN servers -> Option C
  4. Quick Check:

    CDN caching rules = control content caching [OK]
Hint: Caching rules control content storage and delivery [OK]
Common Mistakes:
  • Confusing caching with encryption
  • Thinking caching manages authentication
  • Assuming caching monitors server CPU
2. Which Azure CDN caching rule syntax correctly sets a cache duration of 3600 seconds for all images?
easy
A. cache-duration: 3600; match: *.jpg, *.png
B. cache-control: max-age=3600; match: *.jpg, *.png
C. cache-control: max-age=3600; match: images/*
D. cache-duration: max-age=3600; match: images/*

Solution

  1. Step 1: Identify correct cache-control syntax

    Azure CDN uses 'cache-control: max-age=seconds' to set cache duration.
  2. Step 2: Match correct file patterns

    Images are matched by extensions like '*.jpg, *.png' not 'images/*'.
  3. Final Answer:

    cache-control: max-age=3600; match: *.jpg, *.png -> Option B
  4. Quick Check:

    cache-control max-age + correct pattern = cache-control: max-age=3600; match: *.jpg, *.png [OK]
Hint: Use 'cache-control: max-age' with file extensions for images [OK]
Common Mistakes:
  • Using 'cache-duration' instead of 'cache-control'
  • Using folder pattern 'images/*' instead of file extensions
  • Mixing syntax keywords incorrectly
3. Given this Azure CDN caching rule:
cache-control: max-age=0; match: /api/*
What will happen when a user requests /api/data?
medium
A. Content will be blocked by CDN
B. Content will be cached indefinitely
C. Content will be cached for 3600 seconds
D. Content will be cached for 0 seconds, forcing CDN to fetch fresh data every time

Solution

  1. Step 1: Interpret max-age=0 meaning

    max-age=0 means no caching; content is always fresh.
  2. Step 2: Apply rule to /api/* path

    Requests to /api/data match /api/*, so caching is disabled.
  3. Final Answer:

    Content will be cached for 0 seconds, forcing CDN to fetch fresh data every time -> Option D
  4. Quick Check:

    max-age=0 disables caching = Content will be cached for 0 seconds, forcing CDN to fetch fresh data every time [OK]
Hint: max-age=0 means no caching, always fresh content [OK]
Common Mistakes:
  • Assuming max-age=0 caches content indefinitely
  • Confusing path matching syntax
  • Thinking content is blocked
4. You wrote this Azure CDN caching rule:
cache-control max-age=600; match: *.css
But it does not work as expected. What is the error?
medium
A. Missing colon after 'cache-control'
B. Incorrect file extension pattern
C. max-age value too low
D. Match pattern should be a full URL

Solution

  1. Step 1: Check syntax for cache-control

    The correct syntax requires a colon after 'cache-control', like 'cache-control: max-age=600'.
  2. Step 2: Validate other parts

    File extension '*.css' is correct; max-age=600 is valid; match pattern does not require full URL.
  3. Final Answer:

    Missing colon after 'cache-control' -> Option A
  4. Quick Check:

    Syntax error: missing colon = Missing colon after 'cache-control' [OK]
Hint: Remember colon after 'cache-control:' in rules [OK]
Common Mistakes:
  • Omitting colon after 'cache-control'
  • Using wrong file patterns
  • Thinking max-age value causes failure
5. You want to optimize caching for a website with static images and dynamic API data. Which Azure CDN caching rules setup is best?
hard
A. Set cache-control: max-age=86400 for images (*.jpg, *.png) and cache-control: max-age=0 for API (/api/*)
B. Set cache-control: max-age=0 for all content
C. Set cache-control: max-age=3600 for all content
D. Set cache-control: max-age=86400 for API and cache-control: max-age=0 for images

Solution

  1. 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.
  2. Step 2: Match rules to content paths

    Apply long cache to image extensions (*.jpg, *.png) and no cache to API paths (/api/*).
  3. Final Answer:

    Set cache-control: max-age=86400 for images (*.jpg, *.png) and cache-control: max-age=0 for API (/api/*) -> Option A
  4. Quick Check:

    Static long cache + dynamic no cache = Set cache-control: max-age=86400 for images (*.jpg, *.png) and cache-control: max-age=0 for API (/api/*) [OK]
Hint: Long cache for static, no cache for dynamic content [OK]
Common Mistakes:
  • Caching dynamic API data too long
  • Not differentiating static and dynamic content
  • Reversing cache durations for content types