Back to all questions

How Long Does Cached Data Remain in a CDN Server

Michael Hakimi
CDN
July 12, 2024

The duration for which cached data remains in a CDN server varies based on the cache-control policies set by the website owner, the type of data, and the configurations of the CDN. 

This can range from a few seconds to several years. The industry standard is a 24 hour expiry, after which, new content is fetched.

Cache-Control Policies

Max-Age Directive:

  • The max-age directive in the HTTP header defines the maximum amount of time a resource is considered fresh. For example, max-age=3600 indicates that the data should be cached for one hour (3600 seconds).
  • This is the most straightforward method to control caching duration.

s-maxage Directive:

  • The s-maxage directive is similar to max-age but is specifically for shared caches like CDNs. It overrides the max-age directive if both are present.
  • For example, s-maxage=86400 sets the cached data to expire in 24 hours (86400 seconds) for shared caches.

Expires Header:

  • The Expires header sets a specific date and time for the resource to expire. This is an older method and is less flexible than max-age or s-maxage.
  • Example: Expires: Wed, 21 Oct 2024 07:28:00 GMT.

Cache-Control: No-Cache, No-Store:

  • no-cache indicates that the cached data must be revalidated with the origin server before serving it.
  • no-store instructs the cache to never store the data.

Types of Data

Static Content:

  • Images, stylesheets, JavaScript files typically have long caching durations because they don’t change frequently.
  • Example: Images might be cached for a year (max-age=31536000).
  • Check: How to Improve Cache Static Content

Dynamic Content:

  • Content that changes frequently, like news articles or user-specific data, has much shorter caching durations.
  • Example: Dynamic HTML might be cached for a few minutes or even just seconds (max-age=60).

Fine tuning these also improves cache hit ratio, but that’s more related to the quality rather than the longevity. 

CDN Configurations

Default Settings:

  • Many CDNs have default caching policies and cache keys. For instance, if no cache-control header is set, some CDNs might default to caching content for 24 hours.

Custom Rules:

  • CDNs often allow custom rules based on URL patterns, file types, or other criteria. These rules can override default settings and HTTP headers.

Browser Caching vs. CDN Caching

Browser Caching:

  • Browsers cache data locally on the user’s device. The duration is governed by the same cache-control headers but is specific to individual users.
  • Example: If a stylesheet has max-age=31536000, the browser will cache it for a year on the user's device.

Web Caching in CDNs:

  • CDNs cache data across multiple servers to serve it faster to users worldwide. The cache-control headers guide this caching, but CDNs can also revalidate and purge cached content based on specific triggers or manual interventions.

Calculations

Let’s look at a scenario for better understanding:

Example Scenario:

  • A website sets the following header for its images: Cache-Control: public, max-age=604800, s-maxage=2592000.
    • max-age=604800 (1 week) means the browser will cache the images for one week.
    • s-maxage=2592000 (1 month) means the CDN will cache the images for one month.
  • Here, users will get the images from their browser cache for up to a week without re-fetching from the CDN. The CDN will serve the same image to different users from its cache for up to a month before re-fetching from the origin server.

Practical Considerations

Cache Invalidation:

  • Sometimes, you need to purge cached content before its expiration. CDNs offer mechanisms for cache invalidation, either through their APIs or dashboards.

Stale-While-Revalidate and Stale-If-Error:

  • These directives allow serving stale content while fetching new data or if the origin server is down, improving reliability.

Edge Cases:

  • Some content, like personalized data, should not be cached (Cache-Control: private, no-store).