WordPress Cache and Optimization: the Master Post for performance

What’s different in this post that you haven’t already seen or known? This is a general rundown of what causes slowdown and what options you have to solve the problem. Each case is different so this can help you find what you need to do for your specific install. WordPress performance is highly affected by plugins, so keep that in mind. If you already use Cloudflare and are planning on skipping the section, there’s a very important thing you need to do that is not enabled by default, check the CDN section.

Resume

This article covers a lot of different options, advice, tips and tricks to make your WordPress site faster. All of them come from experience, benchmarking , failures, changing hosts, and wasted time. We have made over 30 WordPress sites and currently host more than 20 of them. The article is pretty lengthy and cover different options for optimization. If you’re using something you can’t change (like hosting providers, or your caching plugin) you can just skip over the general topic.

Here’s an example of optimization:

No caching, shared hosting with Cloudflare
Caching, shared host with Cloudflare
Caching, server migration to VPS with memory optimization, image optimization and Cloudflare

If you don’t want to read everything

For Shared Hosting: Find a host with Litespeed and MariaDB like Veerotech or a Apache + NGINX like Siteground.

For VPS: NGINX is your friend here. Use memory cache like Redis or memcached. You should read the VPS section to see what you can do. You can manage your VPS with something like Runcloud and use a reputable VPS like Digital Ocean or Vultr.

Image Optimization: Shortpixel with WebP settings enabled.

Cache plugin: If you’re willing to pay, WP Rocket is well worth it. If not, you can use Fastest Cache in a NGINX / Apache or Litespeed Cache in a Litespeed server.

CDN: Use a CDN please. Cloudflare is free.

Let’s get started.

In General

A website takes time to load for some reasons:

Server processing – the server needs to think – each time a page is requested, it must be processed by the server and then delivered (this can be “bypassed” by using static content, like HTML instead of PHP – caching does this).

Distance from the user to the servers – more distance to the servers means the information takes more time to get to the user, the number of requests aggravate this and  distance can make more difference than it seems.

Number of requests – each file that is requested by the page add time to load it. The request must go back and forth.

File sizes – Generally this involves the size of html, js and image files. File sizes affect load speed because they depend on the server bandwidth and user internet speed as well. Speed also degrades over distance, because of multiple networks jumps, throttling, package loss etc.

Server improvements are great because they can improve not only page load times for users, but for the WordPress admin area, which is important (and convenient) if you use it regularly.

Server Improvements

This is a bit more technical but its very important, and becomes more important the more concurrent users you have. So, in general you’ll have some web server applications, and those will affect your performance. If you have a site with very high traffic, besides caching you can speed it up with server improvements, like memory caching static files.

 

Web servers

First of all, WordPress is compatible with PHP 7+, so use that. PHP 7+ has a nice performance improvement over PHP 5 and unless you have a specific need for PHP 5, you should definitely switch. If you have updated WordPress plugins and installation, you can just switch over and it’ll work fine. If you use cpanel you can simply switch in the cpanel PHP settings. The best performer is PHP 7.2, but from 7 onward it’s not noticeable.

About databases. You’ll be running MySQL, but there are better performers. If you’re looking for a new shared hosting, prefer MariaDB over standard MySQL. If you’re on a VPS and you know how to do it, you should switch over as well. MariaDB is a drop-in replacement for MySQL.

Now about web servers. The most common combinations are: Apache, Nginx + Apache and Litespeed.

If you don’t already know, you should check with your web hosting what you’re currently using.

Apache

If you’re only using apache (if some host still only provide this anyway), you have to consider changing to a new web host. Seriously…  If you need to use .htaccess you should use Apache + Nginx stack OR Litespeed.

Apache + Nginx vs Nginx

The only reason to use Apache here is if you NEED to use .htaccess and what you do is not compatible (or it’s too much work to change to NGINX, since it can do pretty much the same thing). I won’t go into much detail, but NGINX acts as a reverse proxy in this case and is much better than Apache alone.

Here’s the thing, performance wise, NGINX is much better than Apache. It’s less resource intensive and more stable for large spikes in traffic, and while the Apache + NGINX stack is better than Apache alone, if you have the option to switch, there’s no reason to NOT use only NGINX. I have migrated my WordPress sites with no problem, there’s no additional configuration needed or anything. Siteground is one of the most praised WordPress webhosting and they use Apache + Nginx.

If you still need .htaccess you should check Litespeed.

Litespeed

Now here’s something some people don’t know. Litespeed is an Apache replacement, you can use .htaccess with it, and it’s performance is on par with NGINX alone, if not better. Like I said, Litespeed is on par or faster than NGINX by itself, but there’s one reason why I like Litespeed so much for WordPress sites in specific. The Litespeed Cache plugin.

