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 has several ways to persist data

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Today, I will talk to you about the data persistence methods of redis, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Redis provides two ways to persist data, namely RDB (Redis DataBase) and AOF (Apend Only File).

Redis is an in-memory database that stores data in memory and is much faster to read and write than traditional databases that store data on disk. But once the process exits, Redis data is lost.

In order to solve this problem, Redis provides two persistence schemes, RDB and AOF, to save the data in memory to disk to avoid data loss.

RDB mode

RDB is a snapshot-based persistence method that persists data at a certain time to disk.

In the process of data persistence, redis will first write the data to a temporary file, and the last persisted file will not be replaced with this temporary file until the persistence process is over. It is this feature that allows us to back up at any time, because snapshot files are always fully available.

For the RDB mode, redis creates (fork) a child process separately for persistence, while the main process does not perform any IO operations, thus ensuring the extremely high performance of redis.

If large-scale data recovery is needed and is not very sensitive to the integrity of data recovery, then RDB is more efficient than AOF.

RDB trigger mechanism is divided into manual trigger using instructions and automatic trigger with redis.conf configuration.

The instructions that manually trigger Redis for RDB persistence are:

Save, which blocks the current Redis server. During the execution of the save instruction, Redis cannot process other commands until the RDB process is complete.

Bgsave, when executing this command, Redis will perform snapshot operations asynchronously in the background, and Redis can still respond to client requests. The specific operation is that the Redis process executes the fork operation to create the child process, and the RDB persistence process is responsible for the child process, which ends automatically after completion. Redis only blocks during fork, but usually for a short period of time. However, if the amount of Redis data is particularly large, the fork time will be longer and the memory footprint will double, which requires special attention.

AOF mode

The AOF method records the write instructions that have been executed and executes the instructions again in the order before and after the cluster when the data is recovered.

The AOF command appends the save of each write to the end of the file using the redis protocol. Redis can also rewrite AOF files in the background, so that the size of AOF files is not too large.

The default AOF persistence policy is fsync once per second (fsync refers to recording write instructions in the cache to disk), because in this case, redis can still maintain good processing performance, and even if redis fails, only the last second of data will be lost.

It doesn't matter if you happen to encounter incomplete log writes due to full disk space, full inode, or power outage when you append logs. Redis provides a redis-check-aof tool that can be used to repair logs.

Because of the append method, if no processing is done, the AOF file will become larger and larger. For this reason, redis provides an AOF file rewriting (rewrite) mechanism, that is, when the size of the AOF file exceeds the set threshold, redis will start the content compression of the AOF file, keeping only the minimum instruction set that can recover the data.

For example, it may be more vivid. If we call INCR instructions 100 times, we have to store 100 instructions in the AOF file, but this is obviously very inefficient. We can combine these 100 instructions into a single SET instruction, which is how the rewriting mechanism works.

In AOF rewriting, the process of writing temporary files first and then replacing them is still used, so power outages, disk fullness and other problems will not affect the availability of AOF files.

After reading the above, do you have any further understanding of the two data persistence methods of redis? If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading.

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