Redis and object caching can vastly speed up your WordPress page load times with each subsequent visit. It’s also used by many popular websites like GitHub, Pinterest, StackOverflow and many others.
Remote Dictionary Server (Redis) “is an open source, in-memory data structure store used as a database, cache, and message broker.” It’s a key-value store which is often called a NoSQL database.
It’s best used on dynamic websites such as WordPress sites when it comes to object caching, which caches repeating query results.
Today, I’ll share more detail on object caching, its benefits, and how to install and use Redis for object caching on WordPress websites.
A Quick Refresher: What is Caching?
If a web page is cached, it means that elements of that page such as images, stylesheets, and other content is loaded once, then stored in what’s called a “cache.”
It memorizes what was loaded, creates a static version of it, then can serve that version much faster the next time that page is loaded.
The result is faster page load times and less of your server’s resources being used.
Check out Caching for WordPress, Explained in Plain English for further details.
What is Object Caching?
Object caching is a type of server-side caching. This means the caching is administered at the server level, and isn’t controlled by the end user or a system they use for caching.
Object caching stores database query results that have been loaded. Then, it serves them up faster the next time they’re requested so the database doesn’t have to be queried again.
WordPress also has object caching built-in with the WP_Object_Cache class.
The trouble is, the inherent object caching WordPress has isn’t persistent by default. This means that cached data is only stored for as long as the request to the database lasts, which is ultimately no more than for one page load, and inefficient.
If you were to install and use a persistent object caching solution such as – ehem – Redis, for example, data could be cached for all subsequent page loads, giving your database more of a break..
For details, check out What is Object Caching and How to Use It With WordPress.
Who Needs Object Caching?
If your WordPress site is static and all it needs to load are a stylesheet and some images, for example, you’re not going to see any difference in you use object caching.
Conversely, a dynamic site loads tons of data across pages that are stored in your database such as user details, taxonomies, links, and other similar data.
As previously mentioned, every time a page loads that content, it sends one (or often several!) database queries. If you use object caching, that data is stored in the cache and it’s ready to be displayed on the page in the flashes of flashes.
Your database can be queried much less often and retrieving content from the cache is a lot faster than sending queries to the database.
This results is page loading times that are a lot faster. Your server’s resources are also used more efficiently. This is an especially crucial factor if you’re looking to scale your WordPress website.
So, if your site gets a lot of traffic or you’re expecting it will soon, and it’s dynamic, you should consider using object caching.
Memcached vs Redis: What’s the Difference?
Memcached is another persistent object caching WordPress option that you could use instead of Redis. It’s a popular option and works well, though, it has a smaller set of features when compared to Redis.
This is why Redis is a far more popular option. But, that doesn’t necessarily mean you should completely write-off Memcached, especially when it’s used by top sites such as Facebook, Twitter, YouTube, and more.
Weigh your options by considering the features you need to help you decide which one is right for your WordPress site since each case is different.
For details, check out Memcached: What Is It And How To Use It On Your WordPress Site?
Using Redis Object Caching on WordPress
Many managed WordPress hosting solutions already offer Redis object caching so you can check with your host to see if it’s an option. They may have a quick or one-click installation available.
If you find you need to install it yourself, you can do so manually.
If you decided you want to manually install Redis for object caching on your Linux server, there are a few other prerequisites:
- Root access to your Linux server
- Ability to install Redis through SSH access
- WordPress installed
- PHP version 7.0 or higher
- Server has both Wget and Sudo installed
- Text editor is installed on your server such as Nano
- Phpize is installed to prepare PHP extension for compiling
Be sure to also back up your entire site in case something goes wrong and you need to restore it. It’s also a good idea to test Redis before deploying it on a production server.
1. Installing Redis for Object Caching
While you could use the package manager of your specific Linux distribution such as apt or yum, it may not always be up-to-date with the latest version so it’s recommended you use wget with a link to the master archive.
On the command line, enter the following to install the latest stable release:
Next, type the line below:
Then, follow it with this:
Finally, enter the following:
2. Configuring Redis as a Cache
It’s necessary to edit the configuration file now that Redis is installed to, well, configure it to use it specifically for object caching.
To get to the configuration file, use the line below:
Note that “nano” is the command to access the file using the Nano editor. Replace this with the applicable command if you have a different editor installed.
Find where it says the following:
Then, replace it with this:
Keep in mind that you can change “128” to suit your needs.
For example, 50 MB is suitable for many WordPress installations, but if you run a high-traffic site, 128 MB or 256 MB may be a better fit. Use what works best for your site.
3. Edit the wp-config.php File
By default, Redis is now set up to have any stored data accessible to all apps on the server. Since this is a security risk, it’s important to edit the wp-config.php WordPress core file to include a cache key salt.
Even if you only plan on having WordPress on your server, it’s still crucial not to skip this step in case you decide to install Multisite or include other apps on your server in the future.
Open the wp-config.php file and above the “happy blogging” line, add the following, then save the file to your server:
Don’t forget to replace “example.com” with whatever you want. It doesn’t have to be your site’s domain, but be sure it’s unique.
4. Installing a PHP Extension
It’s also essential to let WordPress contact the Redis key-value store so you can enable object caching. To do this, you need to install a PHP extension such as PHP Redis.
Go back to the command line and enter the following:
Follow it with the line below:
Then, this command:
You now have the source code downloaded and extracted.
To install it, enter this:
Then, type the line below:
Follow it with the word below:
Also, enter the command below:
Now that PHP Redis is installed, you need to create a new INI file. You can do this with the line below:
Be sure to replace the “x” in “7.x” to reflect the version of PHP 7 you have installed.
Keep in mind that if you’re not using Apache, the file path will need to be updated to reflect the file hierarchy you have on your server.
5. Restart Redis and PHP
It’s critical you restart PHP and Redis to apply the changes you made above.
You can restart Redis by entering the command below:
Then, restart PHP with this:
Note that if you’re not using an Apache server, use the PHP restart command that applies to you.
6. Verify Redis is Working
Now that Redis is installed, it’s time to check that it’s working properly by running the following command:
If you see the “PONG” response, Redis is set up and working.
7. Install and Activate the Redis Object Caching Plugin
Next, install the Redis Object Caching plugin. Now that you have finished the ground work, it installs like most other plugins.
For details, check out Installing Plugins in the WordPress Codex.
WP Rocket and Redis
While WP Rocket doesn’t have Redis object caching built-in since its focus is on page caching, it’s still compatible. You can certainly use WP Rocket at the same time as you use Redis.
Boost the performance of your site with Redis + WP Rocket: Get WP Rocket today!
Object caching can increase your WordPress site’s page load speed drastically, and you can make it happen with Redis. It takes some configuration, but if you follow the steps above, you’ll be set up and ready to go.