We’ll talk more about it later in the cache plugins section, but it is one of the fastest WordPress caching plugins I have used (yes, as fast as the famous premium WP Rocket) and the best part, it’s free, but use it ONLY ON LITESPEED, since it takes advantage of LSCache server feature. You can use it on other servers, but the performance won’t be as good as WP Fastest Cache or WP Rocket.

Litespeed is not as popular as the other applications, but it is a really good performer. On shared hosting, if you’re looking for a affordable litespeed option, I recommend Veerotech (they also have Cloudflare Railgun integration, SSD, MariaDB). If you’re on VPS your only free Litespeed option is Openlitespeed, you can use Cyberpanel to get a full control panel, unless you’re willing to buy the license.

Shared Hosting

Improvements for shared hosting are quite complicated, they depend on the options the hosting provider gives you. If you have cpanel, you can go to optimize site > Compact all content, this enabled mod_deflate (gzip compression). Some hosting providers also give you memory caching, like memcached and redis, to use the benefit of those you’ll need some manual config or a plugin for WordPress. We’ll talk about those later. But they can improve back and frontend speeds. If you’re willing to learn a bit, using a VPS can be as cheap or cheaper than a Shared Server and much better for performance. You can check our article about Hosting WordPress for $2.5 a month on a VPS.

 

Personal recommendations

For Shared Hosting I can recommend you to look for providers with MariaDB, Litespeed OR Nginx + Apache.  There’s a ton of web hosting providers, I myself have used a bunch, but my recommendations from experience are:

Veerotech – Litespeed, MariaDB, Cloudflare Railgun

Siteground – Nginx + Apache, MariaDB, Cloudflare Railgun

 

NOTE: If you’re spending more than USD 12/month on a shared hosting and you like tinkering with stuff, I highly recommend you to take a look using a VPS like Vultr (starting at $2.5 /month only) with a manager like Runcloud (starting free with no SSL, $8/month with SSL). It makes server control as simple as using cpanel, you have your own server resources for much better performance and servers deployed everywhere in the world.

VPS

Well, VPS is complicated (but good). You can do anything you want… But let’s go over the general good setup for a WordPress VPS (in my experience). For VPS I generally use Vultr. But Digital Ocean is pretty good as well (use the reflink to get $10 for free).

My general setup: PHP 7.2, Nginx, php-fpm, MariaDB, Redis. – I don’t manually install everything, I use Runcloud to manage my web VPSs, since it makes as simple as using cpanel. If you manage multiple VPSs manually you should definitely take a look at Runcloud.

Now, I have used Openlitespeed and got even faster results on a VPS. To test Openlitespeed you can install Cyberpanel in a VPS, it is a full webserver control panel (like cpanel), but quite simple. It installs openlitespeed, phpfpm, mariadb, email server, extensions like memacached. The only reason I have not kept using it is that its pretty new and have a small team involved, so I’m unsure about stability and security. I never took it to live servers, but it looks really promising.

Redis and Memcached

Redis and Memcached can improve site speeds by reading object cache from the memory instead of disk, which is much faster. I won’t go into much detail about how it works, you can read more about them here, but if your hosting has them available you should use (only one of them).

The cost of using one of them is higher memory consumption. They’re not that common in Shared Hosting, but you can manually install them in VPS. Siteground has memcached, some other hosting providers have Redis.

You enable the WordPress use of Redis and memcached by installing the correspondent plugin. Generally speaking, Redis is a better performer, so use it if both options are available.

Plugins:

Redis

Memcached

WordPress improvements you can make

 

Most people and most articles focus only on WordPress improvements. While it’s true that it generally has the most impact of all the things you can do, WordPress improvements still limit you to what your server has to offer.  If you’ve skipped server improvements, it’s still possible to get sub seconds load times and use pretty much no server resources, but your admin area will probably still be slow, unless you currently have huge database plugin hogs or badly optimized plugins and fix that.

Making a really fast WordPress (for users) is actually really easy, you just need a good caching plugin + a CDN and some image optimization. That’s it. We’ll talk more about this when we go to the last section, the CDN. Now we’ll cover how to improve WordPress itself.

Reduce HTML Bloat and plugins

I put reduce HTML bloat and plugins together because WordPress by itself is pretty fast and lightweight, with little requests. So what increases the number of requests is custom code and additional plugins. The problem is that it’s so easy to add plugins and themes to it that it becomes extremely bloated, sometimes for things that don’t actually need plugins. Some plugins are badly optimized as well which is the worst case scenario. The overall consensus about plugins is: the less, the better and if you can do it without a plugin, don’t use one.

