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

How to use the persistence of redis

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use the persistence mode of redis". In the daily operation, I believe that many people have doubts about how to use the persistence mode of redis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the persistence mode of redis". Next, please follow the editor to study!

Redis supports two ways of persistence, which can be used alone or in combination.

1. RDB

Rdb persistence is done through snapshots, and when certain conditions are met, redis will automatically snapshot all data in memory and store it on the hard disk. It is stored by default in the dump.rdb file in the redis root directory. (the file name is dbfilename in the configuration file)

Time for redis to take a snapshot (in the configuration file redis.conf)

Save 9001: means that if at least one key is changed within 900 seconds, a snapshot will be taken.

Save 300 10

Save 60 10000

The process of realizing snapshots automatically by redis

1:redis uses the fork function to make a copy of the current process (child process)

2: the parent process continues to receive and process commands from the client, while the child process begins to write data in memory to temporary files on the hard disk

3: when the child process has written all the data, it will replace the old RDB file with the temporary file. At this point, a snapshot operation is completed.

Note: redis will not modify the RDB file during the snapshot, and will only replace the old file with the new one after the snapshot, which means that the RDB file is complete at any time. This enables us to back up the redis database by backing up RDB files regularly. RDB files are compressed binary files, which occupy less space than the data in memory, which is more convenient for transmission.

Manually execute the save or bgsave command to have redis perform the snapshot.

The difference between the two commands is that the save is snapped by the main process, which blocks other requests. Bgsave is that redis executes the fork function to copy a child process for snapshot operation.

File repair: redis-check-dump

Advantages and disadvantages of rdb

Advantages: because there are data snapshot files stored, it is very convenient to restore data.

Cons: all data changed since the last snapshot will be lost.

II. AOF

Persistence in aof mode is done through log files. Redis does not enable aof by default. You can enable it by parameter appendonly.

Appendonly yes

The aof file is saved in the same location as the rdb file, both of which are set by the dir parameter. The default file name is appendonly.aof, which can be modified by the appendfilename parameter.

Appendfilename appendonly.aof

Timing of redis write command synchronization

A ppendfsync always executes every time.

Appendfsync everysec performs synchronization operation once per second by default (recommended, default)

Appendfsync no does not synchronize actively, which is done by the operating system, once every 30 seconds.

Aof log file rewrite

Auto-aof-rewrite-percentage 100 (rewritten again when the current aof file size exceeds how many percent of the aof file size at the time of the last override, based on the aof file size at startup if it was not previously overridden)

Auto-aof-rewrite-min-size 64mb

Execute bgrewriteaof manually for rewriting

The rewriting process is only related to the data in memory and has nothing to do with the previous aof file. The so-called "rewrite" is actually an ambiguous word. In fact, AOF rewriting does not require any writing or reading to the original AOF file, it is aimed at the current value of the key in the database.

File repair: redis-check-aof

Dynamically switch redis persistence mode, from RDB to AOF (Redis 2.2 or above is supported)

CONFIG SET appendonly yes

CONFIG SET save "" (optional)

Note: when redis starts, if both rdb persistence and aof persistence are turned on, then the program will first use aof to recover the dataset, because the data saved in aof is usually the most complete. If the aof file is missing, the database content is empty after startup.

Note: if you want to switch the running redis database from RDB to AOF, it is recommended to use dynamic switching first, and then modify the configuration file to restart the database. (you cannot directly modify the configuration file and restart the database, otherwise the data in the database will be empty.)

At this point, the study on "how to use the persistence of redis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Servers

Wechat

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

12
Report