Redis is a popular key-value type, in-memory data storage. Redis is very fast because it saves the data into the memory. But where does it actually stores the data in the file system? In this blog, we will learn all about redis persistence, so let’s get started.
What is redis persistence?
Redis persistence basically means writing the data to durable storage like SSD or hard disk. Persistence is very important in redis as it makes sure that the data is available for use if the server reboots.
By default, there are many persistence options available in redis. It is very important to understand each of them before setting up any type of persistence in redis.
Redis persistence options
Redis provides 4 types of persistence options to users. Users can choose any of the persistence based on their requirements or use case. The 4 types of persistence options in redis are:-
- Redis persistence default(RDB)
- Append only file(AOF)
- No persistence
- RDB + AOF
RDB persistence in redis
The RDB persistence takes point-in-time snapshots of the redis dataset at specified intervals.RDB generates a single file which is the point-in-time representation of data.
RDB files are very good for backup.RDB persistence is very fast as they do not need to perform heavy disk I/O.
Redis persistence default
The RDB persistence is the default persistence in redis.
AOF persistence in redis
The AOF persistence logs every write operation received by the server, and that will be played again at server startup, to reconstruct the original dataset.
No persistence in redis
Redis also provides the facility to disable persistence completely. In this case, the data will be available as long as the server is running.
RDB + AOF persistence in redis
Redis also provides the facility to enable both RDB and AOF persistence. In case when Redis restarts the AOF persistence file will be used to rebuild the actual dataset since it guarantees the most complete data.
Redis persistence configuration
In this session, we will understand where to set up persistence in redis.In redis, all the configuration is set via /etc/redis.conf file.
Redis setup RDB persistence
Go to the /etc/redis.conf file and look for the below properties in SNAPSHOTTING portion.
# save 60 1000
To enable RDB persistence, uncomment the above directive.
This configuration means, performing an RDB backup for 1000 keys which are changed in 60 seconds. Also, make sure the below properties are also enabled for RDB backup
stop-writes-on-bgsave-error no
rdbcompression yes
dbfilename dump.rdb
Redis setup AOF persistence
To enable AOF, Go to the /etc/redis.conf file and look for the below properties in the APPEND ONLY MODE portion.
appendonly no
change the above entry from no to yes to enable AOF. Also, make sure the below property in enabled
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
Restart the redis service and AOF persistence will be enabled.
Disable redis persistence
In this session, we will understand how to disable the persistence is redis. To disable the persistence user needs to perform the changes in /etc/redis.conf file.
open the redis.conf file and perform the below action based on the persistence type
Disable RDB persistence in redis
To disable the RDB persistence, comment the below line of code
# save 60 1000
Restart the redis service and the RDB persistence will be disabled.
Disable AOF persistence in redis
To disable the AOF persistence, change the appendonly mode to no
appendonly no
Restart the redis service and AOF persistence will be disabled.
Redis persist command
In Redis, PERSIST command removes the expiration from the key. The syntax for the persistence command is:-
PERSIST key_name
The persistence command will return either 0 or 1.
1, if timeout is removed from the key.
0, if the key does not have an associated timeout.
Redis persist command example
Let’s insert data into the redis
127.0.0.1:6379> SET key1 value1
Now set the expiry key and TTL
127.0.0.1:6379> EXPIRE key1 120
(integer) 1
127.0.0.1:6379> TTL key1
(integer) 112
Now persist the data and check TTL again
127.0.0.1:6379> PERSIST key1
(integer) 1
Persistence 1 means timeout is removed from the key.
127.0.0.1:6379> TTL key1 (integer) -1
Conclusion
I hope you have liked this tutorial on redis persistence. Please do let me know if you are facing any issues while following along.