Redis is in-memory data structure store, it can be used as a database, cache, and message broker. Although redis was introduced with in-memory data storage, it has options for persistence storage. The options are:
- RDB (Redis Database Backup)
- AOF (Append Only File)
- Hybrid (RDB + AOF)
Using the persistence option will reduce the redis performance, that is why in this tutorial we will compare the performance of all the persistence mode option.
Redis is already bundled with a load testing tools that is
redis-benchmark. It can test the performance of our redis in specified host. In this tutorial, we will test the redis installed on my laptop. This is my laptop spec:
- CPU: Intel Core i3-6006U, 2 cores, 4 threads.
- RAM: 16 GB
- OS: Ubuntu 18.04 WSL
- Redis Version: 6.2.6
We will test the standard and most common
GET, operation 10 times and get the average results for each of the persistence option. This the command we used to test the redis:
redis-benchmark -t set,get | egrep "======|throughput summary:"
RDB persisten performs point-in-time snapshots of your data at specified intervals. It is not always persist the data to disk on every write operation. Because of that, RDB should be faster than other persistence storage option. But RDB has a weakness, it has higher chance of data loss especially if the redis stop working between the specified intervals snapshots.
Redis by default will use the RDB persistence with this rule:
- After 3600 seconds (an hour) if at least 1 key changed
- After 300 seconds (5 minutes) if at least 100 keys changed
- After 60 seconds if at least 10000 keys changed
In this test, we will use the default Redis RDB configuration. This is the average test results for RDB Persistence after tested 10 times.
AOF persistence logs every write operation performed on the redis, it also will be used at redis startup to reconstructing the original dataset. AOF is more durable than RDB, but you can have different fsync policies to determine when to log the write operation to find the balance between performance and durability.
By default, Redis is not using AOF persistence, so you must configure redis to use AOF. The default fsync policies when the AOF enabled is every second. Redis team said that using this policies, the performance wlll still be great.
In this test, we will use the default Redis AOF configuration. This is the average test results for AOF Persistence after tested 10 times.
RDB + AOF
It is possible to combine these two persistent option. The redis will logs every write operation, and then on a specified intervals it will perform a snapshot. When the redis booting, it will use AOF file instead of RDB to reconstructing the data because it is guaranteed to be the most complete.
In this test, we will use the default Redis AOF and RDB configuration. This is the average test results after tested 10 times.
If you are not interested with persistency, you can choose to run fully on memory. It will gives you the best performance, but when the redis rebooting, your data will be lost.
This is the average test results after tested 10 times.
There are some key point when I was doing this experiment:
- The Persisten option will only affect the performance of write operation. As tested, all of them have relatively the same read speed that is around
47k req/s. This is make sense, because read operation does not change the redis data.
- The write performance on RDB is the same with no persistence at all. This is actually because the snapshot process is never happened in this test. Each test is done in less than 3 second. The default interval for RDB is 60 second. So, if we configure Redis using RDB persistence, it will have maximum performance until the snapshot process happend.
- The write performance on AOF is reduced around 9% from
42k req/s. The AOF get performance reduce because it will log the write operation to disk every second (by default). You will get better performance by extending the fsync policy in Redis configuration.
- The hybrid (AOF + RBD) approach by default will have the same performance as AOF, but the performance maybe will be reduced further when the snapshot process happend.
Thats it, hope you benefit from this experiment. From this test, we know that we are not sacrificing a lot of performance when we want to persist our data in redis. But, if a little performance change matter to you, then choose the persistence mode wisely according to your need.