[Rpm] of the Redis persistence RDB & AOF

Transfer: https://www.cnblogs.com/itdragon/p/7906481.html

 

There are two Redis persistence solution, RDB (Redis DataBase) and AOF (Append Only File). If you want to quickly understand and use RDB and AOF, you can jump directly to the bottom of the article to see the summary. This section through the configuration file, trigger snapshots, data recovery operations, command operations demonstrate the advantages and disadvantages to focus knowledge learning Redis persistence .

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.

1. Understand the RDB from the configuration file

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

Commentary: save <specified time intervals> <perform update operation specified number>, the condition will synchronize data in memory to the hard disk. 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 the local database file name, generally use the default dump.rdb

dbfilename dump.rdb

3) specify the local database storage directory, generally use the default configuration

the dir ./ # or modified to / var / lib / redis /

4) data compression enabled by default

rdbcompression yes

Commentary: whether to compress data when the configuration is stored to 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. Recommended that you turn.

2. Trigger RDB Snapshot

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

3. Restore data files by RDB

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.

4.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) when the memory for backup, 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 memory is twice the original oh), before the final and then replace the temporary files backup file.
So Redis persistence and recovery of data to be selected to perform in the dead of night is more reasonable.

 

5. Operation demo

[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)

 

Two, AOF Detailed

AOF: Redis is not turned on by default. It appears to make up for the lack of RDB (data inconsistency), so it takes the form of a log to record each write operation , and append to the file. Redis will restart the write command based on the contents of the log file from front to back in order to perform a complete recovery of data.

1. Understand the AOF from the configuration file

Open redis.conf file, find the corresponding content APPEND ONLY MODE
1) redis off by default, need to manually open the yes to no

appendonly yes

2) specify the local database file name, the default value appendonly.aof

appendfilename "appendonly.aof"

3) specify the update log criteria

# appendfsync always
appendfsync everysec
# appendfsync no

Commentary:
Always: synchronization persistence, data changes are written to disk immediately each occurrence. Poor performance when data integrity is better (slow, safe)
everysec: The factory default recommendation, asynchronous recorded once per second (default value)
NO: are not synchronized

4) Configuring Rewrite the trigger mechanism

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

Commentary: When AOF is triggered when the file size is greater than 64M doubled after the last rewrite and file size. Generally set to 3G, 64M too small.

2. Trigger AOF Snapshot

Depending on the configuration file is triggered, can be executed each time the trigger can be triggered per second, you may not be synchronized.

3. Restore data from the AOF file

Under normal circumstances, appendonly.aof copy the file to the bin directory of the installation directory redis, redis restart the service. But in the actual development, probably because of some reason appendonly.aof file format anomalies, resulting in data restore failure, can be repaired by the command redis-check-aof --fix appendonly.aof. Experience from the following operations presentation.

4.AOF rewriting mechanism

Speaking of the front also, works AOF is to write to append to the file, the contents of the file redundancy will be more and more. So clever Redis added rewriting mechanism. When the size of the file AOF exceeds the set threshold value, Redis AOF compresses the content of the file.

Rewrite principle: Redis will fork out a new process to read the data in memory, and re-written to a temporary file. Did not read the old files (you are so big, I also read you ??? o (゚ Д ゚) っ stupid ah!). Finally, replace the old aof file.

Trigger mechanism: When AOF is triggered when the file size is greater than 64M doubled after the last rewrite and file size. Here the "doubled" and "64M" can be modified through a configuration file.

5.AOF advantages and disadvantages

Pros: higher data integrity and consistency
drawback: because the contents of AOF record of more documents will become increasingly large, data recovery will be slower and slower.

6. Operation demo

[root@itdragon bin]# vim appendonly.aof
appendonly yes
[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 keyAOf valueAof
OK
127.0.0.1:6379> FLUSHALL 
OK
127.0.0.1:6379> SHUTDOWN
not connected> QUIT
[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) "keyAOf"
127.0.0.1:6379> SHUTDOWN
not connected> QUIT
[root@itdragon bin]# vim appendonly.aof
fjewofjwojfoewifjowejfwf
[root@itdragon bin]# ./redis-server redis.conf
[root@itdragon bin]# ./redis-cli -h 127.0.0.1 -p 6379
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> QUIT
[root@itdragon bin]# redis-check-aof --fix appendonly.aof 
'x              3e: Expected prefix '*', got: '
AOF analyzed: size=92, ok_up_to=62, diff=30
This will shrink the AOF from 92 bytes, with 30 bytes, to 62 bytes
Continue? [y/N]: y
Successfully truncated AOF
[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) "keyAOf"

The first step: modify the configuration file, open the AOF persistence configuration.
Step two: Restart Redis service, and comes into the Redis client.
Step three: Save the value, and then simulate loss of data, close the Redis service.
Step four: Restart the service and found the data restored. (Additional mention one thing: there are tutorials show FLUSHALL command is written AOF files, resulting in data recovery failed I installed the redis-4.0.2 did not encounter this problem.).
Step Five: Modify appendonly.aof, simulation file exceptions.
Step Six: Redis service fails to restart. This also explains, RDB and AOF can exist simultaneously, and the priority load AOF file.
Step Seven: check appendonly.aof file. Normal restart Redis service.

Supplementary points: aof check by redis-check-aof file, then the check is not possible rdb file by redis-check-rdb it? ? ?

Third, the summary

  1. Redis is enabled by default RDB persistent way, within a specified time interval, to perform a specified number of write operations, the data will be written to memory to the disk.
  2. RDB persistence for large-scale data recovery but its poor data integrity and consistency.
  3. Redis need to manually open the AOF persistent mode, the default is second to write the log file is appended to the AOF.
  4. AOF data integrity than RDB high, but much recorded content will affect the efficiency of data recovery.
  5. Redis for large AOF document problems and provide weight-loss mechanism rewritten.
  6. If only intend to use caching Redis, you can turn off persistence.
  7. If you plan to use Redis persistence. RDB recommendations and AOF are open. In fact, RDB is more suitable for data backup, leave a way of escape. AOF a problem, as well as RDB.

Here Redis persistence on the introduction is over, there is something wrong may be noted.

Guess you like

Origin www.cnblogs.com/leokale-zz/p/12156800.html