How does Redis persist to the hard disk

Why does Redis need persistence

We will use Redis more or less in our projects, and Redis is mainly used as a cache database. Using Redis can greatly improve the performance of our programs. One of the reasons why using Redis is faster is that Redis data is stored in memory, and applications only need to read from memory to access Redis.

image

Reading data from the memory can indeed increase the access speed, but when Redis hangs, the data in the memory will be lost. In order to prevent data loss, we need to persist the data to the hard disk. When Redis hangs, the data has been stored in the hard disk. After Redis restarts, the data in the hard disk will be reloaded into the memory.

image

So here comes the problem.

How is Redis persistent?

Two persistence methods

Two different persistence methods are provided in Redis: RDB and AOF.

  • The RDB persistence method can perform snapshot storage of your data at a specified time interval.

  • The AOF persistence method records each write operation to the server. When the server restarts, these commands will be re-executed to restore the original data. The AOF command uses the Redis protocol to append and save each write operation to the end of the file. Redis can also rewrite the AOF file in the background so that the volume of the AOF file is not too large.

    When we enable both persistence methods at the same time, the AOF file will be loaded first to restore the original data when Redis restarts, because under normal circumstances the data set saved by the AOF file is more complete than the data set saved by the RDB file.

Let's take a look at the Redis configuration file redis.conf and take a look at the configuration of persistence.

If we want to enable AOF mode, we need to change

appendonly no修改为appendonly yes

image

This is the configuration of 3 AOF persistence strategies.

  • appendfsync everysec: Means that the operation of data synchronization to the hard disk is performed once every second, then the data is likely to be lost within this second interval. This is the default strategy of the program.

  • appendfsync always: Indicates that data synchronization will be performed every time it is written.

  • appendfsync no: Indicates that Redis does not perform data synchronization and is performed by the operating system.

Let's take a look at the RDB form of storage configuration.

image

The format here is:

save <时间间隔(秒)>  <写入次数>

From the above configuration, we know:

  • save 900 1: If the value of at least one key changes within 900 seconds, it will be saved.

  • save 200 10: If at least 10 key values ​​change within 300 seconds, it will be saved.

  • save 60 10000: The rules are the same as above.

If we want to disable it, just comment directly.

About RDB

Let's take a look at the advantages and disadvantages of RDB.

Advantages of RDB

  • RDB is a very compact file. It saves a data set at a certain point in time. It is very suitable for data set backup. For example, you can save the data in the past 24 hours every hour, and save the past 30 days every day. data. In this way, even if something goes wrong, you can restore to a different version of the data set according to your needs.

  • RDB is a compact single file that can be easily transferred to another remote data center and is very suitable for disaster recovery.

  • When RDB saves the RDB file, the only thing the parent process needs to do is to fork a child process. The next work is done by the child process. The parent process does not need to do other IO operations, so the RDB persistence method can maximize the performance of redis .

  • Compared with AOF, the RDB method will be faster when recovering large data sets.

Disadvantages of RDB

  • If you want to have the least amount of data lost when Redis stops working unexpectedly (such as a power outage), then RDB is not for you. Although you can configure different save time points (for example, every 5 minutes and 100 write operations to the data set), it is a relatively heavy task for Redis to completely save the entire data set. You usually do it every 5 minutes Or do a complete save longer. In case of unexpected downtime in Redis, you may lose a few minutes of data.

  • RDB needs to fork child processes frequently to save the data set to the hard disk. When the data set is relatively large, the fork process is very time-consuming and may cause Redis to fail to respond to client requests within a few milliseconds. If the data set is huge And if the CPU performance is not very good, this situation will last for 1 second, and AOF also needs a fork, but you can adjust the frequency of rewriting the log file to improve the durability of the data set.

From the above, we can know that RDB saves data. Since data saving is a very heavy operation, it saves data for a certain period of time. Therefore, it will be faster to restore data with RDB. But the recovered data may be lost.

About AOF

Advantages of AOF

  • Using AOF will make Redis more durable. We can use different fsync strategies: no fsync, fsync per second, and fsync every time you write. Using the default fsync per second strategy, Redis's performance is still very good (fsync is processed by a background thread, and the main thread will try its best to process client requests). Once a failure occurs, you can lose up to 1 second of data.

  • The AOF file is a log file that is only appended, so it does not need to be written to seek, even if for some reasons (disk space is full, downtime during writing, etc.), we can still use the complete write command. The redis-check-aof tool fixes these problems.

  • Redis can automatically rewrite AOF in the background when the volume of the AOF file becomes too large: the new AOF file after rewriting contains the minimum set of commands required to restore the current data set. The entire rewriting operation is absolutely safe, because Redis will continue to append commands to the existing AOF file during the process of creating a new AOF file. Even if there is a downtime during the rewriting process, the existing AOF file will not be lost. . Once the new AOF file is created, Redis will switch from the old AOF file to the new AOF file and start appending the new AOF file.

  • The AOF file stores all the write operations performed on the database in an orderly manner. These write operations are saved in the format of the Redis protocol, so the content of the AOF file is very easy to be read by people, and it is easy to analyze the file (parse). Exporting AOF files is also very simple: For example, if you accidentally execute the FLUSHALL command, but as long as the AOF file is not rewritten, just stop the server, remove the FLUSHALL command at the end of the AOF file, and restart Redis, You can restore the data set to the state before FLUSHALL was executed.

Disadvantages of AOF

  • For the same data set, the volume of the AOF file is usually larger than the volume of the RDB file.

  • Depending on the fsync strategy used, the speed of AOF may be slower than RDB. In general, the performance of fsync per second is still very high, and turning off fsync can make AOF as fast as RDB, even under high load. However, when dealing with huge writes and loads, RDB can provide a more guaranteed maximum latency (latency).

From the above, we can know that the AOF mode saves write commands. Since the save is an operation command, it is easier to save in this step, and the restored data will be more complete, but because the saved is a command, when restoring These commands need to be executed once, which is time-consuming.

In summary, we can use the RDB and AOF hybrid mode for persistence. In the future, Redis may combine RDB and AOF into a single persistence model.

reference

Redis official website: http://www.redis.cn/topics/persistence.html

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

回复"java" 获取java电子书;
 
回复"python"获取python电子书;
 
回复"算法"获取算法电子书;
 
回复"大数据"获取大数据电子书;
 
回复"spring"获取SpringBoot的学习视频。
 
回复"面试"获取一线大厂面试资料
 
回复"进阶之路"获取Java进阶之路的思维导图
 
回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)
 
回复"总结"获取Java后端面试经验总结PDF版
 
回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)
 
回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/115325050