Redis - 8. Persistent RDB (Redis DataBase)

1. General introduction

Redis is a memory-based database. Its data is stored in memory. One problem with memory is that it will be lost when the service is shut down or the power is turned off.

Redis data also supports writing to the hard disk. This process is called persistence.

Redis provides two different forms of persistence

  • RDB(Redis DataBase)
  • AOP(Append Of File)

2、RDB(Redis DataBase)

2.1. What is RDB?

Write the snapshot of the data set in the memory to the disk within the specified time interval, that is, the Snapshot snapshot in jargon. When it is restored, the key snapshot file is directly read into the memory.

2.2. How the backup is performed

Redis will create (fork) a child process separately for persistence, and will first write the data into a temporary file, and after the persistence process is over, then use this temporary file to replace the last persisted file. During the whole process, the main process does not perform any IO operations, which ensures extremely high performance. If large-scale recovery is required and the integrity of data recovery is not very sensitive, then the RDB
method more efficient. The disadvantage of RDB is that the data after the last persistence may be lost.

2.3、Fork

  • The function of fork is to copy a process that is the same as the current process. All the data (variables, environment variables, program counters, etc.) of the new process are the same as 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. Considering efficiency, Linux introduces "copy-on-write technology"
  • Generally, the parent process and the child process will share a piece of physical memory. Only when the content of each section of the process space changes, will the content of the parent process be copied to the child process.

2.4, RDB persistence process

2.5. Specify the name of the backup file

In redis.conf, you can modify the name of the rdb backup file, the default is dump.rdb, as follows:

2.6. Specify the directory where the backup file is stored

In redis.conf, the saved directory of the rdb file can be modified, and the default is the directory where the Redis startup command is located, as follows

2.7. Trigger RDB backup

2.7.1. Method 1: Automatic backup, backup rules need to be configured

Automatic backup rules can be configured in redis.conf, the default rules are as follows:

save is used to configure backup rules

The format of save: the number of write operations in save seconds

The default is 10,000 revisions within 1 minute, or 10 revisions within 5 minutes, or 1 revision within 30 minutes.

Example: If there are at least 3 key changes within 20 seconds, backup will be performed

save 20 3

2.7.2, Method 2: Manually execute command backup (save | bgsave)

There are 2 commands to trigger the backup.

save: Just save when saving, and ignore other things, all blocked, manual saving, not recommended.

bgsave: Redis will perform snapshot operations asynchronously in the background, and snapshots can also respond to client conditions.

You can use the lastsave command to get the time of the last successful snapshot.

2.7.3. Mode 3: flushall command

Executing the flushall command will also generate a dump.rdb file, but it is empty and meaningless.

2.8, redis.conf some other configurations

2.8.1, stop-writes-on-bgsave-error: When the disk is full, whether to close the write operation of redis

stop-writes-on-bgsave-error is used to specify whether to directly turn off the write operation of redis when redis cannot be written to the disk, yes is recommended.

2.8.2, rdbcompression: Whether to enable compression for rdb backup

For rdb snapshot files stored on the disk, you can set whether to compress them, and if so, redis will use the LZF algorithm to compress them.
If you don't want a small CPU to perform compression, you can set it to disable this function. Yes is recommended.

2.8.3, rdbchecksum: whether to check the integrity of the rdb backup file

After storing the snapshot, you can also let redis use the CRC64 algorithm for data verification, but this will increase the performance consumption by about 10%. If you want to obtain the maximum performance improvement, you can turn off this function.
recommend yes.

2.9, rdb backup and recovery

2.9.1. First query the directory of the rdb file through config get dir

2.9.2, then copy the rdb backup file *.rdb file to another place

cp dump.rdb dump2.rdb

2.9.3. Recovery of rdb

  • close redis
  • First copy the backup file to the working directory cp dump2.rdb dump.rdb
  • Start redis, the backup data is directly loaded, and the data is restored

2.10. Advantages 

  • Suitable for large-scale data recovery
  • It is more suitable to use if the requirements for data integrity and consistency are not high
  • save disk space
  • fast recovery

2.11 Disadvantages

  • When forking, the data in the memory will be cloned, roughly double the expansion, which needs to be considered
  • Although Redis uses copy-on-write technology during fork, it still consumes performance if the data is huge
  • Make a backup at a certain interval during the backup cycle, so if Redis goes down unexpectedly, all modifications after the last snapshot will be lost

2.12. How to stop RDB?

Dynamically stop RDB: redis-cli config set save "" #save and give a null value, indicating that the save strategy is disabled.

Guess you like

Origin blog.csdn.net/qq_34272760/article/details/125393766