Speed up your WordPress Blog with Redis

Redis Object Cache for WordPress

If you’re running a WordPress blog and you’d like to speed it up, you have no better chance of doing that than caching your content in Redis Server to alleviate the MySQL Server (or MariaDB) database backing the blog. WordPress supports caching with Redis via a plugin extension. You will find many options if you search the plugins by “Redis”. I have tested the features-rich Redis Object Cache and I am satisfied with it. To install it, search for it in the directory and install it as you would typically do with plugins.

Assuming that you are capable to execute Redis on the web server host, or another host (or your host supports Redis cache as a service out-of-the-box), this plugin supports several configurations, the most important:

  • WP_REDIS_HOST, the hostname or IP address to connect to your Redis Server
  • WP_REDIS_PORT, the port to connect to your Redis Server
  • WP_REDIS_PASSWORD, the password to authenticate to Redis Server
  • WP_REDIS_DATABASE, the database, if you would like to use Redis virtual databases
  • WP_REDIS_PREFIX, a prefix to all the keys in the cache, used to avoid conflicts

Configuring Redis Object Cache

If Redis is not running locally but is exposed as a service, you may want to configure at least the WP_REDIS_HOST. In my case, I will edit the desired configuration by accessing my WordPress blog file system and editing the file wp-config.php. Note where you will place your configuration:

if ( !defined('ABSPATH') )
	define('ABSPATH', dirname(__FILE__) . '/');

// Add your configuration here

/** Imposta le variabili di WordPress ed include i file. */
require_once(ABSPATH . 'wp-settings.php');

And this is how the final configuration file looks like after the edition.

if ( !defined('ABSPATH') )
	define('ABSPATH', dirname(__FILE__) . '/');

define( 'WP_REDIS_HOST', 'your-redis-hostname' );
define( 'WP_REDIS_PORT', 6379 );

// change the prefix and database for each site to avoid cache data collisions
define( 'WP_REDIS_PREFIX', 'mortensi:' );
define( 'WP_REDIS_DATABASE', 0 ); // 0-15

// reasonable connection and read+write timeouts
define( 'WP_REDIS_TIMEOUT', 1 );
define( 'WP_REDIS_READ_TIMEOUT', 1 );

/** Imposta le variabili di WordPress ed include i file. */
require_once(ABSPATH . 'wp-settings.php');

When done, you can browse to your WordPress dashboard and verify that WordPress can connect to Redis.

Now, if you have access to the Redis Server using the command line client redis-cli, you can peek into what’s cached:

> SCAN 0 MATCH 'mortensi:*' 
1) "352"
2) 1) "mortensi:wp:post_meta:1105"
   2) "mortensi:wp:post_meta:294"
   3) "mortensi:wp:term_meta:8"
   4) "mortensi:wp:terms:get_terms-df0bccef865047dabca2174b2a6933bf-0.10691100 1688073222"
   5) "mortensi:wp:yarpp:title_index"
   6) "mortensi:wp:post_format_relationships:1265"

Performance boost using Redis

So, now that my cache is up and running, let’s compare some numbers. The download of a webpage took the eternity of 7 seconds (I presume I was hitting MySQL congestion, as my blog is running on a shared host and uses a shared MySQL database)

Mirkos-MacBook-Pro:tmp mortensi$ wget https://www.mortensi.com/2023/06/diagnose-mysql-performance-issues/
--2023-06-28 16:04:44--  https://www.mortensi.com/2023/06/diagnose-mysql-performance-issues/
Resolving www.mortensi.com (www.mortensi.com)... 2001:4b78:1001::1401, 217.64.195.242
Connecting to www.mortensi.com (www.mortensi.com)|2001:4b78:1001::1401|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html.1’

index.html.1                           [ <=>                                                            ]  95,72K  --.-KB/s    in 0,09s   

2023-06-28 16:04:51 (1,07 MB/s) - ‘index.html.1’ saved [98017]

Using Redis, the download of a page collapses to less than a second, which is quite an improvement!

wget https://www.mortensi.com/2023/06/diagnose-mysql-performance-issues/
--2023-06-29 23:29:15--  https://www.mortensi.com/2023/06/diagnose-mysql-performance-issues/
Resolving www.mortensi.com (www.mortensi.com)... 2001:4b78:1001::1:1101, 217.64.195.24
Connecting to www.mortensi.com (www.mortensi.com)|2001:4b78:1001::1:1101|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html.10’

index.html.10                          [ <=>                                                            ] 101,18K   633KB/s    in 0,2s    

2023-06-29 23:29:15 (633 KB/s) - ‘index.html.10’ saved [103605]

Advanced usage of Redis Object Cache

A single Redis Server should be more than enough to cache an average amatorial blog, but if you need something professional and future-proof, you can scale Redis Server using the Redis Cluster configuration, or set up a Sentinel deployment if scalability is not a requirement but high availability is a must-have. Redis Object Cache comes with a series of professional plans, too.

If you are looking for a managed and highly available Redis you can try Redis Cloud with the cheap fixed plan or the flexible plan, if looking for a fully configurable, scalable, highly available, and multi-tenancy data platform. You can try it for free too, make sure to create the Redis database as close to the blog as possible (Redis Cloud DBaaS is available in AWS, GCP, and Azure, so choose the region wisely).

Leave A Comment