How to Speed Up Your WordPress Website with Redis Cache
Tags: wordpress
Published: 2020-11-30 03:11:42

In the previous article on <a href="https://gretathemes.com/cache-plugin-speedup-website/">the best cache plugins</a>, we introduced the most popular cache techniques and object cache is one of them. This technique is often combined with supporting software such as Redis, Memcached. In this article, let's find out <strong>what is Redis cache and how to use it to speed up your WordPress site</strong>.

<h2>What is Redis Cache and Its Benefits?</h2>

Redis stands for Remote Dictionary Server. It's an open-source, in-memory key-value data store. It can be used as a database, message broker, and of course, cache. Redis stores data in RAM, so its accessing speed is quite fast.

Redis cache is based on <a href="https://gretathemes.com/cache-plugin-speedup-website/#object-cache">object cache</a>. It stores data about objects that are usually requested by clients into RAM. Thus, web servers can use these data without accessing the database. This reduces the server load and makes your site run more smoothly (to get more details about how the web server handles requests, refer to <a href="https://gretathemes.com/cache-plugin-speedup-website/">this article</a>).

Normally, Redis cache is used for websites having huge traffic or web server services. It's designed for Linux servers so you can install it easily on Linux distributions (CentOS, Ubuntu, Debian, ...). In this article, we'll <strong>install Redis cache on CentOS and Ubuntu</strong>.

<h2>How to Install Redis Cache on Your WordPress Website</h2>

To install Redis cache on your WordPress site, follow these steps:

<ol>

   <li>Install Redis on your web server</li>

   <li>Install Redis PHP Extension</li>

   <li>Install Redis Caching plugin</li>

   <li>Verify Redis Caching on WordPress</li>

</ol>

If you implement these steps exactly, it won't be difficult. Now let's do it!

<h3>Step 1: Install Redis on Your Web Server</h3>

First, make sure you installed WordPress on your server and it runs on <strong>Ubuntu</strong> / <strong>CentOS VPS</strong>. Additionally, you have to activate <strong>repo EPEL</strong> (Extra Packages for Enterprise Linux) on your server.

After that, install Redis on your web server by running these following commands:

To install Redis on <strong>Ubuntu VPS</strong>, run this command:

<pre>apt-get install redis-server</pre>

To install Redis on <strong>CentOS VPS</strong>, run this command:

<pre>yum install redis</pre>

Next, start and activate Redis on system boot by the following command:

<pre>systemctl start redis.service 

systemctl enable redis.service</pre>

<h3>Step 2: Install Redis PHP Extension</h3>

Along with installing Redis, you need to install its <strong>PHP extension</strong>. This extension will allow WordPress to <strong>contact the Redis key-value store</strong>. To install this extension, run these following commands:

On Ubuntu VPS:

<pre>apt-get install php-redis</pre>

On CentOS VPS:

<pre>yum install php-pecl-redis</pre>

<h3>Step 3: Installing Redis Caching Plugin</h3>

Next, you have to install the Redis Caching plugin on your WordPress site. This plugin is available on <a href="https://wordpress.org/plugins/redis-cache/">wordpress.org</a>. So you just need to install and activate it as usual (To get more details, refer to <a href="https://gretathemes.com/install-wordpress-plugin/">this article</a>).

After that, go to <strong>Settings</strong> &gt; <strong>Redis</strong> &gt; <strong>Overview</strong>, set the default Redis listening address as <strong>127.0.0.1</strong> and the default listening port as<strong> 6379</strong>. Next, click <strong>Enable Object Cache</strong>.

<img class="aligncenter" src="https://i.imgur.com/lWOdNxK.png" alt="Installing Redis Caching Plugin to speed up your WordPress website" width="1349" height="624" />

Now you've finished installing Redis Caching plugin on your WordPress site!

<h3>Step 4: Verify Redis Caching on WordPress</h3>

To check if Redis cache works well on your website or not, connect to the web server via SSH and run this command:

<pre>redis-cli monitor</pre>

The above command will show all requests handled by Redis cache on your site. Therefore, you will know what is happening to your database. If requests appear as below, Redis cache is running on your site.

<pre># redis-cli monitor

OK

1510415208.863435 [0 127.0.0.1:50076] "PING"

1510415208.865491 [0 127.0.0.1:50076] "GET" "wp_:default:is_blog_installed"

1510415208.870259 [0 127.0.0.1:50076] "GET" "wp_:options:notoptions"

1510415208.870433 [0 127.0.0.1:50076] "GET" "wp_:options:alloptions"

1510415208.871197 [0 127.0.0.1:50076] "GET" "wp_:site-options:1:notoptions"

1510415208.875126 [0 127.0.0.1:50076] "GET" "wp_:options:uninstall_plugins"

1510415208.882241 [0 127.0.0.1:50076] "GET" "wp_:wordfence:alloptions"

1510415208.913368 [0 127.0.0.1:50076] "GET" "wp_:site-options:1:notoptions"

1510415208.913547 [0 127.0.0.1:50076] "GET" "wp_:site-options:1:notoptions"

1510415208.916283 [0 127.0.0.1:50076] "GET" "wp_:site-options:1:notoptions"

1510415208.916434 [0 127.0.0.1:50076] "GET" "wp_:site-options:1:notoptions"

1510415208.947299 [0 127.0.0.1:50076] "GET" "wp_:site-options:1:notoptions"

1510415208.947480 [0 127.0.0.1:50076] "GET" "wp_:options:can_compress_scripts"

1510415208.947637 [0 127.0.0.1:50076] "GET" "wp_:site-options:1:notoptions"

1510415208.954565 [0 127.0.0.1:50076] "GET" "wp_:posts:last_changed"</pre>

That's all, now your website is powered with Redis cache.

<h2>Last Words</h2>

From now on, whenever your web server receives database requests, it can send cached data from Redis cache to save time and effort. If the requested data isn't cached by Redis, the web server will retrieve and process data from the database as usual. Thus, Redis cache will reduce the server load and make your website run more smoothly.

If you have any questions about Redis cache, feel free to share it with us in the comment section!