What is the full name of Redis RDB? Redis RDB memory data persistence operation mode

The full name of RDB isRedis database.

  • As the name suggests, RDB is a Redis database used to store data.
  • Therefore, through RDB persistence, the data stored in the Redis memory is written to the RDB file and saved to the disk to achieve persistence.
  • The feature of Redis is that it can persist data, that is, write data in memory to disk to ensure that no data is lost, and can also load data from disk into memory.

What is the full name of Redis RDB? Redis RDB memory data persistence operation mode

The operations of Redis at the beginning are all based on memory, so the performance is very high, but once the program is closed, the data is lost.

Therefore, we need to write in-memory data to disk at specified intervals, which is Snapshot in jargon.

When restoring, the snapshot file is written directly to memory.

This is also one of the main differences between Redis and Memcached, because Memcached has no persistence capability.

For the persistence of Redis memory data, Redis provides us with the following methods:

  • Snapshot method (RDB, Redis DataBase): write memory data to disk in binary form at a certain moment;
  • Append Only File (AOF, Append Only File), record all operation commands, and append to the file in text form;
  • Hybrid persistence, a new method after Redis 4.0, hybrid persistence combines the advantages of RDB and AOF.When writing, first write the current data to the beginning of the file in the form of RDB, and then save the subsequent operation commands to the file in the form of AOF, which can not only ensure the speed of Redis restart, but also reduce the risk of data loss .

Because each persistence scheme has specific usage scenarios.

Redis RDB memory data persistence operation mode

  • RDB (Redis DataBase) is the process of writing a memory snapshot (Snapshot) at a certain moment to disk in binary form.
  • Memory snapshots are what we said above.It refers to the state record of data in memory at a certain moment.
  • This is similar to taking a photo. When you take a photo of a friend, a photo can instantly record all the images of the friend.
  • There are two ways to trigger RDB: one is manual triggering, and the other is automatic triggering.

Manually trigger the RDB

There are two operations to manually trigger persistence:savebgsave.

The main difference between them is whether or not to block the execution of the Redis main thread.

1. save command

Executing the save command on the client side will trigger the persistence of Redis, but it will also make Redis in a blocking state. It will not respond to commands sent by other clients until the RDB is persisted, so it must be used with caution in the production environment.

127.0.0.1:6379> save
OK
127.0.0.1:6379>

The process of executing the command is shown in the figure 

2. bgsave command

  • bgsave (background save) is a background save.
  • The biggest difference between it and the save command is that bgsave will fork a child process to perform persistence.
  • The whole process is only when the child process is fork.There is only a brief blockage.
  • After the child process is created, the main process of Redis can respond to requests from other clients.

with blocking the entire processsavecompared to the commandbgsaveCommand is obviously more suitable for us to use.

127.0.0.1:6379> bgsave
Background Saving started # 提示开始后台保存 
127.0.0.1:6379>

Automatically trigger RDB

After talking about manual triggering, let's look at automatic triggering.We can configure the conditions for automatic triggering in the configuration file.

1. save mn

  • save mn means that within m seconds, if n keys change, persistence is automatically triggered.Parameters m and n can be found in the Redis configuration file.
  • For example, save 60 1 means that within 60 seconds, as long as one key changes, RDB persistence will be triggered.
  • The essence of automatically triggering persistence is that if the set trigger conditions are met, Redis will automatically execute the bgsave command once.

Note: When multiple save mn commands are set, any one condition will trigger persistence.

For example, we set the following two save mn commands:

save 60 10
save 600 20
  • When the Redis key value changes 60 times within 10s, persistence will be triggered;
  • If the Redis key changes within 60s, and if the value changes less than 10 times, Redis will determine whether the Redis key has been modified at least 600 times within 20s, and if so, trigger persistence.

2. Flushall

  • The flushall command is used to flush the Redis database.
  • It must be used with caution in a production environment.
  • When Redis executes the flushall command, it triggers automatic persistence and clears the RDB files.

