Introduction
Redis is an open-source key value store that can operate as both an in-memory store and as cache. Redis is a data structure server that can be used as a database server on its own, or paired with a relational database like MySQL to speed things up, as we’re doing in this tutorial.
For this tutorial, Redis will be configured as a cache for WordPress to alleviate the redundant and time-consuming database queries used to render a WordPress page. The result is a WordPress site which is much faster, uses less database resources, and provides a tunable persistent cache. This guide applies to Ubuntu 14.04.
While every site is different, below is an example benchmark of a default WordPress installation home page with and without Redis, as configured from this guide. Chrome developer tools were used to test with browser caching disabled.
Default WordPress home page without Redis:
804ms page load time
Default WordPress home page with Redis:
449ms page load time
Note: This implementation of Redis caching for WordPress relies on a well-commented but third-party script. The script is hosted on DigitalOcean’s asset server, but was developed externally. If you would like to make your own implementation of Redis caching for WordPress, you will need to do some more work based on the concepts presented here.
Redis vs. Memcached
Memcached is also a popular cache choice. However, at this point, Redis does everything Memcached can do, with a much larger feature set. This Stack Overflow page has some general information as an overview or introduction to persons new to Redis.
How does the caching work?
The first time a WordPress page is loaded, a database query is performed on the server. Redis remembers, or caches, this query. So, when another user loads the WordPress page, the results are provided from Redis and from memory without needing to query the database.
The Redis implementation used in this guide works as a persistent object cache for WordPress (no expiration). An object cache works by caching the SQL queries in memory which are needed to load a WordPress page.
When a page loads, the resulting SQL query results are provided from memory by Redis, so the query does not have to hit the database. The result is much faster page load times, and less server impact on database resources. If a query is not available in Redis, the database provides the result and Redis adds the result to its cache.
If a value is updated in the database (for example, a new post or page is created in WordPress) the Redis value for that query is invalidated to prevent bad cached data from being presented.
If you run into problems with caching, the Redis cache can be purged by using the flushall
command from the Redis command line:
redis-cli
Once you see the prompt, type:
flushall
Additional Reference: WordPress Object Cache Documentation
Prerequisites
Before starting this guide, you’ll need to set up a sudo user and install WordPress.
- Ubuntu 14.04 Droplet (1 GB or higher recommended)
- Add a sudo user
- Install WordPress. This guide has been tested with these instructions, although there are many ways to install WordPress
Lire la suite…