Redis persistence of RDB and AOF Detailed

A, RDB Detailed

RDB is the default persistence Redis program. Within a specified time interval, to perform a specified number of write operations, it will be written into the data memory to the disk. That dump.rdb generate a file in the specified directory. Redis reboot will restore the data files by loading dump.rdb.

Learn from the configuration file RDB

Open redis.conf file, find the corresponding content SNAPSHOTTING
1 RDB core rules configuration (Key)

save <seconds> <changes>
# save ""
save 900 1
save 300 10
save 60 10000

Explain: save <specified time intervals> <perform a specified number of update>, satisfying the condition data memory to the hard drive will be synchronized. Official factory default configuration is a change within 900 seconds, 300 seconds there are 10 changes, and there are 10,000 change within 60 seconds, the data will be written to disk memory snapshot.
If not want to use RDB program, you can save "" comment open, the following three comments.

2 Specify local database file name, generally use the default dump.rdb

dbfilename dump.rdb

3 Specify local database storage directory, generally use the default configuration

to you ./

4 data compression enabled by default

rdbcompression yes

Explanation: When you configure whether to compress data stored in the local database, the default is yes. Redis using LZF compression, but takes up little CPU time. If this option is turned off, but will cause the database files become huge. It recommended that you turn.

RDB trigger snapshots

1 within the specified time interval, to perform a specified number of write operations
2 Do save (blocked, just save a snapshot, others wait) or bgsave (asynchronous) command
3 command execution flushall, empty database for all data, of little significance.
4 Run the shutdown command to ensure normal server shut down without losing any data, meaning ... not great.

RDB data file recovery

Dump.rdb copy the file to the bin directory of the installation directory redis, redis restart the service. In the actual development, it will generally consider the physical hard disk damage, select the backup dump.rdb. Experience can be from the following presentation to the operation.

RDB advantages and disadvantages

Advantages:
1 for large-scale data recovery.
2 If the business of data integrity and consistency do not ask, RDB is a good choice.

Disadvantages:
1 data integrity and consistency is not high, because RDB possible downtime when the last backup in the end.
2 backup memory for the time, because Redis will create a separate child process at the time of the backup, the data is written to a temporary file (this time the data in memory is twice the original oh), before the last backup and then replace the temporary file file.
So Redis persistence and recovery of data to be selected to perform in the dead of night is more reasonable.

RDB trigger snapshots

1 within the specified time interval, to perform a specified number of write operations
2 Do save (blocked, just save a snapshot, others wait) or bgsave (asynchronous) command
3 command execution flushall, empty database for all data, of little significance.
4 Run the shutdown command to ensure normal server shut down without losing any data, meaning ... not great.

RDB data file recovery

Dump.rdb copy the file to the bin directory of the installation directory redis, redis restart the service. In the actual development, it will generally consider the physical hard disk damage, select the backup dump.rdb. Experience can be from the following presentation to the operation.

RDB advantages and disadvantages

Advantages:
1 for large-scale data recovery.
2 If the business of data integrity and consistency do not ask, RDB is a good choice.

Disadvantages:
1 data integrity and consistency is not high, because RDB possible downtime when the last backup in the end.
2 backup memory for the time, because Redis will create a separate child process at the time of the backup, the data is written to a temporary file (this time the data in memory is twice the original oh), before the last backup and then replace the temporary file file.
So Redis persistence and recovery of data to be selected to perform in the dead of night is more reasonable.

Operation and demonstration

[root@itdragon bin]# vim redis.conf
save 900 1
save 120 5
save 60 10000
[root@itdragon bin]# ./redis-server redis.conf
[root@itdragon bin]# ./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> set key2 value2
OK
127.0.0.1:6379> set key3 value3
OK
127.0.0.1:6379> set key4 value4
OK
127.0.0.1:6379> set key5 value5
OK
127.0.0.1:6379> set key6 value6
OK
127.0.0.1:6379> SHUTDOWN
not connected> QUIT
[root@itdragon bin]# cp dump.rdb dump_bk.rdb
[root@itdragon bin]# ./redis-server redis.conf
[root@itdragon bin]# ./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> FLUSHALL 
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> SHUTDOWN
not connected> QUIT
[root@itdragon bin]# cp dump_bk.rdb  dump.rdb
cp: overwrite `dump.rdb'? y
[root@itdragon bin]# ./redis-server redis.conf
[root@itdragon bin]# ./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> keys *
1) "key5"
2) "key1"
3) "key3"
4) "key4"
5) "key6"
6) "key2"

Step: vim modifications persistent configuration time, 120 seconds, to modify the 5 persistence time.
Step two: Restart the service configuration to take effect.
The third step: each set 5 Ge key, after two minutes, it will automatically produce a dump.rdb file in the current directory of the bin. (Set key6 role is to verify the shutdown triggered RDB snapshot)
Step 4: The current dump.rdb backup copy (analog line of work).
Step five: Clear command execution FLUSHALL database data (analog data loss).
Step six: Restart Redis service, data recovery ..... Huh? ? ? ? ( '◔ ‸◔`). Data is empty? ? ? ? This is because FLUSHALL also functions as a trigger RDB snapshot.
Step Seven: The backup dump_bk.rdb replace dump.rdb then re Redis.

Precautions: SHUTDOWN command and FLUSHALL RDB will trigger a snapshot, which is a pit, please pay attention.

Other commands:

  • keys * matching all key database
  • save RDB blocking trigger snapshot, so back up your data
  • FLUSHALL empty Redis server's entire data (almost no)
  • SHUTDOWN Shutdown leave (rarely used)

 To be continued ......

 

 

Guess you like

Origin www.cnblogs.com/wangjian941118/p/11294762.html