Home  >  Article  >  Database  >  Example analysis of RDB persistence in Redis

Example analysis of RDB persistence in Redis

PHPz
PHPzforward
2023-05-28 18:11:17984browse

1. Introduction to RDB

RDB is a method used by Redis for persistence. It writes a snapshot of the data set in the current memory to the disk, that is, a Snapshot snapshot (all key-value pairs in the database). data). During recovery, the snapshot file is read directly into memory.

2. Triggering method

RDB has two triggering methods, namely automatic triggering and manual triggering.

①. Automatic triggering

Under SNAPSHOTTING in the redis.conf configuration file, we have introduced it in this article.

 Example analysis of RDB persistence in Redis

 ①.save:This is used to configure the RDB persistence conditions that trigger Redis, that is, when to save the data in the memory to the hard disk. . For example "save m n". Indicates that when the data set has been modified n times within m seconds, bgsave is automatically triggered (this command will be introduced below, and the command to manually trigger RDB persistence)

The default configuration is as follows:

save 900 1:表示900 秒内如果至少有 1 个 key 的值变化,则保存
save 300 10:表示300 秒内如果至少有 10 个 key 的值变化,则保存
save 60 10000:表示60 秒内如果至少有 10000 个 key 的值变化,则保存

Of course, if you If you are just using the caching function of Redis and do not need persistence, you can comment out all save lines to disable the saving function. You can use save "" to disable

 ②, stop-writes-on-bgsave-error: The default value is yes. When RDB is enabled and the last background save of data fails, whether Redis stops receiving data. This would make the user aware that the data was not persisted to disk correctly, otherwise no one would notice that a disaster had occurred. If Redis restarts, it can start receiving data again

 ③, rdbcompression;The default value is yes. For snapshots stored on disk, you can set whether to compress them for storage. If so, redis will use the LZF algorithm for compression. If you don't want to consume CPU for compression, you can set this feature to off, but the snapshots stored on disk will be larger.

 ④, rdbchecksum: The default value is yes. After storing the snapshot, we can also let redis use the CRC64 algorithm for data verification, but this will increase performance consumption by about 10%. If you want to get the maximum performance improvement, you can turn off this function.

 ⑤、dbfilename:Set the file name of the snapshot, the default is dump.rdb

 ⑥、dir:Set the storage path of the snapshot file, This configuration item must be a directory, not a file name. The default is to save it in the same directory as the current configuration file.

 That is to say, through the save method configured in the configuration file, when the actual operation meets the configuration form, RDB persistence will be performed, and the current memory snapshot will be saved in the directory configured by dir. The file name is Determined by the configured dbfilename.

②. Manual trigger

There are two commands to manually trigger Redis for RDB persistence:

1. save

This command will block the current Redis Server, during the execution of the save command, Redis cannot process other commands until the RDB process is completed.

Obviously, this command will cause long-term blocking for instances with relatively large memory. This is a fatal flaw. In order to solve this problem, Redis provides a second way.

 2. bgsave

When executing this command, Redis will perform snapshot operations asynchronously in the background, and the snapshot can also respond to client requests. When the Redis process performs a fork operation to create a child process, the child process will be responsible for executing the RDB persistence process and automatically terminate after completion. Blocking only occurs during the fork phase and is generally very short-lived.

 Basically all RDB operations inside Redis use the bgsave command.

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

3. Restore data

Move the backup file (dump.rdb) to the redis installation directory and start the service. Redis will automatically load the file data into memory. The Redis server will block during loading of the RDB file until the loading work is completed.

To obtain the installation directory of redis, you can use the config get dir command

 Example analysis of RDB persistence in Redis

4. Stop RDB persistence

In some cases, we only If you want to take advantage of the caching function of Redis, it is not like using the persistence function of Redis, so we'd better stop RDB persistence at this time. You can disable the save function by commenting out all the save lines in the configuration file redis.conf as mentioned above, or by directly using an empty string to disable it: save ""

You can also pass the command:

1

redis-cli config set save " "

5. Advantages and Disadvantages of RDB

 ①、Advantages

The Redis data set is saved in an RDB file at a specific point in time. This file is very compact. This file is ideal for backup and disaster recovery.

 2. When generating an RDB file, the redis main process will fork() a child process to handle all saving work. The main process does not need to perform any disk IO operations.

 3.RDB is faster than AOF when restoring large data sets.

 ②、Disadvantages

 1. RDB data cannot achieve real-time persistence/second-level persistence. Because every time bgsave runs, it must perform a fork operation to create a child process, which is a heavyweight operation (the data in the memory is cloned, and roughly 2 times the expansion needs to be considered). Frequent execution costs are too high (affecting performance)

 2. RDB files are saved in a specific binary format. During the evolution of the Redis version, there are multiple formats of RDB versions. There is a problem that the old version of the Redis service is not compatible with the new version of the RDB format (version incompatibility)

3. Make a backup at a certain interval, so if redis accidentally goes down, all modifications after the last snapshot will be lost (data will be lost)

6. The principle of RDB automatic saving

Redis has a server status structure:

1

2

3

4

5

6

7

8

9

struct redisService{

struct saveparam *saveparams;

long long dirty;

       time_t lastsave;

}

 ① First, look at the saveparam array that records the save conditions. Each element in it is A saveparams structure:

1

2

3

4

5

6

struct saveparam{

        time_t seconds;

       int changes;

};

Previously we configured save in the redis.conf configuration file:

1

2

3

save 900 1: Indicates if there is at least 1 key within 900 seconds If the value changes, save it

save 300 10: Indicates that if the value of at least 10 keys changes within 300 seconds, save it

save 60 10000: Indicates that if at least 10000 key values ​​change within 60 seconds, save

Then the saveparam array in the server status will be It looks like this:

 Example analysis of RDB persistence in Redis

 ②、Dirty counter and lastsave attribute

The dirty counter records the distance since the last successful execution of the save command or bgsave command. The Redis server performs How many times has it been modified (including writing, deleting, updating, etc.).

The lastsave attribute is a timestamp that records the last time the save command or bgsave command was successfully executed.

Through these two commands, when the server successfully performs a modification operation, the dirty counter will be incremented by 1, and the lastsave attribute records the time when save or bgsave was last executed. The Redis server also has a periodic operation function. severCron is executed every 100 milliseconds by default. This function will traverse and check all save conditions in the saveparams array. As long as one condition is met, the bgsave command will be executed.

After the execution is completed, the dirty counter is updated to 0, and lastsave is also updated to the completion time of the executed command.

The above is the detailed content of Example analysis of RDB persistence in Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete