Principles of Redis Persistence:
Redis supports two types of persistence:RDB and AOF mode
First, the noun explanation:
RDB: persistence can generate a point-in-time snapshot of a dataset (Point-in-time snapshot) within a specified time interval.
AOF: persists all write commands performed by the server and restores the dataset when the server is started by re-executing the commands.
The commands in the AOF file are all saved in the Redis protocol format, and the new command is appended to the end of the file. Redis can also override the AOF file in the background (rewrite)
The volume of the AOF file does not exceed the actual size required to save the state of the dataset.
priority for PDB and AOF :
if both the RDB and AOF modes are turned on, the AOF priority is higher than the RDB :
Redis can also use both AOF persistence and RDB persistence. In this case, when the Redis restarts, it takes precedence over the AOF file to restore the dataset.
Because the data set saved by the AOF file is usually more complete than the data set saved by the RDB file.
AOF's way is a bit like orcal's logic reserve!
AOF Redis will also be in the background to rewrite the data, such as set Key1, set Key2, in fact, set key1 useless, so you can delete the set Key1. This saves the data set is very small can be compressed!
You can even turn off the persistence feature so that the data exists only when the server is running.
Ii. advantages and disadvantages of rdb&aof
Advantages and disadvantages of the RDB:
Advantages:
1, compact and easy to back up, he just a file.
2, the RDB can maximize the performance of Redis, the parent process does not need to do anything but only for a child process
3, recovery than aof block
Disadvantages:
1. Data integrity: If you pay attention to the integrity of the data, then the RDB will not work, although he is a point-in-time snapshot, but in the process of the snapshot, Redis restarts, then the data in the snapshot will be lost
2, the data is very large, very CPU and time, then Redis may be down for 1 seconds to set longer.
Advantages and disadvantages of aof:
Advantages:
1, using AOF persistence will make Redis become very durable,AOF the default of every second, you can modify his way not to execute a command to append once, so you lose up to 1 seconds of data
2. AOF file is a log file (append only) for append operation only
3. Redis can automatically rewrite the AOF in the background when the AOF file size becomes too large
Disadvantages:
1, for the same data set, the volume of the AOF file is usually larger than the size of the RDB file.
2. Depending on the Fsync strategy used, the AOF speed may be slower than the RDB
RDIs Persistence settings:
The default redis is the persistence of the open Rdb mode as seen in the following configuration file:
vim/etc/redis/6379.conf=============================================================########################## ###### snapshotting ################################## Save the DB on disk:## save <seconds> <changes>## Would save the DB if both the given number of seconds and the given# number of write operations against the DB occurred.# # in the example below the behaviour would be is to save:# after the SEC (min) If at least 1 key changed# after s EC (5 min) If at least changed# after the SEC if at least 10000 keys changed## note:you can disable saving com Pletely by commenting-all "save" lines.## It's also possible to remove all the previously configured save# points By adding a save directive with a single empty string argument# like in the following example:## save "" Save 1sav E 10save 60 10000================================================================ above 3 save is or the relationship # save <seconds > <changes> # #格式! Explanation: # AFter/sec (min) If at least 1 key changed# after SEC (5 min.) If at least, changed# after ten sec if at Least 10000 keys changed900 sec has 1 key changed to do a snapshot or 300sec within 10 keys have changed to do a snapshot or in the SEC 10000 keys changed to do a snapshot snapshot principle: fork Er a process that is a copy of the current process is equivalent to a child process and does not affect the process you are currently running. When the child process is written, there will be a temporary file, when the sub-process will be finished after the temporary file move to replace the old file, so the Rdb file any time is a full available copy! You do not affect the RDB when you write the file, because the forker out of the child process is writing a temporary file! But if it fails, you save the time is the moment you start the snapshot that time, your snapshot to the end of the period of time the data is lost! If you want to disable persistence, delete these three lines. Save 1save 10save 60 10000
1. Where are the snapshots stored?
# The filename where to dump the Dbdbfilename Dump.rdb #如果你启用了多个快照名称, can be used to define ports well such as: dump_6379.rdb# Note that you must SP Ecify a directory here, not a file name.dir./ #不仅仅是RDB模式下的DB存放在这个目录AOF模式下也是存放在这个目录的, it is recommended to store in your designated place! For example: dir/opt/redis/like I specified above: # The filename where to dump the dbdbfilename dump_6379.rdb# Note so you must specify a Direc Tory, not a file name.dir/opt/redis/
2. Save manually in Redis
127.0.0.1:6379> SET Key 1ok127.0.0.1:6379> Saveok The following directory has not been modified:-rw-r--r--1 root root Oct 13:35 Dump_6379.rdb Current time Create a key to see:127.0.0.1:6379> set key 2ok127.0.0.1:6379> saveok-rw-r--r--1 root root Oct 13:37 dump_6379.rdb12 What is the difference between 7.0.0.1:6379> Bgsavebackground saving Startedsave and Bgsave: Save is blocked when you execute the save directly, he does not work, Bgsave is executed in the background. Forker a child process to perform save! The Save usage scenario is limited to: when Redis needs to be migrated, Redis has no data to write to and can stop when it is used! Test add one: Key then stop and look! Do not save: The current key is:127.0.0.1:6379> KEYS * *) "key" 2) "Key2" 3) "Key3" 127.0.0.1:6379> set Key4 4OK killed, restarted after the discovery of the set key is missing. #所以当redis异常挂掉之后, no save receipts!
3, after the AOF is enabled
Append to this file, write all the commands into a file, and you execute one I write one. If you do it again, you can do it again. Very simple (but recovering relative to the RDB mode slows him down by re-aof the records in the library into memory) and can be used by both RDB and AOF! The pros are all occupied! But also according to the business to decide! Open method: Modify the configuration file appendonly Yes #改为yesappendfilename "appendonly.aof" #文件名工作原理: Forker A child process to a temporary file, After writing, send a signal to the parent process, start writing to the completion of the process will also have a child process to signal the parent process. Save in memory first but he has a good function, rewrite, he will periodically aof, so the file will be very small! Test: (he writes files according to Redis's recognizable form, but probably people can read them) 16:50 [[Email protected]]$ cat appendonly.aof *2$6select$10*3$3set$4kye1
Redis-cluster Cluster "Second article": Redis persistence