Setup Redis Object Cache to Optimize WordPress Database Usage

Learn how to set up a Redis Object Cache that helps in reducing the frequent MySQL database calls by caching the complex queries, and serve cached output.

Just like caching the front-end static files play an important role in improving the site speed. Similarly, we need to cache the backend for enhancing server performance. Therefore, we will use the Redis Object Cache.

The Redis Object Cache helps in storing the complex MySQL queries on RAM memory. Hence, next time any similar query request, the Object cache will serve the output stored in RAM rather than hitting the fresh MySQL query. Thus, enhancing the overall speed and performance.

In this article, we are going to cover how to install and configure the Redis Server, and also enable the Redis Object Cache using the WordPress plugin.

If you’re following, this is a tenth guide and video demo in the #CloudServer series. We recently posted about setting up FastCGI Nginx Cache on the LEMP stack.

Let’s dive into configuring the Redis object cache for WordPress blog —

Install Redis Server

Execute below command to update the APT repository of our Ubuntu Cloud Server.

sudo apt-get update

Install the redis-server as well as the php7.3-redis module for supporting the WordPress Object caching.

sudo apt-get install redis-server php7.3-redis

install redis sever and php7.3-fpm on Ubuntu

Configure Redis Server

Now that we have installed the Redis server, we will adjust the configuration file to improve its default configuration. Edit the redis.conf using below command:

sudo nano /etc/redis/redis.conf

And add the below lines to set the maximum memory limit the Redis Server should use for processing the Object Cache.

#You can adjust this value as you see fit - 200mb or 20000mb
#it depends on how much RAM you have. On a 1GB server, I use 100mb.
maxmemory 100mb
# this forces old keys to be deleted using first-in-first-out
maxmemory-policy allkeys-lru

Setting maxmemory and allkeys-lru in redis.conf file

Further down in the same configuration file. Comment out these lines in Redis to use only memory instead of the storage disk.

#save 900 1
#save 300 10
#save 60 10000

The SSD disk writing speed is quite low compared to RAM memory, hence we will be saving the Redis in RAM memory cache instead of disk.
redis configuration comment out save RAM memory instead of disk

Now, close the configuration file and restart the Redis server and php7.3-fpm using below command:

sudo service redis-server restart
sudo service php7.3-fpm restart

The Redis server has been configured and running on the Ubuntu Cloud server.

Setup Redis Object Cache for WordPress

We have completed the backend setup of Redis Server. Now we will set up the Redis to run as Object Cache for WordPress Database queries.

The process is really simple. We just need to install a WordPress Plugin called Redis Object Cache by Till Kruss and enable the Object Cache.

Follow the below steps for Redis Object Cache on WordPress:

  1. Login to WordPress Dashboard
  2. Navigate to Add New under Plugin menu
  3. Search and Install Redis Object Cache by Till Kruss
    Redis Object Cache WordPress Plugin
  4. Activate the Plugin and navigate to Settings > Redis
  5. Hit on Enable Object Cache command button
    Enable Object Cache in WordPress
  6. That’s it!

Redis Object Cache Enabled in WordPress

The Plugin will automatically add the required files in our WordPress server directory and enable the Redis Cache for MySQL database optimization.

Monitoring Redis Object Cache

We have completed the setup with Redis Server configuration as well as using it on WordPress for Object Cache. In order to monitor Redis Object Cache performance, we can run the following command in the SSH console.

redis-cli monitor

redis-cli monitor working with WordPress
This will output the real-time data of Object Caching used by WordPress.

Video of Redis Object Cache for WordPress

Watch the video demo of Redis Object Cache setup for WordPress blog. I’ve recorded the step by step guide of the entire Redis Server guide in this tutorial.

Enable Redis Object Cache for WordPress Database Optimization

I hope you liked the video, please subscribe to our channel.

What’s next in the #CloudServer series?

Now that we’ve learned about setting up Redis Object Cache with WordPress. Next, we will go through managing the WordPress MySQL database using WP-Optimize Plugin.

If you've any thoughts on Setup Redis Object Cache to Optimize WordPress Database Usage, then feel free to drop in below comment box. If you find this article helpful, please consider sharing it with your network.

Also, please subscribe to our restoreBin YouTube channel for helpful videos.

Disclosure: This page may contain affiliate links, which means we may receive compensation for your purchases using our links; of course at no extra cost to you (indeed, you may get special discounts). This will support our effort to create more useful content, and keep it open and free forever.

5 Responses

  1. diegoJC says:

    Hi Kushal, thanks for the post.

    What should I do if I need to prevent some pages or things to be cached?
    And what If I use a builder like Elementor? do I need to do something for it to works?

    Thanks again man.

    • Hi Diego, as far as I know, the Redis Cache plugin does not have a feature to restrict any specific page. However, WordPress plugins and themes are ignored. Hence, the Elementor page builder queries will be ignored for caching.

    • simplysexyboy says:

      redis is caching object not the entire page. It means only the data.
      so you dont need to exclude any page nor you should afraid of that.

      If you real want to afraid , afraid of fastcgi woocommerce cache 😀

  2. Ronak R Prajapati says:

    With what i should go with. WordPress with nginx or redis?

    • Kushal Azza says:

      Hi Ronak,

      You can use both Nginx and Redis for making WordPress powerful. Follow this guide –> WordPress on DigitalOcean.

      Instead of DO, you can use any SSD Cloud Server, however, I personally use and recommend DigitalOcean(DO) every time.

      Regards //KushalAzza

Leave a Reply

Your email address will not be published. Required fields are marked *