What is the WordPress Object Cache? As explained here, Object caching involves storing database query results so that the next time a result is needed, it can be served from the cache without having to repeatedly query the database.
There are two pre-made solutions to improve the object cache performance by storing it into memory instead of using the standard WordPress database. One is called wp-memcached for Memcached, and the other is Redis Cache for Redis.
apt install memcachedapt install php8.0-memcache
After that copy the object-cache.php to your wp-content folder. Done.
Although WordPress doesn’t use native PHP session functionality it still makes sense to set your php.ini session.storage and session.save_path so that other non-Wordpress code on your server can benefit, too.
session.save_handler = memcached session.save_path = "localhost:11211?persistent=1";session.save_path="/tmp/memcached.sock" # if you install memcached on the same server as wordpress, you can and should use the unix socket instead of a tcp connection
While Memcached is stable and has been around for a very long time, I prefer Redis. Redis itself is more feature-rich and the WordPress Plugin that integrates it has more features as well.
apt install redis-server redis-toolsapt install php8.0-redis
Install the plugin with
composer require wpackagist-plugin/redis-cache, deploy your code, enable it. Other than the drop-in plugin for Memcached, the Redis plugin shows you a couple of stats, let’s you flush the entire cache easily and comes with a performance graph on your WordPress dashboard.
Then, set your php.ini session.storage and session.save_path:
session.save_handler = redissession.save_path = "tcp://127.0.0.1:6379?persistent=1"; session.save_path = "unix:///var/run/redis.sock" # use the unix socket where possible
Note that you can’t use the socket, if you choose to install Redis on a dedicated server. That, on the other hand, has the advantage that it can be used by multiple WordPress servers, when you have some sort of cluster setup behind a load balancer.
Something to keep in mind is, that you should not forget to protect your Memcached or Redis instances from unwanted access. Ideally you combine all of these measures:
- Do not use the standard ports. These can be modified in the corresponding configuration files.
- Do not run your instances (of Redis or Memcached) on a public IP address, instead use a private IP address (that is only accessible from within the same datacenter). Have Redis/Memcache listen on these IPs. That reduces risk, it does not eliminate it.
- Make sure to configure a firewall (for example ufw) to limit access to your Memcached/Redis to the private IP of the server(s) that run WordPress.
- Use the optional
authparameter of Redis, and use a strong, sufficiently long password or passphrase.
There are extensive articles and tutorials covering how to setup Simple Authentication and Security Layer (SASL) for Memcached at Digital Ocean and at CouchBase. Unfortunately, the sheer length of these tutorials, hint towards this being anything but simple – which is another reason I chose Redis over Memcached.
Continue to part 26 of this tutorial series: Pre-compress static assets with Brotli or Gzip