Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis-- persistence RDB and AOF principle

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Foreword:

There are two redis persistence methods: RDB snapshot and AOF (default is RDB mode). When the Redis server is restarted, the data will be automatically restored, first from AOF, and then from RDB.

1. The principle of RDB snapshot mode RDB mode:

When redis needs to be persisted (execute SAVA or BGSAVA commands, or when configuration conditions are reached), redis will fork a child process, which writes data to a temporary RDB file on disk. When the child process finishes writing the temporary file, it replaces the original RDB (default file name is dump.rdb).

RDB backup conditions and commands:

1. Execute the SAVE command, which will get stuck when executed in the current thread.

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 snapshot all the data in memory and store them on the hard disk. Consists of two parameters: time and the number of keys changed. Snapshots are taken when the number of keys changed within the specified time is greater than the specified value, and three conditions have been preset in the configuration file redis.conf:

Snapshot if at least one key is changed within save 9001 # 900s save 300 10 # 300 seconds if at least 10 keys are changed within 60 seconds save 60 10000 # 60

Advantages and disadvantages of RDB:

Regular backup, Redis is efficient, but it is easy to cause data loss. How much data is lost is related to the backup strategy. For example, if you back up once every 5 minutes, but if you go down at 8 minutes, you will lose the data in the next 3 minutes.

Second, the principle of AOF mode AOF mode:

AOF can persist the whole process, and every time Redis executes a command to modify data, it will add this command to the AOF file, and when Redis restarts, it will read the AOF file for "replay" to restore to the last moment before Redis shuts down.

Because os caches changes made by write in the kernel, it may not be written to disk immediately. In this way, it is still possible to lose some of the changes in aof persistence. 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 to do the following (default: fsync once per second)

Appendonly yes / / enable aof persistence mode

Appendfsync always / / Force to write to disk every time you receive a write command, the slowest, but guaranteed to be fully persistent, not recommended

Appendfsync everysec / / is forced to write to disk once per second, which makes a good compromise between performance and persistence. It is recommended

Appendfsync no / / is completely dependent on os, with the best performance and no guarantee of persistence

Advantages and disadvantages of AOF

AOF can basically guarantee that data will not be lost, but AOF persistence 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, but 99 of them are redundant.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report