Both Redis and Memcached are in-memory data storage systems. Redis is an open-source key-value store, and Memcached is a high-performance distributed cache service. They are in charge of making in-memory copies of accessed data in a certain period.
In our case, we configure WordPress to cache pages, database queries, objects, and to minify and cache some assets such as JS and CSS files, and, of course, the HTML of the response itself.
Let’s start with Redis. According to their site:
Redis is an open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker.
Supported data structures
- sorted sets with range queries,
- hyper logs,
- geospatial indexes with radius queries and streams.
Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different on-disk persistence levels. It provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
Such an amount of available data structures allow us to cache a lot of different types of data. However, with great power comes great responsibility for some performance issues… (you were almost thinking of uncle Ben, didn’t you?).
What does Redis use?
Redis uses single-cores, so it excels in performance in low-sized data, and if you want to increase the Redis service performance, you should increase the number of server cores.
Redis also supports disk-cache for caching, not so frequently queried data when your memory limit reaches its max capacity. You can cache, for example, assets that are only shown in one lose page in your site or some pages that are not so relevant according to your analytics.
It is also useful to store the cached data while restarting the service or doing some maintenance tasks that require stopping the service like updating the daemon. You just want to have a snapshot of your cache for further usage or analysis.
A free & open-source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
What is it?
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. Simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.
Following the same structure as the previous analysis, Memcached uses multiple cores to work with your data, so it outperforms Redis with extensive data (above 100k). On the other hand, it has higher memory usage than Redis. In general, you can use it to store large amounts of data, such as database queries or assets.
Finally, in terms of data persistence, Memcached does not support it because it does everything in memory, its essence is a high performance, and disk persistence does not excel.
Before enabling anything, we should do some performance tests to have data to compare to. We use BlazeMeter to generate concurrent traffic and give us an idea about how the site is performing.
In BlazeMeter, create a Performance test pointing to your site and let it run for a few minutes.
Once you have data to compare performance, continue by installing W3 Total Cache Plugin, activate it, and it will ask you to add a configuration line in your wp-config.php file, add it to the end of it.
<?php ... /** Enable W3 Total Cache */ define('WP_CACHE', true); // Added by W3 Total Cache
Then, to enable Redis support, add these lines next to the previous ones.
//Redis define('WP_REDIS_MAXTTL', '900'); define('WP_REDIS_SELECTIVE_FLUSH', true); define('WP_CACHE_KEY_SALT', 'thebestday.ever' ); define('WP_REDIS_HOST', 'redis.xxxxxx.ng.0001.eun1.cache.amazonaws.com');
Then you should go to the configuration of the plugin itself. This is done by going to the menu item called Performance and then General Settings.
Enable Page Cache, Minify, Database Cache, and Object cache.
Next, go to each item configuration and activate the caching method that best suits you; we use these:
Page Cache: Memcached
Database Cache: Redis
Object Cache: Redis
Example of page config:
Then, go to each of these options and configure them to reach the correct server instances and check their connection with the test button.
After enabling caching, rerun the same BlazeMeter tests and compare the results, you’ll see an improvement in the response times when the number of users goes up.
To really make an impact on your workflow, you should analyze how your data is structured and how it is accessed from your users so you can decide which method will perform better in each case.
There are some scenarios where you can go for Redis or Memcached immediately, but it is always better to back up this with your own analysis. Also, don’t forget to inform your clients which services will be needed to improve their sites or web apps’ performance.