Do you have WordPress sites that need to move faster? Maybe you’ve already got a good page caching plugin like WP-Rocket, but the site admin is slow? Redis Server is the answer.
What is Redis Server? It stands for Remote Dictionary Server and it deals with caching database queries for WordPress. As you probably know, WordPress relies heavily on a MariaDB database (or MySQL), and depending on the size and load of your server, database queries can add quite the drag to the user experience.
WordPress has database query caching built in — It’s called WP Object Cache. This cache protects your site from doing the same query to the database more than once for each page load.
For example, the home page of my site generates over 100 queries on load!
But there’s a SMALL PROBLEM. Read from WordPress.org:
WP_Object_Cache is WordPress’ class for caching data which may be computationally expensive to regenerate, such as the result of complex database queries…
…By default, the object cache is non-persistent. This means that data stored in the cache resides in memory only and only for the duration of the request. Cached data will not be stored persistently across page loads unless you install a persistent caching plugin.
Did you catch it? WP Object Cache is NON PERSISTENT.
What’s that mean? That it only works for each single page load.
Let’s say page number uno of your site uses 50 of the exact same queries as your sub pages. WP Object Cache starts over for each page! No!!!!!
So what’s the solution? I give you Redis Server.
Actually, I don’t give you anything. It’s the tech gurus at redis.io that give you this little gem. I’m simply talking about how cool it is, and how to install it on Debian and your WordPress site.
Step 1: Install Redis Server on Debian
apt install redis-server
That’s the command to install it. You’ll of course want to do an apt-get update, but we all know that already. Next, install the PHP extension:
apt install php-redis
Then make sure you have Net Tools installed, and we’ll use that too:
apt install net-tools
Step 2: Configure Redis Server
After you need to install Redis Server, you need to make sure your binding is set correctly, and we’ll change a couple other settings while we’re in the config file. Open the redis server config file in whatever way you prefer to edit server files:
Look for the following bind statement:
bind 127.0.0.1 ::1
This is repeated more than once in the configuration, and the default Redis config should already have one of them uncommented. What this is doing is telling Redis to bind both IPv4 and IPv6. Lose the ‘::1’ if you aren’t using IPv6.
Next, look for the maxmemory line. For my VPS with 16GB of RAM I set mine to at least 1512MB or 1.5GB
Next we need to tell Redis how to clear memory when it fills. Look for:
You want this set to allkeys-lru so that if your memory allotted memory is full (the maxmemory setting) Redis can evict the oldest unused keys taking up memory.
Next we need to find the following three lines and comment them out:
save 900 1
save 300 10
save 60 10000
Why? Commenting them out will keep Redis Server from writing to disk. You write to disk, you lose performance.
Restart redis server, nginx and php7.x-fpm for the changes to take affect:
service redis restart
service nginx restart
service php7.3-fpm restart
Step 3: Start Redis on Reboot
If you want Redis Server to start automatically on reboot, enter this on the command line:
systemctl enable redis-server.service
Step 4: Verify Redis is Running
netstat -lnp | grep redis
That command will tell you if Redis is running. You should see the following which is a line for IPv4 and a line for IPv6:
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 7810/redis-server 1
tcp6 0 0 ::1:6379 :::* LISTEN 7810/redis-server 1
You rock. Redis server is running! Now it’s time to move to configure WordPress to work with Redis.
Step 5: Edit WP-Config
First you’ll need to edit the wp-config.php file found in the root folder of your site. And btw, if you’re a MAC user, ForkLift3 combined with Atom is simply the best sFTP client and code editor out there.
With your wp-config.php file open, look near the bottom for the line that says something like, “Stop editing! Happy blogging.” Add the three lines below right above that line.
define( 'WP_CACHE_KEY_SALT', 'yoursite.com:' );
define( 'WP_REDIS_SELECTIVE_FLUSH', true);
Sanity Tip: Make sure you replace ‘yoursite.com’ with your site’s name. Adding a key prefix like this to the end of the key salt directive allows you to have more than one WordPress site on the same server utilizing the same Redis database. If you don’t differentiate them, you’re gonna have problems.
Step 6: Install Redis Plugin
If you’re like me you don’t want to use another WordPress plugin. And yeah, you can Google around and find PHP code to add to your site — That hasn’t been updated in like 3 years. Trust me, just install Redis Object Cache by Till Krüss. It works. It’s maintained. End of story.
With the plugin installed, go to the plugin settings and enable the cache. Once it connects you should see the key prefix you set in the wp-config.php and a status of connected as shown below:
With the plugin installed and enabled, WordPress is configured to work with Redis! Start clicking around in your admin. For me, it took a minute, but then things got insanely fast. I was so excited!
Step 7: Monitor Redis Server
If you’d like to see that redis is for sure working, hop back to your server command line, and type in the following command:
This will show an exhaustive output every time you click on a new page on your website (whether in the admin or the front end) via Redis Monitor. Hit Ctrl-C to get out of the feed. And know that running Monitor hurts performance, so don’t go for coffee and leave it running.
redis-cli memory stats
This nifty command shows the memory utilization of Redis Server. Remember, we’re using the LRU setting, so filling up the memory won’t bring down your sites, but performance will be hit. The memory stats command allows you to see if your max memory setting is set at a good amount.
Sanity Tip: The output from the above command displays a lot of information. First it will show the metric following by the value, which begins with (integer). The first metric is ‘peak allocated’ and the value is in bytes. So 2249336 bytes would be roughly 2.24MB.
See more Redis Server commands here: https://redis.io/commands
That’s a wrap! If you followed the steps you’ve installed Redis Server on Debian and configured WordPress to use it. You’re now able to get more juice out of your VPS, which means happier clients with less money spent.
It just doesn’t get better. Thanks for checking out my blog!