Search code examples
azurecachingazure-cdn

Cached content in Azure CDN from Microsoft expires too early


We are using a Standard Microsoft Azure CDN to serve images for a web application. These images are requested as /api/img?param1=aaa&param2=bbb, so we cache every unique URL. The cache duration is 7 days. We also override the "Cache-Control" header so that the image is only cached for 1 hours by the client browser.

The problem is, the images do not stay in cache for 7 days. The first day after the images have been requested, they seem to be in CDN (I verify the X-Cache header and it returns "TCP_HIT"), however if I make the same requests 2-3 days later, around 25% of images are not cached anymore (the X-Cache header is "TCP_MISS"). The origin server receives and logs requests, so I am sure that they bypass CDN.

Is there any explanation for this? Do I have to set additional parameters for images to be cached correctly?

We use the following settings :

  1. Caching rules "Cache every unique URL"
  2. Rules Engine:
  • if URL path begins with /api/img
  • then Cache expiration: [cache behaviour] Override, [duration] 7 days
  • and then Modify response header: Overwrite, "Cache-Control", "public, max-age=3600"

Solution

  • From some folks on the CDN Product Group:

    • for all but the Verizon Premium SKU, the max-age and cache expiration are one and the same thing, so 2c overrides 2b.
    • The CDN reserves the right to flush entries from the CDN if they are not used - cache items are evicted using an LRU algorithm.
    • the Verizon Premium SKU offers the ability to have two different age values, one for browser-to-edge (the "External Max-Age") and one for edge-to-source (original expiration time, or forced override time - see the docs).