How to load WordPress in a few milliseconds using Redis

Redis is an advanced key-value store. Like memcached on steroids. Everything is in the RAM and you can theoretically reach 100 000 GET per second with Redis.

My solution below will cache all the HTML output in Redis and display it without the need to load WordPress.

A big thanks to Jeedo Aquino who have rewritten and improved my original solution. Details here. This article updated on the October 25, 2012 with the new code.

If you don't already have your own VPS or server - do these steps first: Guide: WordPress with Maximum Performance and Speed.

Installing Redis

If you are using Debian then just:

apt-get install redis-server

Or else, just see the installation instructions.

Using Predis as a PHP client for Redis

You need a client in order to use PHP to communicate with Redis.

I recommend Predis. Upload predis.php to the WordPress root directory. See more about Predis on Github (if you use a newer version of Predis you need also to modify the script below).

Installing the PHP script for the Frontend Cache

Step 1:

Add the code below to a new file called index-with-redis.php and place it in the WordPress root directory.

Step 2:

Optional: modify the 3 variables at the top of the file.

Step 3:

If you are using Apache change all occurances of index.php to index-with-redis.php in the .htaccess.

If you are using the more preferred nginx then you need to rename index-with-redis.php to index.php . Unfortunately this also means that you have to replace this file each time you upgrade WordPress. You can also change nginx.conf to access index-with-redis.php instead of index.php but it creates some bugs and I would avoid that.

Step 4:

Deactivate all other caching plugins.

Instructions and info:

Cached pages do not expire not unless explicitly deleted or reset. My guess is that Redis probably will use around half of your database size in the RAM.

Appending a ?c=y to a url deletes the entire cache of the domain, only works when you are logged in

Appending a ?r=y to a url deletes the cache of that url

Submitting a comment deletes the cache of that page

Refreshing (f5) a page deletes the cache of that page

Includes a debug mode, stats are displayed at the bottom most part after </html>

Optionally display a powered by Redis badge with page generation time.

Benchmark for this blog:

Average execution without redis: 1.614 seconds for the home page and 0.174 seconds for blog posts (without using any cache plugin).

Average execution time with redis: 0.00256 seconds (regardless of type of page)

I have been using this setup for this blog for quite some time now and all has been working well.

Some other advice

I am using the following setup for this blog: Nginx + PHP-FPM + APC + Cloudflare + Redis. Hosted on a nano VPS with 6sync for $15 per month. No caching plugins. It is maybe the fastest way to serve WordPress today. See here all the steps I did to setup this.

Make sure you also send HTML as gzip to the browser. You can do that in the .htaccess (Apache), nginx.conf (Nginx) or via CloudFlare.

See also my article: Why I recommend CloudFlare and why you should try it too

Please comment

I would love to hear from you!

25 Oct 2012

Other posts