User Agents

Web browsers and everyday web tech, explained

CDN Cache: What It Is and How It Works

· 7 min read · Cookies and Cache

In this article
  1. Background: Why CDN Caching Exists
  2. How a CDN Cache Works
  3. Cache Hit Ratio and Its Importance
  4. Common Problems and Misconceptions
  5. How to Choose CDN Cache Settings
  6. CDN Cache vs. Browser Cache
  7. Frequently Asked Questions

A CDN cache is a temporary storage layer used by a content delivery network (CDN) to keep copies of web files—like images, CSS, JavaScript, and sometimes full HTML pages—on servers located closer to end users. When a visitor requests a resource, the CDN serves the cached copy from the nearest edge server instead of forwarding the request all the way to the origin server. This reduces latency, lowers bandwidth costs, and eases the load on the origin. CDN caching is not a single mechanism but a combination of HTTP caching rules, edge server storage, and cache invalidation strategies that work together to balance speed and freshness.

Background: Why CDN Caching Exists

The internet physically spans thousands of miles. A user in Sydney requesting a website hosted in Frankfurt must traverse undersea cables, routers, and multiple ISPs. Every hop adds milliseconds, and under heavy traffic, the origin server can become a bottleneck. CDNs were built to solve this by distributing content across a network of edge servers. Caching is the core feature that makes this distribution effective: without it, every request would still travel to the origin, and the CDN would only be a fancy DNS redirector.

The concept draws from general web caching, which dates to the early 1990s with proxy caches and browser caches. CDN caching applies the same principles but at a global scale. The first commercial CDNs, like Akamai (founded 1998), used caching to accelerate static content. Over time, caching has expanded to dynamic content, APIs, and even streaming video segments.

How a CDN Cache Works

A CDN cache operates on a simple request-response cycle, but with several important steps:

  1. User request: A browser asks for a URL, and DNS resolves it to the nearest CDN edge server.
  2. Cache lookup: The edge server checks its local storage for a matching cached object. If found and fresh, it returns the object immediately.
  3. Miss and fetch: If not found or stale, the edge server forwards the request to the origin server, fetches the response, and stores a copy in its cache.
  4. Delivery: The response is sent to the user, and the cached copy is kept for subsequent requests.

What makes this efficient is the cache key—a unique identifier derived from the URL, query parameters, and sometimes headers like Accept-Encoding. The cache key determines whether a request is a hit or a miss. For example, image.jpg?v=2 is treated as a different object than image.jpg?v=1, even if the underlying file is the same.

Freshness is governed by HTTP headers. The most common are Cache-Control (e.g., max-age=3600) and Expires. The Cache-Control header is the modern standard. If the origin sends Cache-Control: public, max-age=86400, the CDN will serve the cached copy for 24 hours before revalidating. Revalidation uses conditional request headers like If-Modified-Since or ETag. If the origin responds with 304 Not Modified, the CDN keeps the old copy but resets its freshness timer.

CDNs also support purge (manual invalidation) and surrogate cache control—a way for the origin to tell the CDN different caching rules than those sent to the browser. For instance, the origin might set s-maxage=60 for the CDN and max-age=0 for the browser, so the CDN caches for a minute but the browser always checks for updates.

Cache Hit Ratio and Its Importance

The cache hit ratio is the percentage of requests served from the cache without contacting the origin. A high ratio (90% or more) means the CDN is doing its job. A low ratio means many requests reach the origin, defeating the purpose. Several factors affect hit ratio:

  • Cacheability of content: Static assets (images, CSS, JS) cache well. Personalized HTML or API responses with user-specific data often cannot be cached.
  • TTL settings: Short TTLs force more revalidations. Long TTLs improve hit ratio but risk serving stale content.
  • Query strings: By default, many CDNs treat every unique query string as a separate cache key. If a URL has a random parameter like ?timestamp=123456, the cache is effectively bypassed.
  • Cookie handling: If the CDN includes cookies in the cache key, each user gets a separate cached copy, which kills the hit ratio.

