How to add redis cache to your wordpress site
Have you noticed any performance issues with your wordpress site (or sites)? Is the query to database becomes a bottleneck to your site performance? Caching might be the answer for you.
Therefore many caching solutions out there, the easiest, and well-known option is Redis cache. Obviously there are other options too if you want to explore.
WordPress has an easy to use plugin to allow wordpress to make use of redis as its object cache. Having an object cache in wordpress reduces the number of expensive calls to recreate same page content or expensive queries to pull data from your database.
What do you need to setup an object cache for your wordpress site?
Setup Redis server
On your linux (or windows, mac, whatever you use), install and setup your redis server. Instruction here is based on Ubuntu linux.
- Update your linux OS
sudo apt update && sudo apt -y upgrade
Obviously, if you like, you can run the two commands separately, and not auto accept the upgrade, which allows you to review what will be upgraded. - Install redis server
sudo apt -y install redis-server
- Update redis server configuration in /etc/redis/redis.conf, consider the following fields
maxmemory <bytes>
, e.g.maxmemory 512mb
. I’m using 512mb to begin with, and will monitor how it goes. If you have less memory available, you can reduce it to e.g. 128mb or even less- if you do not want to use disk for cache because it is slower, then consider disabling the following settings by prepending a # in front of each line
# save 900 1
# save 300 10
# save 60 10000
- Restart redis server
sudo systemctl restart redis-server.service
- Test your redis server is working, e.g.
redis-cli ping
redis-cli info
redis-cli info Stats
redis-cli monitor
Setup PHP for Redis
- Setup php to with redis
sudo apt install php-redis
or, if you have version specific installed, e.g. v8.1 of phpsudo apt install php8.1-redis
- Restart your php service (I’m using php-fpm)
sudo systemctl restart php-fpm.service
or, if you have version specific installation, e.g. v8.1 of phpsudo systemctl restart php8.1-fpm.service
Setup WordPress To Use Redis
- Setup wordpress to utilise redis
- Go to your wordpress admin area
https://yourwebhost.com/wp-admin
and login - Go to (left panel), plugins->Add New and search for Redis Object Cache and click Install
- Activate plugin by clicking the Activate button
- Enable Redis Object Cache
- Once done, you will see a screen similar to this
- Go to your wordpress admin area
- Test out wordpress with redis object cache and monitor
- Go to your redis server and issue the following command
redis-cli monitor
- Go to your website and try to access your pages and then monitor what has been cached from the previous command
- Go to your redis server and issue the following command