If you’re looking to boost your WordPress site’s performance, using Redis as an object cache is an excellent option. Redis can help speed up the retrieval of data from your WordPress database, reduce server load, and improve the overall performance of your site. One of the easiest ways to integrate Redis into your WordPress site is by using the Redis Object Cache plugin.
In this article, we’ll walk you through the step-by-step process to set up Redis with WordPress using the Redis Object Cache plugin.
What is Redis?
Redis is an open-source, in-memory data structure store, commonly used as a cache or message broker. In the context of WordPress, Redis can be used to store database queries, object data, and other transient information that would otherwise be retrieved from the database every time a page is loaded.
By caching this data in memory (instead of fetching it from the database on every request), Redis can dramatically reduce the time it takes to load pages, improving your site’s performance.
Prerequisites
Before we start, make sure you have the following:
- Access to your server – You’ll need SSH or root access.
- Docker Installed on your server
Step 1: Install WordPress and Redis on your server
WordPress as well as redis needs to be installed on your server before you can start. Here’s how to install Redis on a Linux server using docker:
- Create docker-compose.yml:
---
services:
wp-app:
image: wordpress
restart: always
ports:
- 8085:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: db_user
WORDPRESS_DB_PASSWORD: ${database_password}
WORDPRESS_DB_NAME: wp_db
PUID: 1000
PGID: 1000
volumes:
- ${container_volumes_location}/wordpress/wp-app:/var/www/html
db:
image: mysql:5.7
restart: always
deploy:
labels:
- homepage.group=WP
- homepage.name=db
- homepage.icon=https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Database-mysql.svg/1448px-Database-mysql.svg.png
- homepage.description=MYSQL DB
environment:
MYSQL_DATABASE: wp_db
MYSQL_USER: db_user
MYSQL_PASSWORD: ${database_password}
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- ${container_volumes_location}/wordpress/mysql-db:/var/lib/mysql
redis-db:
image: 'redis:alpine'
ports:
- '6379:6379'
restart: always
expose:
- '6379'
- Deploy stack using docker-compose:
docker compose up -d
You should be able to reach your WordPress instance at <serverip>:8085
Step 2: Install the Redis Object Cache Plugin in WordPress
Now that Redis is running on your server, the next step is to install the Redis Object Cache plugin on your WordPress site.
- Log in to your WordPress admin dashboard.
- Go to Plugins → Add New.
- In the search bar, type Redis Object Cache.
- Install the plugin by clicking the Install Now button.
- Once the plugin is installed, click Activate.
Step 3: Enable Redis Object Cache in WordPress
Once the Redis Object Cache plugin is activated, you need to enable Redis caching for your WordPress site.
- Go to Settings → Redis in your WordPress admin dashboard.
- On the Redis settings page, click the Enable Object Cache button.
- If Redis is properly installed and running on your server, you should see a message that says “Redis Object Cache is working” along with the number of objects cached.
Step 4: Configure wp-config.php to Connect WordPress to Redis
To connect WordPress to Redis, you’ll need to edit your wp-config.php
file.
- Access your site’s root directory via FTP or SSH.
- Open the
wp-config.php
file in a text editor. - Add the following lines of code before the line that says
/* That's all, stop editing! Happy publishing. */
:
/** Enable Redis object caching */
define('FS_METHOD', 'direct');
define('WP_REDIS_HOST', 'redis-db');
define('WP_REDIS_PORT', 6379);
define( 'WP_REDIS_PREFIX', '<your-site-name-here>' );
Step 5: Verify Redis is Working
Once you’ve configured wp-config.php
, go back to your WordPress dashboard:
- Visit Settings → Redis again.
- You should see a message indicating that Redis is enabled and that it’s successfully caching your WordPress objects.
To test that Redis is working properly, try clearing the cache and visiting your site. The plugin will automatically store objects in Redis, speeding up subsequent page loads.
Step 6: Monitor Redis Performance (Optional)
Redis Object Cache provides a few tools to help monitor its performance. The Redis Dashboard plugin is a popular tool that lets you check Redis’ memory usage, hit/miss ratio, and other stats. You can also monitor your server’s resources using tools like htop or Redis’ built-in commands.
For example, to check the cache hit/miss ratio, run the following command in your Redis CLI:
redis-cli info stats
You’ll see stats such as:
keyspace_hits:12345
keyspace_misses:678
A high ratio of hits means that Redis is effectively caching your objects.
Step 7: Advanced Configuration (Optional)
- Persistent Object Caching: By default, Redis Object Cache works as a transient cache, meaning that cached objects may be cleared periodically. If you want to make your cache more persistent, you can configure Redis for persistent caching.
- Configure Cache Expiration: You can adjust the cache expiration settings depending on the needs of your website. Caching too aggressively can cause stale content, so it’s important to find a balance.
- Caching Custom Objects: If you have custom objects or data you want to cache, you can extend the Redis Object Cache to handle these cases using WordPress hooks.
Conclusion
Integrating Redis with WordPress through the Redis Object Cache plugin is an excellent way to improve your site’s performance, reduce database load, and speed up page loading times. With just a few simple steps—installing Redis, configuring your server, and activating the plugin—you’ll be able to take advantage of in-memory caching and optimize your WordPress site.
If you’re using a managed hosting provider, Redis may already be enabled, so check with your host before going through these steps. Otherwise, follow this guide to set up Redis manually and see significant performance improvements.
Leave a Reply