Redis (7) ------ Redis persistence

Redis (7) ------ Redis persistence

12, Sustainability

  • In interviews and at work, persistence is key
  • Redis is an in-memory database. If the database state in memory is not saved to disk, once the server process exits, the database state in the server will also disappear. All Redis provides persistence capabilities

12.1 RDB(Redis DataBase)

12.1.1 What is RDB

Insert image description here

  • Snapshot: writes an in-memory snapshot of a dataset to disk at a specified interval

  • When it is restored, the snapshot file is read directly into the memory.

  • Redis will create (fork) a separate sub-process for persistence. It will first write the data to a temporary file. After the persistence process is completed, this temporary file will be used to replace the last persisted file. During the entire process, the main process does not perform any IO 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 if it goes down after the last persistence, the data may be lost.

  • Our default is RDB. Generally, there is no need to modify this configuration.

  • Sometimes in the production environment we will back up this file

  • The file saved by rdb is:dump.rdb

  • Configure in a snapshot of the configuration file

Insert image description here

  • Modification save 60 5: As long as the key is modified 5 times within 60 seconds, the RDB operation will be triggered.
12.1.2 Trigger mechanism
  • 1. saveWhen the rules are met, the rdb rules will be automatically triggered.
  • 2. Executing flushallthe command will also trigger rdb rules
  • 3. Exit redis, and rdb files will also be generated.
12.1.3 Restore rdb files
  • Just put the rdb file in our redis startup directory. When redis starts, it will automatically check dump.rdband restore the data.

  • See where it needs to be

127.0.0.1:6379> config get dir
1) "dir"
# 如果在这个目录下存在 dump.rdb文件,启动就会自动恢复其中的数据
2) "/usr/local/bin" 
12.1.4 Advantages and Disadvantages
  • advantage:
    • Suitable for large-scale data recovery
    • Low requirements for data integrity
  • shortcoming:
    • It requires a certain time interval to operate. If redis crashes unexpectedly, the last modified data will be gone.
    • When forking a process, it will occupy a certain amount of content space.

12.2 AOF(Append Only File)

12.2.1 What is AOF

Insert image description here

  • Record each write operation in the form of a log and record all instructions executed by Redis (read operations are not recorded). Only files can be appended but not rewritten. Redis will read the file and reconstruct the data when it starts. In other words, if Redis is restarted, 根据日志文件的内容将写指令从前到后执行一次the data recovery work will be completed.

  • AOF save file:appendonly.aof

12.2.2 APPEND

Insert image description here

  • The snapshot function (RDB) is not very durable: if Redis fails for some reason, the server will lose data that was recently written and not saved in the snapshot. Starting from version 1.1, Redis adds a fully durable persistence method:AOF持久化

  • If you want to use AOF, you need to modify the configuration file, appendonly yes, which means enabling AOF. After configuration, restart Redis to take effect.

  • Test: Create 5 new key-value pairs k1 v1...

  • View appendonly.aoffiles

*2
$6
SELECT
$1
0
*3
$3
set
$2
k1
$2
v1
*3
$3
set
$2
k2
$2
v2
*3
$3
set
$2
k3
$2
v3
*3
$3
set
$2
k4
$2
v4
*3
$3
set
$2
k5
$2
v5
  • Manually write some garbled characters, and then open the redis service, the connection will fail.
  • If there is an error in the AOF file, Redis cannot start and the aof file needs to be modified.
12.2.3 redis-check-aof
  • Redis provides a tool:redis-check-aof
  • input the command:./redis-check-aof --fix appendonly.aof
  • At this time, I checked the aof file again and found that the garbled characters we entered before were deleted. The AOF file is normal and the Redis service can be restarted.
12.2.4 Rewrite rule description

Insert image description here

  • AOF's default file is infinitely appended, and the file will become larger and larger. If the AOF file is larger than 64M, a process will be copied to rewrite the file.
12.2.5 Advantages and Disadvantages
  • advantage:

    • Every modification will be synchronized, and the integrity of the file will be better
    • Sync every second, 1 second of data may be lost
    • Never synchronize, the efficiency is the highest
  • shortcoming

    • Compared with data files, AOF files are much larger than RDB files, and the repair speed is also slower than RDB.
    • AOF is also slower than RDB, so the default configuration is RDB persistence.

12.3 Comparison between RDB and AOF

advantage RDB AOF
Startup priority Low high
volume Small big
recovery speed quick slow
Data security Will lose data Determine based on strategy
  • Generally speaking. To achieve data security comparable to PostgreSQL, both persistence functions should be enabled at the same time.
  • If you really care about your data but can afford data loss within minutes, you can just use RDB persistence
  • Many users only use AOF persistence, which is not recommended because regularly generating RDB snapshots is very convenient for database backup, and RDB restores data sets faster than AOF restores.

Guess you like

Origin blog.csdn.net/weixin_44176393/article/details/123708533