noted.do
The Tech Guy

Caching - Why Your Website Remembers Things (and Sometimes Shouldn't)

"Have you tried clearing your cache?" is probably the most repeated piece of tech support advice ever. It works often enough that people treat it like a ritual. But it helps to know what's actually being cleared, because there isn't one cache. There are several, stacked on top of each other, and each one can be the reason you're seeing an old version of something.

The idea behind all of them is the same. Doing work is slow. Remembering the answer is fast. So if you'll need the same thing again soon, keep a copy nearby.

Your browser

The cache closest to you lives in your browser. When you visit a site, your browser downloads the logo, the stylesheet, the JavaScript, the fonts. Next time, if the server said those files were fine to keep, the browser just uses its local copy instead of downloading them again. That's a big part of why the second visit to a site feels faster than the first.

The server controls this with response headers, mainly Cache-Control. Something like this tells the browser to keep a file for a year:

Cache-Control: public, max-age=31536000, immutable

And this tells it to always check with the server before reusing what it has:

Cache-Control: no-cache

(Confusingly, no-cache doesn't mean "don't cache." It means "cache it, but revalidate first." The one that means don't store it at all is no-store.)

When the browser revalidates, it can send along an ETag or a Last-Modified date for the copy it has. If nothing changed, the server replies with a tiny 304 Not Modified and no body. Still a round trip, but a cheap one.

The CDN in the middle

If a site uses a CDN, there's another cache sitting between you and the origin server, spread across data centers all over the world. The first visitor in a region causes the CDN to fetch the file from the origin. Everyone after that gets it from the nearby copy. The origin server barely notices.

CDNs mostly follow the same headers, sometimes with their own extra rules. And they're the layer people forget about most often. You fix a typo, deploy, check the site, and the typo is still there. Your browser is fine. The CDN is still serving yesterday's copy. Most CDNs have a "purge" button for exactly this.

The server itself

Further back, the server and the app do their own caching, and this is usually where the biggest speed wins hide. A few common kinds:

  • Full page caching. If a blog post looks the same for every visitor, render it once and serve that saved HTML until it changes, instead of running the app and the database on every request.
  • Object or query caching. Store the results of expensive database queries or API calls in something fast like Redis or Memcached, and reuse them for a while.
  • Opcode caching. For languages like PHP, the server keeps compiled code in memory so it isn't re-parsing your source files on every request. This one is usually just turned on and forgotten.

The hard part

There's an old joke that the two hardest problems in computer science are naming things, cache invalidation, and off-by-one errors. The middle one is the real point. Caching is easy. Knowing when a cached copy is stale and needs throwing out is the hard bit.

The cleanest trick for static files is to never change a file in place. Instead, put a version or a content hash in the filename, like app.3f9a2c.js. When the file changes, the name changes, the HTML points to the new name, and you can safely tell every cache to keep the old one forever because nobody will ask for it again. Most build tools do this for you.

For HTML pages, cache more cautiously. Short lifetimes, or no-cache so browsers check in each time, because that's the file that points to everything else.

And be really careful with anything personal. A page that shows "Hi, Sam" or someone's account details should never land in a shared cache like a CDN. Mark it private or no-store. Getting this wrong means one user's page gets served to another, which is a far worse bug than a slow site.

So the next time someone tells you to clear your cache, it's worth asking which one. Your browser's is the only one you can clear yourself. If a hard refresh doesn't fix it, the stale copy is probably sitting further up the chain.

Comments

Be kind. See the rules.