Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 37 ยท Network and Caching

CDN and Edge Caching

Learn how a content delivery network shortens round trips, what to cache at the edge, how cache keys, TTLs and purging work, and how to avoid caching private data.

A content delivery network (CDN) is a global network of servers, called edge locations, that keep copies of your content close to visitors. A user in Singapore fetches your page from a nearby edge server instead of your origin in Frankfurt. Since latency is mostly distance, this alone can cut hundreds of milliseconds.

Beyond speed, a CDN absorbs traffic spikes, protects your origin and often handles compression, TLS and HTTP/3 for you.

How a CDN request works

flowchart LR
  U["Visitor"] --> E["Nearest edge"]
  E -->|Hit| U
  E -->|Miss| O["Your origin server"]
  O --> E

On a hit the edge answers directly; on a miss it fetches from the origin, stores the response and replies.

  • Cache hit: the edge already holds a fresh copy and answers immediately.
  • Cache miss: the edge asks your origin, stores the response, then answers. The next visitor nearby gets a hit.
  • The share of requests answered at the edge is the cache hit ratio. Higher is better.

What to put on a CDN

Content CDN-friendly? Notes
Fingerprinted JS, CSS, fonts, images Excellent Cache for a year
Product images, video segments Excellent Use size variants
Public HTML that is the same for everyone Good Use short TTLs or purge on change
Public API responses Good s-maxage with a short TTL
Personalised pages, carts, accounts No (or special handling) Use private

Controlling the edge

The CDN reads your normal HTTP caching headers (caching headers). Two are especially useful:

HTTP
Cache-Control: public, max-age=60, s-maxage=3600, stale-while-revalidate=86400

This tells browsers to keep the page for 60 seconds, shared caches (the CDN) for an hour, and to serve a stale copy for up to a day while it refreshes in the background. Browsers stay reasonably fresh, and the CDN rarely bothers your origin.

CDNs also let you configure rules in a dashboard: override TTLs, ignore cookies on static paths, or cache by file extension.

The cache key

The CDN looks up a stored response by a cache key, normally the URL. Be careful what you add to the key:

  • Query strings. ?utm_source=x creates a separate cache entry for every campaign value, lowering the hit ratio. Ignore tracking parameters in the key.
  • Cookies. If the cache key includes a cookie (or the response varies on it), almost every request becomes unique. Strip cookies on static assets.
  • Vary header. Vary: Accept-Encoding is fine (a few variants). Vary: Cookie is dangerous for hit ratio.
JS Cache hit ratio and the cache key

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Dropping the tracking parameter from the key lifts the hit ratio from 13% to 75% in this stand-in. Real traffic patterns differ, but the lesson is real: noisy keys waste your cache.

Invalidation

When content changes before its TTL expires, you need to purge:

  • Purge by URL or by tag/surrogate key. Tag all pages that include a product, then purge that tag when the product changes.
  • Purge everything: simple but empties the cache; use sparingly.
  • Version the URL: for static assets, the fingerprint approach means you never purge at all.
  • Stale-while-revalidate lets you set longer TTLs without serving stale content for long.

Edge compute

Many CDNs can run small functions at the edge. Uses include redirects, A/B routing, header rewriting, authentication checks and generating personalised fragments around a cached page. Doing this at the edge saves a trip to the origin.

Never let a CDN cache a response that contains one user's private data under a shared key. Mark personal responses Cache-Control: private or no-store, and check that cookies and Authorization headers are not ignored by the cache.

Checking that it works

Look for response headers that expose the cache status. Names vary by provider (Age, X-Cache, CF-Cache-Status). Age is standard: it says how many seconds a response has sat in a shared cache.

JS Interpret an Age header

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a cache hit ratio?

    A higher ratio means fewer requests reach the origin and more are answered fast near the user.
  2. 2. Which directive controls only shared caches such as a CDN?

    s-maxage overrides max-age for shared caches.
  3. 3. Why can tracking query strings hurt a CDN?

    If the full URL is the key, every parameter variation is a cache miss on first view.
  4. 4. How do fingerprinted assets avoid purging?

    A new name is a new URL, so old caches are simply never asked for.

Summary

  • A CDN serves content from nearby edge servers, cutting latency and origin load.
  • Use s-maxage and stale-while-revalidate for shared caches.
  • Keep cache keys clean: ignore tracking parameters and avoid Vary: Cookie.
  • Purge by tag, version static URLs and never cache private data under shared keys.

Next: Compression, HTTP/2 and HTTP/3