Redis provides different levels of persistence:
The RDB persistence mode enables snapshot storage of data at specified intervals.
The AOF persistence records every action that is written to the server, and when the server restarts, the commands are re-executed to restore the original data.
If the data is not very important, then no persistence action is required
If both persistence methods are used, the AoF file will be loaded first to restore the original data when the Redis restarts. Because the aof file is typically saved in a dataset that is more complete than the data set saved by the Rdb file.
Advantages of an RDB
When the RDB saves the file, it is done by the Redis child process, and the parent process does not need to do any more IO operations
When recovering a large data set, the Rdb method is faster than AOF.
An RDB can be saved based on time and can hold different versions of the data.
Disadvantages of the RDB
An RDB is saved based on different save conditions, so it is possible that the data is not saved in real time if the database is out of date with abnormal downtime
[[email protected] ~]# redis-cli -p 6378127.0.0.1:6378> auth redisok127.0.0.1:6378> config get dbfilename Found in #这些参数文件可以在/etc/redis/6378.conf 1) "Dbfilename" #这个参数表示save时的文件名2) "Dump.rdb" 127.0.0.1:6378> CONFIG GET DIR1) "dir" #这个参数指定了save时的文件位置2) "/var/lib/redis/6378" 127.0.0.1:6378> CONFIG GET SAVE1) "Save" #save参数是触发RDB的关键参数2) "900 1 300 10 60 10000" #表示每900秒如果发生一次数据更改, a backup occurs. #每300秒发生10次数据更改则发生备份. #每60秒发生了10000次数据更改, a backup occurs. 127.0.0.1:6378> quit[[email protected] ~]# ls /var/lib/redis/6378/appendonly.aof dump.rdb[[email protected] ~]# file /var/lib/redis/6378/dump.rdb /var/lib /redis/6378/dump.rdb: data # The Rdb file is a binary file that can be viewed using the strings command [[email protected] ~]# strings /var/lib/redis/6378/ dump.rdbredis0006abcd123 # From here we infer that this is a value [[email protected] ~]# redis-cli -p 6378 127.0.0.1:6378> auth redisok127.0.0.1:6378> set v1 www.oracle.com # Set the value of V1 ok127.0.0.1:6378> get v1 "www.oracle.com" 127.0.0.1:6378> save #save和bgsave都可以出发一次手工RDBOK127 .0.0.1:6378> bgsavebackground saving started127.0.0.1:6378> quit[[email protected] ~]# strings /var/lib/redis/6378/dump.rdbREDIS0006www.oracle.com #这个是刚才我们设置的v1的值abcd123
Advantages of AOF
Persistent data in this way is more accurate and can be set to be recorded every second.
Disadvantages of AoF
The corresponding AOF mode consumes more disk space
Log rewriting
Because AOF works by constantly appending commands to the end of a file, the volume of the aof file becomes larger as the write command grows.
If you call 100 incr on a counter, the AoF file needs to use 100 records just to save the current value of the calculator. In fact, using only one set command is sufficient to hold the current value of the counter, and the remaining 99 records are actually superfluous.
To handle this situation, Redis supports an interesting feature: You can rebuild the AoF file without interrupting the service client (rebuild). To execute the bgrewriteaof command, Redis will generate a new aof file that contains the fewest commands required to rebuild the current dataset. AoF overrides can be triggered automatically after Redis2.4.
[[email protected] ~]# redis-cli-p 6378 127.0.0.1:6378> auth redisok127.0.0.1:6378> config get AP Pendfilename (empty list or set) 127.0.0.1:6378> config get appendonly 1) "AppendOnly" 2) "yes" 127.0.0.1:6378> Config Get appendfsync1) "Appendfsync" #aof的fsync追加方式, there are three kinds of optional, per second, per command, never 2) "Everysec" #这里的是每秒钟fsync
Here are a few examples to test the previous statement
[[email protected] ~]# sed -i ' s/^requirepass/#requirepass/g ' /etc/redis/6378. Conf [[email protected] ~]# redis-cli -p 6378127.0.0.1:6378> auth redisok127.0.0.1:6378> shutdown127.0.0.1:6378> quit[[email protected] ~]# grep ' ^save ' /etc/redis/6378.confsave 900 1save 300 10save 60 10000[[email protected] ~]# sed -i '/^save/s/^/#/g ' /etc/redis/6378.conf #关掉rdb [[email protected] ~]# grep ' ^save ' /etc/redis/6378.conf [[email protected] ~]# grep ' #save ' /etc/redis/6378.conf #save 900 1#save 300 10#save 60 10000[[email protected] ~]# grep ' ^appendonly ' /etc/redis/6378.conf appendonly yes[[email protected] ~]# sed -i '/ ^appendonly/s/yes/no/g ' /etc/redis/6378.conf #关掉AOF [[email protected] ~] # grep ' ^appendonly ' /etc/redis/6378.conf appendonly no[[email protected] ~]# /etc/init.d/redis_6378 startstarting redis server ... [[email protected] ~]# redis-cli -p 6378127.0.0.1:6378> keys * #发现有很多值, this may be because Redis read the Rdb file when it was started. 1) "V1" 2) "Ke1" 3) "S3" 4) "K1" 5) "S1" 6) "S2" 127.0.0.1:6378> flushdb #刷新之后就没有值了OK127.0.0.1:6378> keys * (Empty list or set) [[email protected] ~]# rm -rf /var/lib/redis/6378/* #把这个文件删掉 [[email protected] 6378]# cd /var/lib/redis/6378/[[email protected] 6378]# ls -lrttotal 0[[email protected]  ~]# /ETC/INIT.D/REDIS_6378 RESTARTSTOPPING&NBSP, ..... Redis stoppedstarting redis server ... [[email protected] ~]# redis-cli -p 6378 127.0.0.1:6378> keys * (Empty list or set) 127.0.0.1:6378> set ename kingok127.0.0.1:6378> set sal 5000ok127.0.0.1 :6378> save #手工做一次RDBOK127 .0.0.1:6378> quit[[email Protected] 6378]# ls -lrttotal 4-rw-r--r-- 1 root root 40 oct 25 00:36 dump.rdb[[email protected] ~]# /etc/init.d/redis_6378 RESTARTSTOPPING&NBSP, ..... Redis stoppedstarting redis server ... [[email protected] ~]# redis-cli -p 6378 #如果有RDB文件, and without opening aof, Automatically reads the contents of the Rdb file 127.0.0.1:6378> keys *1) "Sal" 2) "ename" [[email protected] 6378 ]# grep ' ^appendonly ' /etc/redis/6378.conf #打开AOF功能appendonly no[[email protected] 6378]# sed -i '/^appendonly/s/no/yes/g ' /etc/redis/ 6378.conf[[email protected] 6378]# grep ' ^appendonly ' /etc/redis/6378.conf appendonly yes[[email PROTECTED] ~]# /ETC/INIT.D/REDIS_6378 RESTARTSTOPPING&NBSP, ..... Redis stoppedstarting redis server ... [[email protected] ~]# redis-cli -p 6378 127.0.0.1:6378> keys * (Empty list or set) 127.0.0.1:6378> set sal 5000ok127.0.0.1:6378> quit[[email protected] ~]# /etc/init.d/redis_ 6378 RESTART      STOPPING&NBSP, ..... Redis stoppedstarting redis server ... [[email protected] ~]# redis-cli -p 6378 #这里也能说明, if the AOF feature is turned on, Redis will not read the Rdb file. 127.0.0.1:6378> keys *1) "Sal"
This article is from the "long name will be good to remember" blog, please be sure to keep this source http://xiaoyiyi.blog.51cto.com/1351449/1705994
(v) Redis persistence