redis04: Detailed explanation of RDB and AOF of persistence technology in redis

One: What is redis persistence: Redis工作时数据都存储在内存中,万一服务器断电,则所有数据都会丢失。针对这种情况,Redis采用持久化机制来增强数据安全性。Persistence technology is divided into RDB and AOF, I will introduce these two mechanisms separately below.

Two: About RDB (Redis DataBase)
1. What is RDB: In short, it is to generate SNAPSHOTTING and store the data stored in redis on disk at different points in time;

2. RDB mechanism principle: Redis will create (Fork) a sub-process separately for persistence, and will first write the data to a temporary file, and then use this temporary file to replace the last persistence after the persistence process is over The converted file. During the entire process, the main process does not perform any IO operations, which ensures extremely high performance. If large-scale data recovery is required, and the integrity of data recovery is not very sensitive, the RDB method is better than the AOF method More efficient . The disadvantage of RDB is that the data after the last persistence may be lost.

3. The trigger timing of RDB:

#   save ""   禁用RDB持久化的策略,只要不设置任何save指令,或者给
save传入一个空字符串参数也可以.  

sava  秒钟  写操作次数
save 900 1    900s内至少修改1个key会触发RDB
save 300 10    300s内至少修改了10个key会触发RDB
save 60 10000   60s内至少修改了10000次的key会触发RDB

Case demonstration, change save 300 10 to save 60 5 and delete the dump.rdb file first to observe the effect

Insert picture description here

Insert picture description here

Insert picture description here

Here you can see that when we modify the key more than 5 times, the dump.rdb file is regenerated.

This is after we closed redis and reopened it and looked at dump.rdb and found that our previous data has been reloaded into memory.

Insert picture description here

4. How to trigger RDB snapshot:

1.配置文件中默认的快照配置  
拷贝后重新使用  cp dump.rdb   dump.rdb_bk

2.save或者是bgsave命令
save和bgsave一般应用在事件比较紧急的情况下,也就是改动了键值立马保存在dump.rdb中。

3.执行flushall命令,也会产生dump.rdb文件,但里面是空的,无意义。

Insert picture description here

5. Advantages and disadvantages of RDB

优势:
1.适合大规模的数据恢复
2.对数据完整性和一致性要求不高

劣势:
1.在一定间隔时间做一次备份,所以如果redis意外down掉的话,就会丢失最后一
次快照后的所有修改。
2.Fork的时候,内存中的数据被克隆了一份,大约2倍的膨胀性需要考虑。

Three: About AOF (Append Only File)
1. What is AOF:

Record each write operation in the form of a log, record all write instructions executed by Redis ( read operations are not recorded ), only append files but not rewrite files, redis will read the file to rebuild the data at the beginning of the startup In other words, if redis is restarted, the write command will be executed from front to back according to the content of the log file to restore the completed data.

Note that the aof function in redis.conf is closed by default, we need to manually open it

Insert picture description here

2. The form of data recovery in AOF (before this operation, we can delete the dump.rdb file temporarily, and then copy a file named redis.conf_aof in the current directory)

Insert picture description here

As we changed several key values, we can find that the two files appendonly.aof and dump.rdb have been generated separately.

Insert picture description here

3. When appendonly.aof and dump.rdb exist in the directory, the appendonly.aof file has a high priority. For example, we can add some wrong text content to the above appendonly.aof file (the last line is the content we scribbled) , For the correct file dump.rdb, we do not change it.

Insert picture description here

When we restarted redis, we found that there was an error, so we can determine that when these two files appear at the same time, the priority of appendonly.aof is higher.

Insert picture description here

At this time, redis also provides us with a method to repair the damaged appendonly.aof file. If ./redis-check-aof --fix ../appendonly.aofSuccessfully appears, the repair is successful. Then, when redis is restarted again, it can start normally, and the previous data can also be loaded into memory. .

Insert picture description here

4. About several important parameters in AOF

1 appendonly   no  默认是mo,一般我们要用AOF时要将no改为yes

2 appendfilename "appendonly.aof"   生成的AOF的文件名,一般情况下不要去修改这个名字

3 Appendfsync  everysec (其他两个值分别是Always和no) 

Always:同步持久化,每次发生数据变更会立即记录到磁盘,性能较差但数据完整性比较好
Everysec:出厂默认推荐,异步操作,每秒记录,如果一秒内宕机,有数据丢失(此参数用的较多)
no :从不同步


5. About Rewrite in AOF:

5.1 rewrite:
AOF uses the file append method, and the file will become larger and larger. In order to avoid this situation, a
new rewrite mechanism has been added. When the size of the AOF file exceeds the set threshold, Redis will start the AOF file The content is compressed, and only the minimum instruction set that can restore the data is retained.
You can use the command gbrewriteaof

5.2 Principle of rewriet rewriting: When the
AOF file continues to grow and is too large, a new process will be fork to rewrite the file (also write the temporary file first and then rename), traverse the data in the memory of the new process, each record has one The Set statement. The operation of rewriting the aof file does not read the old aof file, but rewrites the database content in the entire memory with a command to rewrite a new aof file, which is similar to a snapshot.

5.3 Rewrite trigger mechanism:
Redis will record the AOF size during the last rewrite. The default configuration is to trigger when the AOF file size is twice the size after the last rewrite and the file is larger than 64M.

Insert picture description here

6. The advantages and disadvantages of AOF

优势:见上述4.3
劣势:相同数据集的数据而言aof文件要远大于rdb文件,恢复速度慢于rdb
AOF运行效率要慢于rdb,每秒同步策略效率较好,不同步效率和rdb相同。

Four: Which one should be used between RDB and AOF?

It is recommended to turn on these two persistence methods at the same time, because in this case
1. When redis restarts, AOF will be loaded first to restore the original data, because under normal circumstances the data set saved by the AOF file is saved than the RDB file. The data set of
RDB must be complete. 2.The data of RDB is not real-time, and when both are used, the server will only find AOF files when restarting, but RDB also needs to exist at this time, because RDB is more suitable for backing up the database (AOF is constantly changing Good backup), fast restart, and there will be no potential BUG of AOF, it can be reserved as a means of data recovery.

Guess you like

Origin blog.csdn.net/weixin_44080445/article/details/114108582