Paj Digital Labs
All articles
Web Design & Engineering

Caching for Websites: Browser, CDN and Server Layers Explained

Caching is the cheapest performance upgrade there is and the most common source of 'why is the old version still showing?' A practical model of each cache layer and the headers that control them.

Paj Digital Labs 3 min read
Web design workspace with desktop and laptop displays
Editorial illustration · Web Design & Engineering

Two complaints arrive in the same week: "the site is slow" and "I updated the page but visitors still see the old version." Both are caching problems — one too little, one too much. Understanding the layers makes both easy to fix.

The layers

text
Browser cache  ->  CDN edge cache  ->  Server / application cache  ->  Database

Each layer can answer a request without asking the next one. The closer to the visitor the answer comes from, the faster it is.

Cache-Control is the main switch

| Header | Meaning | |---|---| | max-age=31536000, immutable | Keep for a year; never revalidate | | max-age=0, must-revalidate | Always check with the server before using | | s-maxage=600 | Shared caches (CDN) may keep it for 10 minutes | | stale-while-revalidate=60 | Serve stale for up to 60s while refreshing in the background | | no-store | Never cache — for private or sensitive responses | | private | Browser may cache; CDNs must not |

Static assets: cache forever, change the name

CSS, JavaScript, fonts and images built by your bundler should have a content hash in the file name, like app.3f9a2c.js. Serve them with a one-year max-age and immutable. When the file changes, its name changes, so there is never a stale version to worry about.

HTML: short and revalidated

HTML pages reference those hashed files, so the HTML itself must stay fresh. A good default for public pages:

text
Cache-Control: public, max-age=0, s-maxage=300, stale-while-revalidate=60

Browsers always check for updates; the CDN serves a copy for five minutes and refreshes in the background. Most visitors get an edge-speed response, and changes appear within minutes.

Never cache private pages publicly

Account pages, admin panels, carts and anything personalised must be private or no-store. A CDN that caches one user's account page and serves it to another is a serious privacy incident. Check every response that sets a cookie or depends on login.

Purging

When content must update immediately — a price correction, a legal change — purge the CDN for that URL. Most CDNs support purge by URL or by tag. Tag-based purging is cleaner: tag every article page with its ID, and purge that tag when the article changes.

Server-side caching

Below the CDN, caching expensive work reduces load:

  • Page caching for public pages generated by a CMS
  • Object caching (for example in Redis) for repeated database queries
  • Opcode caching for PHP, which should always be on

For a typical WordPress site, page caching plus object caching is the difference between struggling at 50 concurrent visitors and comfortably serving thousands.

Debugging stale content

When someone reports old content, check in order:

1. Response headers — look for the CDN's cache status header (HIT, MISS, STALE). 2. Age header — how long the CDN has held this copy. 3. Browser cache — test in a private window. 4. Server cache — clear the application cache if the origin itself is stale.

bash
curl -sI https://example.com/article | grep -iE class="text-code-string">"cache|age"

Watch the cache hit ratio

A healthy content site often serves the large majority of requests from the CDN. If the hit ratio is low, look for cookies or query strings that make every request unique — tracking parameters are a common culprit and can usually be ignored in the cache key.

Caching is not magic. It is a set of explicit rules per type of response. Write them down, apply them consistently, and both "slow" and "stale" complaints disappear.

Paj Digital Labs

The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.

Related reading