What is caching and why does it make a site faster?
What is caching? Caching means storing a result so that it does not have to be built or fetched again every time. Without caching, your server builds every page from scratch for every visitor, even when nothing has changed; with caching, the visitor gets the stored result straight away. There are five layers: the browser cache, the cache of a CDN, the page cache, the object cache and the opcode cache of PHP. If a change does not show up, you are almost always looking at one of those layers.
The five layers, from visitor to server
- Browser cache. The browser of the visitor stores images, style sheets and scripts locally. A second page of your site loads much faster as a result.
- CDN cache. A CDN such as Cloudflare keeps copies of your files on servers close to the visitor. By default this covers static files such as images, CSS and JavaScript, not the pages themselves.
- Page cache. The fully built HTML page is stored. PHP and the database are no longer involved for the next visitor. This pays off most on sites that change little. With WordPress, a caching plugin takes care of it.
- Object cache. The answers from the database are kept in memory, usually with Redis or Memcached. This mainly helps busy sites and webshops, where the same data is requested hundreds of times a minute.
- Opcode cache. PHP first translates every script into executable instructions. OPcache keeps that translation in memory, so it does not happen again on every request. You do not notice it, except that a changed PHP file sometimes only becomes active a little later.
"I cannot see my change"
The classic confusion. You change something, reload the page and nothing looks different. Do not conclude that the change failed; work through the layers instead, from nearest to furthest:
- Open the page in a private window. That window starts without stored files and without a login. If you do see the change there, it was your browser cache.
- Force a full reload: Ctrl+F5 on Windows, Cmd+Shift+R on a Mac. The browser then fetches all files of the page again.
- Clear the page cache of your CMS or caching plugin. If you see the change when you are logged in but not in the private window, this is almost certainly the cause: logged-in users usually do not get cached pages.
- Clear the CDN cache if you use one. With Cloudflare you can purge a single address or everything.
- Did you change a style sheet or a script? Those stick around longest, because browsers are allowed to keep them for weeks. Give the file a new version number in the file name or after the question mark in the address; every browser then treats it as a new file.
- Check that you edited the right file. A change in a copy or in an inactive theme never becomes visible.
When to clear the cache
- after a change to the design, the menu, the footer or a widget, because those appear on every page
- after an update of the CMS, the theme or a plugin
- after a migration or the switch to https, so that no old addresses remain in stored pages
A good caching plugin clears the page you edit by itself. Clearing the entire cache every time is counterproductive: the server then has to build all pages again.
What you never cache
Pages that differ per visitor: the shopping cart, the checkout, the account, and forms with a security token. If those end up in the page cache, one visitor sees the data of another, or a form works only once. Caching plugins usually exclude the standard WooCommerce pages by themselves; check it after you restructure your shop.
Common mistakes
- Two caching plugins side by side. They store each other's output, and clearing then never works completely. Pick one.
- Using caching to hide a slow site. A page that needs eight seconds without a cache stays slow for the first visitor and for every logged-in customer. Find the cause first; see why is my website suddenly slow.
For WordPress, the full approach, with caching as one part of it, is on speed up WordPress. How speed is measured is explained in what are Core Web Vitals.
Frequently asked questions
Do I lose anything when I clear the cache?
No. The cache only contains copies. Your pages, images and database stay untouched; the copies are built again on the next visit.
How do I clear the cache on my phone?
Open the page in a private tab: that is the quickest test. To really clear it, go to the settings of the browser, under privacy or browsing data. You do not need to delete cookies along with it.
Is the DNS cache the same thing?
No. The DNS cache remembers which server your domain name points to, not what is on the page. It only matters after a migration or a DNS change. See why a DNS change sometimes takes so long.
Does a small site need caching?
Browser caching and the opcode cache help every site, and they are usually switched on already. A page cache pays off as soon as your site runs on a CMS. An object cache only makes sense with many visitors, a webshop or a members area.
Still stuck?
If a change stays invisible after you have cleared every layer, send us the address of the page and what exactly you changed via contact.