How to Configure Redis Object Caching for WordPress

A technical guide to eliminating database bottlenecks and drastically reducing TTFB by implementing Redis Object Caching on your WordPress VPS.

How to Configure Redis Object Caching for WordPress

When a visitor lands on your WordPress site, PHP executes and fires dozens (sometimes hundreds) of queries to your MySQL database to assemble the page. For a high-traffic WooCommerce store or a dynamic membership site where page caching (like Varnish or WP Rocket) is bypassed, this constant database querying will quickly overload your server CPU.

The solution is Object Caching. Unlike page caching (which saves the final HTML output), object caching saves the results of database queries in RAM. The next time WordPress needs that specific data, it retrieves it instantly from memory instead of hitting the database.

While Memcached is popular, Redis is the modern industry standard due to its persistence and support for complex data types. Here is how to configure Redis on a Dedicated Cloud VPS.

1. Install Redis on Your Linux Server

First, you must install the Redis server daemon on your VPS.

For Ubuntu/Debian:

sudo apt update
sudo apt install redis-server php-redis

For CentOS/AlmaLinux:

sudo dnf install epel-release
sudo dnf install redis php-pecl-redis

Once installed, start the service and enable it on boot:

sudo systemctl start redis
sudo systemctl enable redis

You can verify Redis is running by typing redis-cli ping. The server should respond with PONG.

2. Configure Redis for Optimal Performance

By default, Redis acts as a permanent datastore. For object caching, we want it to act as an LRU (Least Recently Used) cache, meaning when the RAM limit is reached, it will automatically delete the oldest cached objects to make room for new ones.

Edit the Redis configuration file: sudo nano /etc/redis/redis.conf

Find and update the following directives:

# Set a memory limit (e.g., 256MB for a small site, 1GB for a large WooCommerce store)
maxmemory 256mb

# Tell Redis how to evict old data
maxmemory-policy allkeys-lru

Save the file and restart Redis: sudo systemctl restart redis.

(Note: If you run out of physical RAM and your server crashes, ensure you have sufficient swap space configured. See our Linux Error 28 (Disk Space) guide for related resource management concepts).

3. Connect WordPress to Redis

Now that the server is ready, you must instruct WordPress to use it.

  1. Install and activate the Redis Object Cache plugin by Till Krüss from the WordPress repository.
  2. If your Redis server is running on the same VPS as your website (localhost), simply go to Settings > Redis and click “Enable Object Cache”.
  3. If you have customized the Redis port or added a password in redis.conf, you will need to add the following constants to your wp-config.php file before enabling the cache:
define( 'WP_REDIS_HOST', '127.0.0.1' );
define( 'WP_REDIS_PORT', 6379 );
// define( 'WP_REDIS_PASSWORD', 'your_secure_password' );

Measuring the Impact

Once enabled, monitor your site’s Time to First Byte (TTFB). You should see a dramatic reduction in server response times, especially for logged-in users and dynamic cart pages. If you still encounter slow backend performance, you may need to audit your database indices or review our guide on Fixing MySQL ‘Too many connections’.

Conclusion

Implementing Redis Object Caching is the most effective way to scale a dynamic WordPress application. By serving database queries directly from RAM, you can handle significantly higher concurrent traffic without upgrading your VPS hardware.

To accelerate database object caching and cut server load, migrate to LiteSpeed cPanel hosting.