Skip to content

What is a cache?

A cache is a stash of ready-made results: instead of fetching, computing or building something from scratch every time, a computer keeps a copy somewhere it can reach faster. That is why pages load faster, servers work less and you wait less.

In short

  • A cache is a copy of data kept close at hand, so it does not have to be fetched or computed again next time.
  • It works on many levels at once: in your browser, on the server, in a CDN, in the database, even inside the processor.
  • The price of speed is the risk of an outdated copy - hence the famous "clear your cache".

The file on your desk

Imagine you work in an office, and the archive is in the basement. The first time you need a client's file, you walk downstairs. But since you will need it a few more times this week, you make a copy and keep it on your desk. Next time it takes a second instead of five minutes.

That is a cache. Your desk is small, so only the things you use most live there. And when someone in the basement swaps a document in the original file, the copy on your desk is out of date - which is the whole problem with caches in a nutshell.

Where caches live

  • In your browser - a site's logo, styles, scripts and pictures are saved on your disk. On your next visit, the browser takes them from there instead of downloading them from the server again. That is why the second visit to a site is usually noticeably faster.
  • On the server - WordPress, for example, builds every page from the database and the theme on each visit. A caching plugin or server builds it once, saves the finished HTML and hands that out to the next visitors.
  • In a CDN - copies of files kept on servers spread around the world, close to visitors. More in What is a CDN?.
  • In applications and databases - the results of heavy queries kept in memory (in Redis, for example), so they are not recomputed on every click.
  • In the processor and the disk - even hardware has several levels of cache. The rule is the same everywhere: small and fast nearby, big and slow further away.

Why "clear your cache" works so often

You change something on your site, refresh - nothing. The designer swears the new logo is uploaded, and you still see the old one. Sound familiar? 🙂

Your browser (or the server, or the CDN) is showing a copy from before the change, because it decided the copy is still valid. What helps:

  • a hard refresh - Ctrl+F5 or Ctrl+Shift+R on Windows and Linux, Cmd+Shift+R on a Mac,
  • a private window - it starts with no saved copies, so it quickly tells you whether the problem is on your side,
  • clearing the plugin or CDN cache - if the site caches on the server, the browser alone will not do.

Programmers like to say there are only two hard things in computer science: cache invalidation and naming things. They have a point.

How a site tells the browser what to keep

Along with each file, the server sends a Cache-Control header that says how long a copy stays valid. On Apache servers this is often set in the .htaccess file, e.g. "keep images for a year, check HTML on every visit".

So how do you update a file the browser has been told to keep for a year? Change its address. That is why page source often shows something like style.css?ver=1.4.0. A new version means a new address, and to the browser a new address is a brand new file. In other words: a long cache plus a version in the name gives you both speed and freshness.

When it matters to you

  • You cannot see changes on your own site - before you message the developer, do a hard refresh or open the page in a private window.
  • Your site is slow - server-side caching and sensible headers are usually the cheapest speed-up there is.
  • You build sites or apps - plan from day one how users will get the new version of your files after a release.

CDN, server, .htaccess, cookies, HTTP headers.

Advertisement