Spunk Autos

Websites and web tools

CDN Optimization: Key Resources and Steps

A content delivery network keeps copies of your files on servers close to visitors. It only helps when those copies are actually reused. This page explains the cache hit ratio, the headers that control caching and the reference documents worth bookmarking, with examples for a typical small business site full of photos.

The number to watch: cache hit ratio

The cache hit ratio is the share of requests the CDN answers from its own cache instead of going back to your origin server. A higher ratio means less load on the origin and faster responses for visitors.

Headers that control caching

Header or directiveWhat it does
Cache-Control: max-ageHow long browsers may reuse a response, in seconds
Cache-Control: s-maxageHow long shared caches such as a CDN may keep it
Cache-Control: immutableTells browsers the file will never change at this URL
Cache-Control: no-storeDo not store at all; for private or sensitive responses
ETag and Last-ModifiedLet caches revalidate cheaply instead of downloading again
VaryWhich request headers create separate cached copies

Practical steps

  1. Version static files by putting a content hash in the filename, such as app.3f9a1c.js. Then cache them for a long time with max-age=31536000, immutable, which is one year.
  2. Give HTML a short cache time or revalidation, so new content appears quickly while assets stay cached.
  3. Strip query strings that do not change content, such as tracking parameters, from the cache key. Otherwise each variant is cached separately.
  4. Keep Vary minimal. Varying on User-Agent can create thousands of copies of one file.
  5. Never cache responses that set cookies or show a logged-in user's data on a shared cache.
  6. Turn on modern compression such as Brotli or gzip for text files.
  7. Serve correctly sized images in modern formats; the CDN cannot fix an oversized original.

Measuring the effect

Check response headers in the browser network panel. Most CDNs add a header showing whether a request was a hit or a miss. Load the same file twice: the second request should be a hit. Compare the CDN's own hit ratio report before and after each change, and watch the origin server's request count.

References worth bookmarking

  • RFC 9111, HTTP Caching, which defines how caches must treat Cache-Control and related headers.
  • RFC 9110, HTTP Semantics, for status codes, validators and conditional requests.
  • Your CDN provider's documentation on cache keys, purge methods and default behaviour, since defaults differ.
  • The browser developer tools documentation for the network panel.

Common mistakes

The classic errors are caching HTML for a day and then wondering why an urgent price change does not show, purging the whole cache on every deploy so the hit ratio drops to zero, and putting a long max-age on files without versioned names, so visitors keep stale scripts. Version the files, purge narrowly and the problems mostly disappear.