4
头图

Hello everyone, I am a side dish.
A man who hopes to be about architecture ! If you also want to be the person I want to be, otherwise click on your attention and be a companion, so that Xiaocai is no longer alone!

This article mainly introduces the persistence of

If necessary, you can refer to

If it helps, don’t forget to 161508ed223338 ❥

The WeChat public account has been opened, , students who have not followed please remember to pay attention!

Recently, I have gone further and further on the interview road. Redis definitely a popular interview direction. Like several data structures? How to implement delay queue? What is the elimination mechanism? They are almost numb when asked, and these questions often go around their heads. Then let's talk about a more common and moderately difficult interview question in this article. the persistence strategy of 161508ed22339a Redis

a question at the beginning. I believe that there are not a few students who were asked about 161508ed2233d5 Redis persistence , and there are definitely not a few students who answered correctly. Some Redis persistence 161508ed2233da. After all, they are also AOF and RDB. two concepts, as long as you prepare for the interview, you will not be asked too badly. But do you really understand or do you just deal with memory for the interview? Do you know what word abbreviation is AOF and RDB Have you implemented it? Do you really think that the interviewer can’t tell whether you’re memorizing the question or doing it practically? If you get half of the 4 questions, then you might as well look down. Maybe you will get some gains. At least you have some side dishes in your mind when answering the interview questions~!

Redis persistence

What is Redis persistence?

Let's not remember to move forward in the direction of the solution, first understand the meaning of this question.

persistence is to keep the data permanently. So what is Redis persistent ? That is to write the data stored in the memory of Redis to the disk to prevent the memory data loss problem when the service is down. Then some friends said, the disk is damaged, how can the data persist? Even if multiple backups can solve the disk damage problem, how to fix it if multiple points are lost? Stop, stop, this article is about Redis memory data -> disk persistence, don't expect to rely on this question to talk to the interviewer for half an hour~!

Redis persistence problem from several points in this article.

It is also a three-point general direction, a three-step strategy to solve your persistence problem.

One, RDB

Let's first solve one of the opening problems, what is the full name of the word RDB RDB ( R edis D atabase B ackup file ) --- Redis data backup file, also known as Redis data snapshot.

This thing is used to record all the data in the memory to the disk. When the Redis instance fails and restarts, the snapshot file is read from the disk to restore the data. Inner ecstasy, it seems that the first concept learned can solve the Redis persistence problem~

Before learning RDB , we first understand the two core concepts fork and cow , we will explain below, here is a key point.

RDB is the default persistence mechanism in Redis. It saves the data in the memory to the disk as a snapshot according to a certain period of time. It will generate a special type of file .rdb data file, which can be saved through the save in the configuration file. Parameters to define the period of the snapshot.

We start with two configuration parameters in the configuration file, the first is the save configuration.

This instruction is Redis main process of RDB, which will block all commands

image-20210914183245450

We found the configuration about sava

1、

dbfilename dump.rdb

The function of this configuration item is to define the rdb file name (note that the name cannot be defined as a path, but can only be defined as a file name)

After we execute the save command, we can see a dump.rdb file redis

2、

save <seconds> <changes>

The function of this configuration item is to define how many changes occur during how long and how many times bgsave will be executed. If it is save "" it means that RDB is disabled.

We next open save configuration for testing

dbfilename dump-test.rdb  # 文件名为 dump-test.rdb
save 3600 1     # 在 3600 秒内发生一次更改,便会执行 bgsave

We enter the operation redis-cli

image-20210914201037760

Then we can see the newly generated dump-test.rdb file in the current directory after we exit

Explain that our configuration is effective, then we directly restart Redis to see if there is still the data we just saved

Seeing our data, it shows that redis persistence is successful. Then we delete the newly generated dump-test.rdb file and restart redis

image-20210914201528245

This can indicate that Redis relies on to restore file data when it starts. So what we have been talking about above is bgsave , how is bgsave

We have mentioned two concepts fork and cow . I don’t know if I still have an impression. These two concepts are the key~!

bgsave starts, it will fork main process to get a new child process, and the child process is sharing the memory data of the main process. The child process will write the data to a temporary .rdb file on the disk. When the child process finishes writing the temporary file, it will replace the .rdb This is fork , so what is cow ? cow full name of copy-on-write technology. When the main process performs a read operation, it accesses the shared memory, and when the main process performs a write operation, it will copy a copy of the data and perform the write operation.

The specific process is as follows:

What are the advantages of this persistence method?

  • Convenient for persistence, there is only one dump.rdb file
  • Good disaster tolerance, you can save files to a safe disk
  • To maximize performance, forks the child process to complete the write operation, let the main process continue to process commands, IO , and ensure the high performance of Redis

There are also disadvantages:

  • Data security is low. RDB is persisted at intervals (save <seconds> <change>). If Redis fails during persistence, data will be lost, so this method is suitable for data requirements Use in less rigorous situations
  • Long storage time, if the amount of data is large, the time to save the snapshot will be long, which will take up disk space

