Explain the two persistence mechanisms RDB in Redis (frequently asked interviews, commonly used in work)

To put it bluntly, redis's persistence mechanism is to write a snapshot of the data set in the memory to the disk within a specified time interval. What does it mean to read the snapshot file directly into the memory when it is restored? We all know that the data in the memory, if we lose power, the data will be lost, but the students who have played redis should know that the data is still there when we turn off the power, so it must be in Reloaded the persistent file when redis started

Redis provides two methods for persistence, one is RDB persistence by default, and the other is AOF (append only file) persistence.

Explain the two persistence mechanisms RDB in Redis (frequently asked interviews, commonly used in work)

1. What is RDB?
The principle is that redis will separately create (fork) a child process exactly the same as the current process for persistence, and all data (variables) of this child thread.

Environment variables, program counters, etc.) are exactly the same as the original process. The data will be written to a temporary file. After the persistence is over, the temporary file will be used to replace the last persisted file. During the whole process, The main process does not perform any io operations, which ensures extremely high performance

1. Where is this persistent file

2. When did he fork the child process, or when did he trigger the RDB persistence mechanism

At shutdown, if aof is not turned on, the default snapshot configuration execution command save or bgsave save in the configuration file will be triggered. Save or bgsave save is just to save, and all others will block bgsave: redis will perform snapshot operations asynchronously in the background, and can respond to client requests. The flushall command is empty and meaningless

2. What is aof(–fix) ls -l --block-size=M?
The principle is to write the operation log of Reids to the file in append mode, and the read operation is not recorded

1. Where is this persistent file

2. Trigger mechanism (based on configuration file configuration items)

no: means waiting for the operating system to synchronize the data cache to the disk (fast, no guarantee of persistence) always: synchronization and persistence, every time a data change occurs, it will be recorded to the disk immediately (slow, safe) everysec: means synchronization once per second ( The default value is fast, but data within one second may be lost)

3.aof rewrite mechanism

When the AOF file grows to a certain size, Redis can call bgrewriteaof to rewrite the log file. When the growth rate of the AOF file size is greater than this configuration item, the rewriting is automatically turned on (here means more than 100% of the original size). auto-aof-rewrite-percentage 100 When the AOF file grows to a certain size, Redis can call bgrewriteaof to rewrite the log file. When the AOF file size is larger than this configuration item, the rewrite is automatically turned on auto-aof-rewrite-min-size 64mb

Explain the two persistence mechanisms RDB in Redis (frequently asked interviews, commonly used in work)

4. Hybrid persistence mechanism after redis4.0

Turn on hybrid persistence

The hybrid persistence of version 4.0 is turned off by default and is controlled by the aof-use-rdb-preamble configuration parameter. Yes means to enable, no means to disable, and it is enabled by default after 5.0.

Hybrid persistence is completed by bgrewriteaof. The difference is that when hybrid persistence is turned on, the child process of fork first writes the full amount of the shared memory copy to the aof file in RDB mode, and then rewrites the increment of the buffer The command is written to the file in AOF mode. After the writing is completed, the main process is notified to update the statistical information, and the new AOF file containing RDB format and AOF format is replaced with the old AOF file. Simply put: the first half of the new AOF file is full data in RDB format, and the second half is incremental data in AOF format.

Advantages: Hybrid persistence combines the advantages of RDB persistence and AOF persistence. Since most of them are in RDB format, the loading speed is fast. At the same time, combined with AOF, the incremental data is saved in AOF mode, and the data is less lost.

Disadvantages: Poor compatibility. Once the hybrid persistence is turned on, the AOF file is not recognized in the version before 4.0. At the same time, because the previous part is in RDB format, the readability is poor

Small summary:

Explain the two persistence mechanisms RDB in Redis (frequently asked interviews, commonly used in work)

1. Redis provides an RDB persistence solution, why do I need AOF?

Optimize the problem of data loss. RDB will lose the data after the last snapshot, and the loss of AOF will not exceed 2 seconds.

2. If aof and rdb exist at the same time, who will listen to?

aof

3. RDB and AOF advantages and disadvantages

rdb is suitable for large-scale data recovery. Data integrity and consistency are not high. A backup is made at a certain interval. If redis accidentally shuts down, all operations after the last snapshot will be lost. Aof depends on the configuration items.

1. The official recommendation is that the two persistence mechanisms are enabled at the same time. If both are enabled at the same time, the AOF persistence mechanism is preferred. Performance recommendations (here only for the stand-alone version of redis persistence): Because the RDB file is only used for backup purposes, as long as 15 One backup every minute is enough, and only save 900 1 is the rule.

If Enalbe AOF, the advantage is that in the worst case, it will only lose no more than two seconds of data. The startup script is simpler and only loads your own AOF file. The first cost is the continuous IO, and the second is that the block caused by the new data generated in the rewrite process is written to the new file at the end of the AOF rewrite is almost inevitable. As long as the hard disk is licensed, the frequency of AOF rewrite should be minimized. The default value of 64M for the basic size of AOF rewrite is too small and can be set to above 5G. When the default size exceeds 100% of the original size, the rewrite can be changed to an appropriate value.

It’s not easy to create, and I think it’s useful to you. Please convert your comments and update the redis cluster in the next article

Guess you like

Origin blog.csdn.net/yueyunyin/article/details/108430864