Redis provides a variety of different levels of persistence methods:
- RDB persistence can generate a point in time snapshot of a dataset within a specified time interval.
- Aof (append only file) persistently records all write commands executed by the server and restores the dataset by re executing these commands when the server starts. All commands in the AOF file are saved in the format of redis protocol, and new commands will be appended to the end of the file. Redis can also rewrite the AOF file in the background, so that the volume of the AOF file will not exceed the actual size required to save the state of the dataset.
- Redis can also use AOF persistence and RDB persistence at the same time. In this case, when redis restarts, it takes precedence over the AOF file to restore the dataset, because the dataset saved by the AOF file is usually more complete than that saved by the RDB file.
- You can even turn off persistence so that data only exists when the server is running.
1、 Persistence of RDB
Working principle: take a snapshot of the memory every certain time, and write the data in memory to the file (RDB file). This is the default persistence method of redis. When redis is generated dump.rdb The working process is as follows:
- When the RDB generation condition is reached, the redis main process forks a sub process
- Fork out of the sub process will dump the memory data set to the temporary RDB
- When the child process has finished writing the temporary RDB file, redis replaces the old RDB file with the new one
The configuration parameters are as follows:
RDB example test: you can use redis benchmark to conduct stress test and observe the change of RDB file size.
Bin / redis benchmark - N 100000 means 100000 operations are performed
2、 Disadvantages of RDB:
Between snapshots, if a power failure occurs, data is lost. Example: insert a new value after the RDB is generated. Sudden power failure may result in data loss.
3、 Monitoring RDB:
The most direct way to monitor redis is to use the info command provided by the system. You only need to execute the following command to get the status report of redis system.
bin/redis-cli info | grep rdb_
- rdb_ changes_ since_ last_ Save indicates the number of key changes since the last RDB save
- rdb_ bgsave_ In_ Progress indicates whether the bgsave operation is in progress, 1 indicates that it is in progress, and 0 indicates that it is not
- rdb_ last_ save_ Time stamp of the last RDB file saved
- rdb_ last_ bgsave_ time_ SEC last save time
- rdb_ last_ bgsave_ Status last saved state
- rdb_ current_ bgsave_ time_ The amount of time that SEC has spent saving RDB files