3. Master-slave synchronization trigger

In Redis master-slave replication, when the slave node performs a full replication operation, the master node will execute the bgsave command to send the RDB file to the slave node. This process automatically triggers Redis persistence.

Redis can query the current configuration parameters through commands.

The format of the query command is:config get xxx

For example, if you want to get the storage name setting of an RDB file, you can use config get dbfilename .

The execution effect is as follows:

127.0.0.1:6379> config get dbfilename
1) "dbfilename"
2) "dump.rdb"

Since the Redis server will block when loading the RDB file until the loading is complete, it may cause a long time and the website cannot be accessed.

If you want to manually delete the RDB cache file dump.rdb of Redis, you can use the following command to find the storage path of the dump.rdb file▼

find / -name dump.rdb
  • Then, manually delete the dump.rdb cache file via SSH.

Redis sets the configuration of RDB

Regarding setting the configuration of RDB, you can use the following two ways:

  1. Manually modify the Redis configuration file
  2. Use the command line settings, config set dir "/usr/data" is the storage command to modify the RDB file

Note: The configuration in redis.conf can be obtained through config get xxx and modified through config set xxx value, and the method of manually modifying the Redis configuration file is globally effective, that is, the parameters set by restarting the Redis server will not be lost, but modified using the command , it will be lost after Redis restarts.

However, if you want to manually modify the Redis configuration file to take effect immediately, you need to restart the Redis server, and the command method does not require restarting the Redis server.

RDB file recovery

When the Redis server starts, if the RDB file dump.rdb exists in the Redis root directory, Redis will automatically load the RDB file to restore persistent data.

If there is no dump.rdb file in the root directory, please move the dump.rdb file to the root directory of Redis first.

Of course, there is log information when Redis starts, which will show whether the RDB file is loaded.

The Redis server blocks while loading the RDB file until the loading is complete.

Now we know that RDB persistence is divided into two ways: manual triggering and automatic triggering:

  1. Its advantage is that the storage file is small and data recovery is faster when Redis is started.
  2. The downside is that there is a risk of data loss.

The recovery of RDB files is also very simple. Just put the RDB files in the root directory of Redis, and Redis will automatically load and restore data when it starts.

RDB pros and cons

1) RDB advantages

The content of RDB is binary data, which occupies less memory, is more compact, and is more suitable as a backup file;

RDB is very useful for disaster recovery, it is a compressed file that can be transferred to a remote server faster for Redis service recovery;

RDB can greatly improve the speed of Redis, because the main Redis process will fork a child process to persist data to disk.

The Redis main process does not perform operations such as disk I/O;

Compared with AOF format files, RDB files restart faster.

2) Disadvantages of RDB

Because RDB can only save data at a certain time interval, if the Redis service is accidentally terminated in the middle, the Redis data will be lost for a period of time;

A process in which RDB requires frequent forks to save it on disk using subentry.

If the dataset is large, fork can be time-consuming, and if the dataset is large, the CPU performance is poor, which can cause Redis to be unable to serve clients for a few milliseconds or even a second.

Of course, we can also disable persistence to improve the execution efficiency of Redis.

If you are not sensitive to data loss, you can do this when the client connects config set save "" Command to disable persistence for Redis.

Onredis.conf, if insaveComment out all configurations at the beginning, and persistence will also be disabled, but this is generally not done.

Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "What is the full name of Redis RDB? Redis RDB In-Memory Data Persistence Operation Mode", will help you.

Welcome to share the link of this article:https://www.chenweiliang.com/cwl-26677.html

Welcome to the Telegram channel of Chen Weiliang's blog to get the latest updates!

🔔 Be the first to get the valuable "ChatGPT Content Marketing AI Tool Usage Guide" in the channel top directory! 🌟
📚 This guide contains huge value, 🌟This is a rare opportunity, don’t miss it! ⏰⌛💨
Share and like if you like!
Your sharing and likes are our continuous motivation!

 

Comment

Your email address will not be published. Required fields * Callout

scroll to top