To improve hit ratio, you can normalize query strings, use a CDN that ignores irrelevant cookies, and set appropriate TTLs. Some CDNs offer a “cache everything” mode for static content, but that can be risky if the content changes frequently.

Common Problems and Misconceptions

One common misconception is that clearing the browser cache also clears the CDN cache. That is false—they are separate layers. Another is that CDN caching always speeds up dynamic content. In reality, dynamic content often requires bypassing the cache or using edge-side includes, which add complexity.

Common problems include:

  • Stale content: Users see old versions of a page after a deploy. This happens when TTLs are too long or purge is not performed.
  • Cache poisoning: An attacker can craft a request that stores malicious content in the cache, which is then served to other users. This is mitigated by validating input and using HTTPS.
  • Cache stampede: When a cached object expires and many users request it at once, all requests hit the origin simultaneously, causing a spike. Techniques like request coalescing or stale-while-revalidate help.
  • Over-caching sensitive data: If a CDN caches a page with personal information, it might be served to other users. This is prevented by setting Cache-Control: private or no-store on such responses.

Another misconception is that CDN caching is only for images and videos. It is also used for HTML pages, API responses, and even DNS queries, though the latter is a different type of caching.

How to Choose CDN Cache Settings

Choosing the right cache settings depends on your content type and update frequency. Here are practical guidelines:

  • Static assets (images, CSS, JS): Use a long TTL (e.g., 1 year) and include a fingerprint in the filename (like style.abc123.css). When the file changes, the URL changes, forcing a new cache entry.
  • HTML pages: Use a short TTL (e.g., 60 seconds) or no cache if the page is personalized. For news sites, a TTL of 5–15 minutes is common.
  • API responses: Cache only if the data is not user-specific. Use Cache-Control: public with a TTL that matches your data freshness needs.
  • User-specific content: Set Cache-Control: private or no-store to prevent caching at any layer.

Most CDNs provide a dashboard to override headers per URL pattern. You can also use the Cache-Control header directly from your server. A good practice is to start with conservative TTLs and gradually increase them while monitoring the hit ratio and any error reports.

CDN Cache vs. Browser Cache

Browser cache stores files on the user’s device, while CDN cache stores them on edge servers. They work together: the browser might have a copy and not even request the CDN. The CDN only sees requests that miss the browser cache. This is why you might see a low CDN hit ratio for a site with many return visitors—their browsers already have the files.

Another difference is control. You can set headers that affect both, but Cache-Control has directives that apply only to shared caches (like s-maxage) or only to private caches (like private). The browser cache is per-user, so it can store personalized content, but the CDN cache cannot (unless it keys on cookies, which is inefficient).

Frequently Asked Questions

Does clearing my browser cache clear the CDN cache?

No. Clearing your browser cache only removes files from your device. The CDN cache on edge servers remains intact. To clear the CDN cache, you need to purge it via the CDN provider’s control panel or API.

How long does a CDN cache store files?

It depends on the TTL set by the origin server’s headers. If Cache-Control: max-age=3600 is sent, the CDN will store the file for one hour. After that, it will revalidate with the origin. If the origin is unavailable, some CDNs serve stale content while others return an error.

Can CDN caching cause my website to show old content?

Yes, if you update a file but keep the same URL and the TTL has not expired, users will see the old version. To avoid this, use versioned filenames or purge the cache after deployment.

Is CDN caching safe for user data?

It can be safe if you set proper headers. Never allow caching of pages containing personal information. Use Cache-Control: private or no-store for such responses. Also ensure the CDN does not cache responses with sensitive query parameters.

What happens if the origin server goes down?

If the origin goes down, the CDN can still serve cached content until the TTL expires. Some CDNs offer “stale-if-error” which serves stale content even after TTL if the origin is unreachable. This improves availability but may serve outdated information.

Do all CDNs cache the same way?

No. Different CDNs have different default behaviors for query strings, cookies, and headers. Some cache HTML by default; others only cache static files. You should read your CDN’s documentation to understand its specific caching rules.