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 directive | What it does |
|---|---|
| Cache-Control: max-age | How long browsers may reuse a response, in seconds |
| Cache-Control: s-maxage | How long shared caches such as a CDN may keep it |
| Cache-Control: immutable | Tells browsers the file will never change at this URL |
| Cache-Control: no-store | Do not store at all; for private or sensitive responses |
| ETag and Last-Modified | Let caches revalidate cheaply instead of downloading again |
| Vary | Which request headers create separate cached copies |
Practical steps
- Version static files by putting a content hash in the filename, such as
app.3f9a1c.js. Then cache them for a long time withmax-age=31536000, immutable, which is one year. - Give HTML a short cache time or revalidation, so new content appears quickly while assets stay cached.
- Strip query strings that do not change content, such as tracking parameters, from the cache key. Otherwise each variant is cached separately.
- Keep
Varyminimal. Varying onUser-Agentcan create thousands of copies of one file. - Never cache responses that set cookies or show a logged-in user's data on a shared cache.
- Turn on modern compression such as Brotli or gzip for text files.
- 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.