Redis persistent RDB

Bilibili video tutorial: https://www.bilibili.com/video/BV1Rv41177Af?p=28&spm_id_from=pageDriver

What is RDB

RDB is a persistence technology of redis. It 时间间隔writes the snapshot of the data set in the memory to the disk in the specified memory, and reads the snapshot file in the disk directly into the redis memory when restoring.

dump.rdb file

This file is the file persisted by redis using RDB technology. The file name can be changed in the redis.conf configuration file. The default is dump.rdb.

Insert picture description here

Configure the default path of dump.rdb in the configuration file

Open redis.conf, find dir ./, and add the file location you want to save after dir.

Insert picture description here

Backup and restore of dump.rdb file

  1. Backup
    First use the config get dir command in the redis client to find the location of your dump.rdb file, and then use the cp command to exit the client to copy.
  2. Recovery
    First shut down the redis service, and then directly copy the dump.rdb file you backed up to the directory where dump.rdb is configured in the redis.conf configuration file, and start the redis service.

Trigger RDB persistence

In the redis.conf configuration file, the

Insert picture description here

configuration item of the trigger condition for configuring RDB persistence is save + the specified time (in seconds) + how many pieces of data have changed (changes refer to additions, deletions, and modifications)

Use commands for persistence
save :save时只管保存,其它不管,全部阻塞。手动保存。不建议。
bgsave:Redis会在后台异步进行快照操作, 快照同时还可以响应客户端请求。

How RDB is persisted

Rough process

Redis will create a child process named fork for persistence, he will persist the data 先写到一个临时文件中, and wait until 持久化结束后he can use this, 临时文件替换掉上次持久化好了的文件which is what is mentioned above dump.rdb文件. During the whole process, the main process does not perform any IO operations, which ensures extremely high performance if large-scale data recovery is required.

Insert picture description here

Fork

  • The role of Fork is to copy a process that is the same as the current process. All data (variables, environment variables, program counters, etc.) of the new process have the same values ​​as the original process, but it is a brand new process and is a child process of the original process
  • In a Linux program, fork() will generate a child process that is exactly the same as the parent process, but the child process will often call the exec system afterwards. For efficiency reasons, Linux has introduced " 写时复制技术"
  • In general, the parent process and the child process share the same physical memory. Only when the content of each segment of the process space changes, the content of the parent process will be copied to the child process.

Advantages and disadvantages of RDB persistence

Advantage

  • Suitable for large-scale data recovery
  • Low requirements for data integrity and consistency are more suitable for use
  • Save disk space
  • Fast recovery
Insert picture description here

Disadvantage

  • When Fork, the data in the memory is cloned, and roughly 2 times the expansion needs to be considered
  • Although Redis uses copy-on-write technology when it forks, it still consumes performance if the data is huge.
  • Make a backup at a certain interval in the backup cycle, so if Redis accidentally goes down, all the changes after the last snapshot will be lost.

Stop RDB persistence strategy

Stop RDB dynamically: redis-cli config set save “” #save and give a blank value, which means that the save strategy is disabled

to sum up

Insert picture description here