First thing you can do is remove any active unnecessary plugins. This reduces requests, database querys and improves security. Also, if you’re done developing a site and/or have a staging to test this, disable all the plugins. Activate one by one checking the Chrome network tab. You can do this to see if there’s a plugin slowing your admin panel down. You do this by enabling developer mode in Chrome (Ctrl + Shift + I) > Network Tab and press F5 – repeat multiple times after each plugin to see if it’s not a false positive and make sure “Disable cache” is checked.

Second is taking a look at the number of requests your site make. Pingdom tools can help you, or the Chrome developer network tab again. How can you use this? You’ll see what your site asks for individually and give you an idea of what you can improve. Cache plugins generally help here, since they combine resources, making less requests. But sometimes a single plugin has like 3 different javascript/css calls. So while looking in itself won’t solve the problem, it may help  at least understand whats happening in your site and decide what plugins to keep/change or lines of custom code to improve.

Third is database optimization. Database is always using memory, even when not in use, and the more stuff in it, more time it takes to deliver content. If the server has limited memory (between php, memory cache, database) it takes longer load the content, even disabled plugins still leave footprint, so don’t go installing everything you can on a live site. You can optimize database with optimization plugins (more plugins, the irony…), or by accessing it (phpMyAdmin for example), looking at the tables and deleting unnecessary ones (make sure you make a backup before and you know what you’re doing).

WordPress Cache

Caching is the most effective and important thing to improve WordPress performance. Caching can reduce speed multiple times.

How dos caching work? Basically When a fronted Page is loaded, it needs to be processed by the server. The more stuff the server needs to do, more time it’ll take to load. Plugins and theme functions will slow down the page load.

That’s where caching comes into play.

WordPress caching generally involves creating a HTML static file from the PHP generated one and storing it in the disk (or memory). Static content doesn’t require processing from the server, you just need to deliver the content so it’s way faster to load. So instead of processing, you just have the disk speed (and bandwidth, distance) to deliver the content. It can be even faster using some web server specific caching like Nginx to store HTML files in the memory.

Caching plugins

Like I said, this article is based on experience. I have used and tested a lot of caching plugins and while this is NOT a benchmark comparison, I’ll post the ones I have successfully used and recommend. The best way to decide for yourself is to test it.

WordPress Fastest Cache (Free): Out of the free ones I have tested (WP Super Cache, W3C Total Cache and others), this one had the best results overall, consistently. And with the amount of sites we maintain, the simple setup makes this a pretty easy choice. For me this was: consistent (in multiple different types of sites), Compatible (with very few exceptions, I could enable most of the minifiying and combining scripts options) and fast. Note: I have not used the paid version to compare. If you’re using the free version you can combine it with Fast Velocity Minify to reduce requests and make more adjustments.

 

UPDATE: Swift Performance Lite (Free): This one is kinda new. But it gave similar results do WP Rocket, so it is very impressive. It has a simple setup, that’s good as well. So maybe if you’re looking for a free option you should check it out. It didn’t reduce the number of requests as much as Rocket, so keep that in mind.

WP Rocket (Paid): Well, WP Rocket is generally considered the best caching plugin, end of story. After using Fastest cache (Free) and using Rocket I have to say I was impressed. It actually reduced the load times even more. It has more options than fastest cache and you have to test more for compatibility, but there are a general set of options you can enable and have a pretty good result.

Litespeed Cache (Free, requires litespeed):

Note: DO NOT USE LITESPEED CACHE WITHOUT LITESPEED SERVER. It is worse than other caching options out there without LSCache server feature.

Litespeed Cache plugin is a bit more advanced, you need to enable the advanced tab and check some minifying options to get a standard setup. With pretty much the same settings enabled, I got better results than WP Rocket, but the margin was so little that even though it was the same VPS config it could be some VPS differences. The reason why the plugin is faster than WP Rocket is probably not because the plugin in itself does a better job of caching. It is probably because it uses the LSCache feature from Litespeed servers, so it’s using a server caching to make even faster delivery times. It also has Object Cache settings which WP Rocket doesn’t have (Like Redis, Memcached), so no need for another plugin and faster wp-admin as well.

WordPress speed tests

All the tests were done in a Vultr VPS managed by Runcloud to keep consistency. The tests were run multiple times and the best result was selected. This is not a benchmark comparison but it’ll give you an idea. Keep in mind, while I installed some plugins, the install was very clean overall and had only 8 plugins. After every test the config file was checked and the cache folder deleted to make sure there were no leftover, the server was reboot as well. Cloudflare cache was also disabled of course. The server was in Miami, Florida, USA.

No caching

WP Fastest Cache