good and bad, consider using

Two, AOF

AOF Full name A ppend O nly F ile (Additional file). The function is that every write command processed by Redis AOF file, which can be regarded as a command log file.

This feature is turned off by default, we can view the configuration items related to AOF redis.conf

1、

appendonly yes   # 开启 AOF 日志记录功能,默认是关闭的

2、

appendfilename "appendonly.aof"  # AOF 文件的名称

The above two configuration items are used to enable AOF logging, so there is an additional configuration item that needs to be understood

3、

appendfsync everysec   # AOF 命令记录的频率

This configuration item has three optional values

Configuration itemTime to refreshadvantageshortcoming
AlwaysSynchronous flashingHigh reliability, almost no data lossPerformance impact
everysecSwipe per secondModerate performanceLose up to 1 second of data
noOperating system controlBest performancePoor reliability and may lose a lot of data

There understand Mysql in Relay log students log, this model would not have been very strange.

Principle: It is to append the write command to the end of the AOF file. The use of AOF persistence needs to set the synchronization option to ensure the timing of the write command to the disk file. This is because writing to the file will not immediately The memory is synchronized to the disk, but first stored in the cache area, and then the operating system determines when to synchronize to the disk.

Let's turn on the AOF record function to check:

It can be seen that each of our operations has been recorded in the AOF file , we can also get the data just stored by restarting the Redis , indicating that the persistence is effective~

We see the above AOF record file, do you think it is very regular? But in the online environment, the more regular it is, it is not good, because this file is mainly for the machine to see, not for us, so we better be able to compress it.

In order to solve the AOF file size growing problems, the user can Redis send bgrewriteaof command, which will pass by removing redundant command AOF file to rewrite (rewrite) AOF file, Make the AOF file as small as possible. bgrewriteaof works and bgsave create a snapshot of the works are very similar: Redis creates a child process, the child is then responsible for AOF files to be rewritten. Because AOF file rewriting also requires the use of subprocesses, the performance problems and memory occupancy problems caused by the creation of subprocesses in snapshot persistence also exist in AOF persistence.

Since there is manual trigger compression, there is also automatic trigger compression, which has to talk about two configuration items in the configuration file

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

This configuration item means that when the volume of the AOF file is greater than 64MB, and the volume of the AOF file is at least double (100%) larger than the volume after the last rewrite, Redis will execute the bgrewriteaof command.

In summary, its advantages are as follows:

  • Data Security. AOF persistence can be configured appendfsync properties always , each one write command operations will be recorded AOF file once
  • consistency. Write files through the append model, even if the server is down in the middle, you can use the redis-check-aof tool to solve the data consistency problem

The disadvantages are as follows:

  • AOF file is larger than the RDB file, and the recovery speed is slow
  • When the data set is large, the file startup efficiency is lower RDB

The same is good and bad, use it according to your discretion

Three, the difference between the two

We have introduced the two separately. Let’s review the difference between the two.

aspectRDBAOF
PersistenceTake a snapshot of the entire memory regularlyRecord every command executed
Data integrityIncomplete, will be lost between backupsRelatively complete. Depends on the brushing strategy
File sizeThere will be compression, the file size is smallRecord commands, the file size is very large
Downtime recovery speedsoonslow
Data recovery priorityLow, because data integrity is not as good as AOFHigh because of higher data integrity
System resource occupationHigh, large CPU and memory consumptionLow, mainly due to disk IO resources. And AOF will take up a lot of CPU and memory resources when rewriting
scenes to be usedCan tolerate data loss for several minutes, and pursue faster startup speedHigh data security requirements

After reading the above, I must have a certain understanding of the two persistence mechanisms. Both have advantages and disadvantages, so how do we choose? Here are a few comments~

  1. If you can tolerate data loss for a short period of time, you can use the RDB mechanism to generate RDB snapshots regularly, and the RDB restores the data set faster than AOF.
  2. However, if you only use the RDB mechanism, you may lose a lot of data, so we need to use AOF and RDB two persistence mechanisms, use AOF to ensure that data is not lost as the first choice for data recovery. ; Use RDB to do different degrees of cold backup, in the case of AOF files are lost or damaged or unavailable, you can use RDB for fast data recovery
  3. We can use RDB to quickly restore data, and use AOF to complete the data

Here we have described the Redis persistence mechanism. Through the study of this article, I believe that when I encounter this problem in the interview, I will not be so at a loss~!

Don't talk about it, don't be lazy, and be a X as an architecture with Xiaocai~ Follow me to be a companion, so that Xiaocai is no longer alone. See you below!

看完不赞,都是坏蛋

If you work harder today, you will be able to say less begging words tomorrow!
I am Xiaocai, a man who becomes stronger with you. 💋
The WeChat public account has been opened, , students who have not followed please remember to pay attention!

写做
624 声望1.7k 粉丝