Persistence of redis

Principles of Redis Persistence of RDB and AOF
Preface:
There are two types of redis persistence modes: RDB snapshot and AOF mode (the default is RDB mode). When the Redis server restarts, the data will be automatically restored, and the data will be restored first from AOF, and then restore from RDB

1. Principle of RDB snapshot mode
RDB mode: When redis needs to do persistence (execute SAVA or BGSAVA command, or execute when configuration conditions are met), redis will fork a child process, and the child process writes data to a temporary RDB on disk In the file, when the child process finishes writing the temporary file, replace the original RDB (the default file name is dump.rdb)

RDB backup conditions and commands:
1. Execute the SAVE command, execute it in the current thread, it will be stuck
2. Execute the BGSAVE command, execute it in the background thread, and return immediately
3. When the configuration conditions given by the user are met, Redis will automatically store the memory in the memory. All data is snapshotted and stored on the hard disk. Consists of two parameters: time and the number of keys changed. When the number of keys changed within the specified time is greater than the specified value, a snapshot will be taken. Three conditions have been preset in the configuration file redis.conf:
save 900 1 # At least 1 key within 900 seconds If changed, take a snapshot save 300 10 # Take a snapshot if at least 10 keys have been changed in 300 seconds save 60 10000 # Take a snapshot if at least 10,000 keys have been changed in 60 seconds

Advantages and disadvantages of RDB:
regular backup, Redis has high efficiency, but it is easy to cause data loss, the amount of loss is related to the backup strategy, for example: backup once every 5 minutes, but it goes down at the 8th minute, then the data for the next 3 minutes is lost

1. Principle of AOF mode
AOF mode: AOF can be persistent throughout the whole process. Every time Redis executes a command to modify data, it will add this command to the AOF file. When Redis restarts, it will read the AOF file for "reloading" put" to restore to the last moment before Redis shut down.
Since the os will cache the modifications made by the write in the kernel, it may not be written to disk immediately. In this way, the persistence of the aof method may still lose some modifications. However, we can tell redis through the configuration file when we want to force os to write to disk through the fsync function. There are three ways as follows (default: fsync once per second)

appendonly yes              //启用aof持久化方式
# appendfsync always      //每次收到写命令就立即强制写入磁盘,最慢的,但是保证完全的持久化,不推荐使用
appendfsync everysec     //每秒钟强制写入磁盘一次,在性能和持久化方面做了很好的折中,推荐
# appendfsync no    //完全依赖os,性能最好,持久化没保证

AOF**Advantages and disadvantages**
AOF can basically ensure that data is not lost, but AOF persistent files will become larger and larger. For example, if we call the incr test command 100 times, all 100 commands must be saved in the file. In fact, 99 of them are redundant.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324576865&siteId=291194637