Swift Performance Lite

WP Rocket

 

One more test with Cloudflare in the CDN section.

Image Optimization

 

Image optimization is a very important part in page load times. Using a cache plugin or Cloudflare you’ll generally get minifying, so the html and js files will be as small as they can. What’s left are images.

There’s a couple of ways to go on about this: optimize before upload or after. I use the later because its more convenient, but when uploading files at least you should be reasonable to avoid obvious large files.

You can compress images before uploading with lots of online tools, like tinypng or with Photoshop and other software. But since we’re talking WordPress I’ll talk about the plugin for image optimization I personally prefer over the others.

Shortpixel

Why Shortpixel? The first thing is the simplicity. Its pretty much install and use it. You don’t need to change anything to make it compress well. Second is that I get much better compression than other plugins like WP Smush. And third, a differential from some others is the WebP conversion and implementation.

Shortpixel will compress jpg, png, gif, PDF, but you can go a step further and convert images to WebP for even more savings (about 10% more). The best part is that it has a setting to add the markup automatically, so for compatible browsers (cough… Chrome only) it will automatically display the WebP version, otherwise it’ll display normal images, and it doesn’t make any extra requests to do that.

The biggest downside of Shortpixel is the free image limit of 100 per month. But it has very friendly prices and is well worth it. You can buy a one time pack, optimize all your images and stay with the 100 images/month as well. Use this link to get 100 free extra images on your first site.

Here’s a new pagespeed, now with WP Rocket and Shortpixel

Now with Shortpixel and WebP settings enabled

Note: Using the webp and keeping backups of the original images will increase your inode usage. If you use a Shared Hosting and you have a lots of images be careful.

CDN – Content Delivery Network

 

Now to end the article with something you should be using if you are not currently.

CDN is a way to make content be delivered faster to users.

First let’s understand how it works normally: When a user requests a page, it has to go back and forth between the user computer and the server. This means that the farther the server, the more time it’ll take for the content to get back. The more requests (external resources) on a page, the more aggravating it becomes.

But some content, like static files (html, images, js) doesn’t need to be processed every single time, so they can be served by a CDN. What a CDN does is store static files in multiple distribution datacenters located everywhere in the world. So when a user requests a file, it’ll be served from the closest server to him. This reduces latency since there’s less distance, reduces server load, since you don’t actually even need to hit the server as long as you serve completely static content and reduce bandwidth usage for your hosting, again, you don’t need to hit the server.

That’s why the CDN + Caching is so powerful and important for WordPress. If setup correctly on a mostly static site, there’ll be no need for the request to even go to the server. The CDN will cache the static content it needs and serve as requested. This is the reason why a simple low end shared hosting can still deliver really fast page loads (with slow admin panel) – after processing the static files and deliverying to the CDN, the webserver won’t even be used.

The best part is that Cloudflare offers free CDN. I won’t go in-depth about all the CDNs options and what you can use. My advice is, if you don’t already use a CDN, start using Cloudflare to see how it works, then if you see the need change to a paid one.

Now lets activate Cloudflare and see the pagespeed for WP Rocket

We can see that it is much faster than without a CDN. The reason is mostly the distance. Since the server is in the US, even though it has the same static content (HTML, JS, images) it takes more time to get a response from Stockholm. Distance is very important, so always try to get your main server as close as possible to your users.

Now, to get this level of caching there are 2 things that are very important to note:

VERY IMPORTANT FOR PERFORMANCE WITH CLOUDFLARE

You NEED to activate Cloudflare HTML file caching that is not enabled by default. And you have to do it on every domain.

You can do this by following this tutorial from Cloudflare.

You NEED the ” *.html “ part in the end or it won’t make any difference.

Something else to note:

With this level of caching you may have problems with development and content update on frontend pages (admin should be fine) – Cloudflare will keep the cache for as long as it is defined in the Edge Cache, so you’ll need to purge it on the Cloudflare site. Make sure to disable Cloudflare caching in this case.

This means that if you update your site, post something you’ll need to manually purge the cache (if you don’t set the Cloudflare API in WP Rocket or your plugin of choice), or just set the Edge cache for a reasonable timeframe (if you have lots of visits and a site that is updated multiple times a day, maybe 2h cache should be fine).

If your site is highly dynamic (updated more than every 1h) and have lots of traffic you’ll have a bit more trouble here as you may not be able to store static HTML, but maybe if your site is SO dynamic like that (maybe with realtime updates for something?) you may be better using another plataform than WordPress.

I hope this post has given you some insight besides “use that plugin or that one”.

That’s it for now. If you guys have any  question feel free to ask in the comments.

Leave a Reply

Close Menu