Real Talk on WordPress Performance: 5 Must-Haves for High-Speed Sites
This article was originally published by Josh Koenig on Dec. 21, 2015 on the Pantheon blog.
I was honored to be selected to give a Lightning Talk slot on performance at WordCamp US earlier this month in Philadelphia, and got a lot of positive feedback on the presentation. You can check out the video, but fair warning: it goes pretty fast.
For people looking to digest these ideas and/or follow through on some of the recommendations I make, this blog post should serve as a decent reference.
Starting with the Real Talk
We’ll get to the five must-have “listicle” in a minute, but I wanted to start out with the “Real Talk” part of the presentation. There are a lot of different tips and techniques out there for optimizing web performance — ranging from solid best practices to more dubious “one weird trick” type tweaks. Blindly following tactical advice can leave you in a bad space.
The concepts that I call “Real Talk” are important things to understand before you start down any particular implementation path. Consider these things first.
Real Talk: The Keys to Breakthrough Performance Aren’t Inside WordPress
You can’t fix performance with just a plugin. The keys to optimizing the performance of your website in a game-changing way are in understanding the “full stack” at work, and implementing best practices for the runtime environment and other surrounding layers. Getting breakthrough performance requires going outside the WordPress bubble.
Real Talk: It’s Caches All The Way Down
Most things performance-related in computing come down to cache optimization. Preventing unnecessary re-computation is the single most important thing, and caches exist at every layer in the stack, sometimes several at a time. This is fundamental knowledge for any developer who is keen on optimizing for performance.
Real Talk: WordPress is the Smartest and Slowest Thing in Your Stack
Did you know that a vanilla WordPress homepage with just one post compiles down to billions of raw CPU instructions? It’s an awesomely extensible, infinitely flexible, hookable, pluggable, page-slinging genius machine. But it’s also relatively slow (in computing terms) compared to simpler subsystems that focus on doing one thing with maximum efficiency.
Real Talk: The Database is the Ultimate Bottleneck
At the end of the day, every single time WordPress executes, it needs to query the database. And if that database isn’t responsive and performant, every page load for that site will suffer. Moreover, the rate at which database performance can degrade — e.g. due to a “query of death” — can be sudden and deadly, just as the name suggests.
Real Talk: You Can’t Throw Hardware at a Problem and Expect it to Stay Fixed
If you have serious performance problems due to a ramshackle site — bad queries, blocking http calls, etc. — you may be able to get some quick wins by throwing hardware at the problem, or by adding some clever caching. However, those types of solutions typically don’t hold up over time, especially if your site’s traffic or content footprint continue to grow.
Real Talk: It Takes Constant Improvement to Have Great Performance
If performance is important for you, you have to understand where your bottlenecks are, and how hardware (or caching) are going to help alleviate and prevent these issues. While big wins exist, there are no quick fixes.
The Five Must-haves
These are not “one weird tricks”. They’re not experimental. They’re widely accepted best-practices that can help every site. You might not get them if you don’t ask though, so be sure you’ve got these all checked off:
#1: Modern PHP With Opcode Cache
More recent versions of PHP are between 20 and 100% faster than trusty old 5.2. If you aren’t pushing to be on a modern version of PHP, you are missing out on one of the quickest wins available.
Likewise, you have to use an Opcode Cache, either with the APC module (prior to PHP 5.5) or with the built-in OPcache thereafter. This will dramatically reduce the number of CPU cycles WordPress requires, so make sure it’s set up and working properly for you.
#2: Persistent Object Cache
WordPress does a lot of computation creating data objects that are thrown away at the end of a page load. Those objects then need to be regenerated — first by quering data out of the DB, and then unserializing or compiling them in PHP — every single time, even though usually nothing has changed.
A persistent Object Cache saves that labor, keeping load off the DB and reducing the CPU overhead for PHP. Pantheon supplies Redis as built-in object cache, but you can also use Memcached if you like.
#3: Reverse Proxy-Page Cache
Serving cached pages is a simple job, and one that’s best “outsourced” to another system. Implementing a simple reverse-proxy in front of WordPress can literally deliver up to a 200x improvement in speed and volume.
Serving cached pages at scale is a problem that the wider web community has spent a lot of time on in general. There are wonderful open-source solutions (e.g. Varnish, Nginx) as well as commercial CDNs that can do this much better than WordPress can left to its own devices. This is how all kinds of sites (WordPress or otherwise) breeze through “internet scale” traffic spikes that come from one particular page becoming super popular.
#4: SSD Databases with the InnoDB Engine
The database is your ultimate bottleneck, so make sure you’re not fighting with one hand tied behind your back. If your DB isn’t running on Solid State Disk (SSD) drives, then you are literally wasting time waiting for spinning metal disks to read data. Don’t do that.
Likewise, if you aren’t using the InnoDB storage engine, you risk queries being stuck waiting on table-level locks in the database, which can lead to serious logjams. As an added benefit, InnoDB also has the best chance of preserving your data in the event of a system crash.
#5: A Little Expertise and Willingness to Learn
The challenge of website performance is generally proportional to the complexity of the site, and as sites get more and more featureful, the obvious “must have” answers might not be enough. If you’re a site owner with performance problems, consult a developer you trust before you implement any changes.
On the other hand, if you’re a developer interested in performance, dive in. There’s no better way to become an expert than to start gaining experience, and these skills are not going out of style anytime soon.
Bonus #6: A Dedicated Search Index
I didn’t get to this in my Lightning Talk, but it’s another no-brainer. If site search is an important part of your site experience, you need a real search index. The default WordPress post search doesn’t do relevance, and it can easily bog down the database, which in turn can quickly spill over to affect site performance in general.
The popular open-source alternatives are ElasticSearch and Apache Solr — which we bundle into the platform — and there are other cloud services that can also help solve this problem. The main point is that if you expect a lot of searching, get it out of your database ASAP.