This site is now faster thanks to Redis!

Patrick Garman

About a year ago I was searching around for how to best implement Redis into WordPress and did not come up with much. There were a few blog posts around the internet from a few developers who got it working but I never did replicate their success. After watching Mark Jaquith’s WordCamp Europe talk about scaling WordPress I decided it was time to take another crack at it. I was amazed at what I saw on the first try.

If you have been involved in any website that has needed any sort of scaling or major caching, you have probably heard of Memcache. There are a lot of people who will swear by Memcache, and Memcache was at one time probably really really great. However just like the web has shifted radically in the past few years from the Apache being the standard web server, I believe Redis is going to become the standard for caching. Redis is as lightning fast key/value data store, and when I say lightning fast I mean insanely fast and super easy to setup.

I don’t mean to sound fanboy-ish, but when I find something that works and works WELL I am happy. The first time I setup Memcache it actually slowed down the site. It was more overhead than it was worth and was a real pain in the ass to setup (note: this was on 2x 1GB Linodes at the time, Nginx/PHP on one and MySQL/Memcache on the other). I came to the conclusion, though I may be wrong on this, that Memcache was going to be great… for a site that had enough traffic to make it worth while. Redis on the other hand takes me about 5 minutes to configure on my websites and even speeds up low traffic sites. I’ve now setup Redis on about 5 production websites, and a dozen or so development sites. Each time it is extremely simple and load times always drop.

So how do I setup WordPress with Redis

All of my servers run Ubuntu 14.04, with Nginx & PHP5-FPM. On Ubuntu Redis is extremely easy to install, it is just one apt-get command away.

apt-get install redis-server

This will install and setup Redis locally without much work on your end. To get a slight edge out of Redis, and because I have no need for persistent data, I have been disabling saving the key/value data in Redis to disk. To do this open the /etc/redis/redis.conf file and about 3/4 the way down comment out the “save” lines. Redis will save the data to disk every X seconds at the intervals in the config. Since we are disabling saving to the disk this is slightly less work the server is doing and in theory gives you that much of an edge.

Next install the object-cache.php file into WordPress. There are a few version of a Redis object-cache.php file floating around, the version I use is the one provided by Pantheon on WordPress.org. Simply download this plugin, and upload the object-cache.php file into the root of your wp-content folder. This is not your normal plugin, and should not be installed like a normal plugin – it will not work. This is what WordPress calls a “drop-in” – meaning, WordPress will look to see if this file exists in a very specific location and if it does override some of the core functionality of WordPress with it. In this case we are replacing the core WordPress object cache with Redis. The core WordPress object cache will only persist through a single page load and then disappear. This doesn’t help much when you have multiple users all hitting the same page if that cache doesn’t persist to the next page load. This is why we want to save that cached data into Redis and not the servers memory. This way on the next page reload the queries (assuming they are unchanged) will be able to just pull data from Redis instead of waiting for MySQL to go perform a query and return data.

Why should I care about caching? I am only saving milliseconds.

Your results may vary, some sites I saw savings in multiple full seconds, others I only saved 50ms or so. This may seem negligible, but I believe optimizations come in baby steps. There are very few changes you can make on a properly setup server with a properly coded website that will give you amazing speed boosts. Your optimizations may only save you 20-200ms at times, BUT if you keep chipping away at the load time 10x 20-200ms will soon be 200-2000ms. Give it time, and if you did it right, speed will come.