Redis is an in- memory database . If the state of the database in memory is not saved to disk , once the server process exits , the state of the database in the server will also disappear . So Redis provides persistence function!
RDB(Redis DataBase)
Write the snapshot of the data set in the memory to the disk within the specified time interval, which is the Snapshot in the jargon, and it reads the snapshot file directly into the memory when it is restored.
Redis will separately create (fork) a child process for persistence, and will first write the data to a temporary file. After the persistence process is over, this temporary file will replace the last persisted file. During the whole process, the main process does not perform any I0 operations. This ensures extremely high performance. If large-scale data recovery is required, and the integrity of data recovery is not very sensitive, the RDB method is more efficient than the AOF method. The disadvantage of RDB is that the data after the last persistence may be lost.
1 time in 900 seconds
300 seconds 10 times
1W times a minute
save 60 5 Mainly, if we modify it 5 times within 60s, it will trigger the generation of dump.rdb file
Trigger mechanism
- When the save rule is met, the rdb rule will be triggered automatically
- Execute the fluxhall command, which triggers our rdb rule
- Exit redis, it will also generate a rdb file
The backup will automatically generate a dump.rdb
How to recover rdb files
-
Just put the rdb file in our redis startup directory. When redis starts, it will automatically check dump.rdb to restore the data!
-
See where it needs to be
127.0.0.1:6379> config get dir 1) "dir" 2) "/usr/local/bin"# 如果在这个目录下存在 dump. rdb文件,启动就会自动恢复其中的数据
advantage:
- Suitable for large-scale data recovery, dump.rdb
- The integrity of the data is not high
Disadvantages:
- It takes a certain time interval for process operations. If redis crashes unexpectedly, the last record will be gone
- When forking the process, it will occupy a certain amount of memory space
AOF(Append Only File)
Record all our commands, history, and execute them again when you restore
Record each write operation in the form of a log, record all the instructions executed by Redis (read operations are not recorded), only append files but not rewrite files, redis will read the file to rebuild the data at the beginning of startup. In other words, if redis restarts, it will execute the write command from front to back according to the content of the log file to complete the data recovery work.
AOF saves appendonly.aof file
- Not open by default, manually open, restart to take effect
- You can choose the name of the generated file
Write once every second
[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-BGqKq4Um-1600050464516) (C:\Users\黄良帅\AppData\Roaming\Typora\typora-user-images\ 1600046440315.png)]
What if appendonly.aof goes wrong?
[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-lx6q9koM-1600050464520) (C:\Users\黄良帅\AppData\Roaming\Typora\typora-user-images\ 1600046729742.png)]
Can not start our Redis
Solution
Use our Redis-check-aof --fixe
[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-GW7CvYTC-1600050464523) (C:\Users\黄良帅\AppData\Roaming\Typora\typora-user-images\ 1600046658253.png)]
If our aof is greater than 64M, we will fork a new process to rewrite our file
advantage:
- Every modification is synchronized, the integrity of the file will be better
- The default is to synchronize once per second
- Never synchronize, efficiency is the highest
Disadvantages:
- Compared with data files, AOF is much larger than RDB memory, and the repair speed is slower than RDB
- Aof runs slower than RDB, so the default configuration of redis is our RDB persistence