Redis: Persistence
Let’s discuss how Redis stores data on hard disk.
Redis is an in-memory database, which means the data is stored in memory(RAM). In the case of a server crash, all the data stored in the memory is lost. To prevent this, Redis has some mechanism in place to back up the data on the disk. In case of a failure, the data can be loaded from the disk to the memory when the server reboots.
Redis provides two persistence options, which we will discuss here:
RDB persistence(snapshotting)
In RDB(Redis Database) persistence, snapshots of the data are stored in a disk in a dump.rdb
file. The interval for snapshotting is defined in the configuration file. The default configuration present in the redis.conf
file is:
save 900 1
save 300 10
save 60 10000
In the example above, the behavior will be to save data as follows:
- After 900 sec (15 min), if at least 1 key changed
- After 300 sec (5 min), if at least 10 keys changed
- After 60 sec, if at least 10000 keys changed
If the load is too high and a lot of data is being stored, Redis will take a backup every minute. If the load is medium, the backup will be taken after every five minutes and so on.
The durability of data depends upon how frequently the data is being dumped to the disk. If data is dumped after every five minutes, and the server crashes after minutes since the last dump then all the data stored within the last four minutes are lost. So, the duration of backup should be decided carefully.
A user can also save ...