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 redis to achieve persistence

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

Share

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

How to use redis to achieve persistence? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.

RDB

RDB is a means of persistence that writes in-memory data to disk under certain conditions. So under what conditions should it be written? It is impossible to write without brain, write one at a time, affect performance, and you can't wait for a long time to write one. In case there is a downtime and all data is lost, you might as well use memcached. In the configuration of redis, there is a section like this:

Save 900 1

Save 300 10

Save 60 10000

A critical section of configuration, which is the core of RDB persistence. It means:

1. If there is a key change (insert or update) in 900 seconds, I will synchronize it to disk.

two。 If there are 10 key changes (insert or update) in 300 seconds, I will synchronize to disk.

3. If there are 10000 key changes (insert or update) in 60 seconds, I will synchronize to disk.

How do you know these time points and the number of changes? there are two other crucial things, one is called the dirty counter, and the other is called lastsave (the time of the last save). The dirty counter records the number of key changes since the last save, and the lastsave records the time of execution of the save. For example, the initial time is time1,dirty is 0, when there are 20 key changes, dirty is 20, and now the time is time2. Time2-time1 > = 300, if the second condition is met, the data in memory will be save and the dirty will be cleared to 0, and then wait for the condition to be triggered.

If I have 100000 key in 60 seconds, then the problem is that when a large number of disk io comes, the redis main process will block and all the commands during this period will not be executed, which is impossible, so there is a child called bgsave, which is a child process from the redis main process fork, which specializes in performing the persistence of rdb.

The saved file format is in binary format. In case the database goes down and recovery does not require human intervention, redis will automatically read the disk file.

AOF

Unlike RDB, AOF stores the commands you execute. When the aof function is turned on, the update commands executed will not be written directly to the aof file, but will be written to an aof buf first. We know that we can't keep writing to buf. Buf is also memory, so when can it be synchronized to disk? There is also such a configuration in redis.

Appendfsync always

Appendfsync everysec

Appendfsync no

It means:

1. As long as there are updated orders, I will synchronize.

two。 If the last synchronization time is now more than one second, synchronize.

3. Out of sync, waiting for the operating system to judge (when I am free I will synchronize)

According to the analysis, the first kind of io is frequent, the io pressure is high, but the probability of data loss is the smallest, the second kind of io pressure is not very great, at most, the data is lost for about 1 second, and the third kind of io pressure is very small, and the probability of data loss is too high. Generally speaking, the second kind is taken into consideration. But there is also a problem, I executed INCR num 100 times, according to reason num is 100 of the same command, no problem, then I would like to ask the implementation of 100 times INCR num and SET num 100 what is the difference, the same result of the former 99 times more space, very wasteful ah, so there is an AOF rewrite, how it does it. It's simple: first read the current value from the database, and then replace it with a record, which is the principle of AOF rewriting. Rewriting takes time, so it is also a child process to deal with. In the process of rewriting, what to do if a new command comes? the old way is to write buf buffering. After rewriting is complete, append the commands in buf to the new aof, and then replace the old aof with the new aof.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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