Learn more about the snapshot method (RDB) in Redis persistence
This article will introduce you to the snapshot method (RDB) in Redis persistence. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Redis reads and writes in memory, so its performance is high, but the data in memory will be lost as the server restarts. In order to ensure that the data Without losing it, we need to store the data in the memory to the disk so that the original data can be restored from the disk when Redis restarts. The whole process is called Redis persistence.
Redis persistence is also one of the main differences between Redis and Memcached, because Memcached does not have persistence functionality.
1. Several persistence methods
Redis persistence has the following three methods:
- Snapshot method(RDB, Redis DataBase) writes the memory data at a certain moment to the disk in binary form;
- file append method (AOF, Append Only File) records all operation commands and writes them as text Append to the file in the form;
- Hybrid persistence method, 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 store the subsequent operation commands in the file in the AOF format. This can not only ensure the speed of Redis restart, but also reduce the risk of data loss.
Because each persistence solution has specific usage scenarios, let’s start with RDB persistence.
2. Introduction to RDB
RDB (Redis DataBase) is the process of writing a memory snapshot (Snapshot) at a certain moment to disk in binary form.
3. Persistence triggering
There are two types of persistence triggering methods for RDB: one is manual triggering, and the other is automatic triggering.
1) Manual trigger
There are two manual trigger persistence operations: save
and bgsave
. Their main difference is reflected in: whether to block or not. Redis main thread execution.
① save command
Executing the save
command in the client will trigger the persistence of Redis, but at the same time it will also make Redis in a blocking state until the RDB is persisted. Completed, it will respond to commands sent by other clients, so must be used with caution in a production environment.
save
The command is used as follows:
As can be seen from the picture, after executing the save
command , the modification time of the persistence file dump.rdb
changes, which means save
successfully triggered RDB persistence.
save
The command execution process is as shown below:
save command is that
bgsave will fork() a child process to perform persistence. In the whole process, only the fork() child process Sometimes there is a short blocking. After the child process is created, the main process of Redis can respond to the requests of other clients. Compared with the
save command that blocks the entire process, obviously
bgsave command is more suitable for us to use.
bgsave The command is used as shown in the figure below:
The execution process is as shown in the figure below:
2) Automatic triggering
After talking about the manual triggering method of RDB, let’s look at how to automatically trigger RDB persistence?
RDB automatic persistence mainly comes from the following situations.
① save m n
means that if n keys change within m seconds, persistence will be automatically triggered. The parameters m and n can be found in the Redis configuration file. For example,
save 60 1
indicates that if at least one key changes within 60 seconds, RDB persistence will be triggered. Automatically trigger persistence. The essence is that Redis will automatically execute the
bgsave
command once if the set trigger conditions are met. Note: When setting multiple save m n commands, persistence will be triggered if any condition is met.
For example, we set up the following two save m n commands:
- save 60 10
- save 600 1
If the Redis key value changes 10 times within 60s, persistence will be triggered; if Redis key value changes within 60s If the key value has changed less than 10 times, Redis will determine whether the key value of Redis has been modified at least once within 600s. If so, persistence will be triggered.
② flushall
flushall
command is used to clear the Redis database. It must be used with caution in a production environment. When Redis executes the flushall
command, Automatic persistence will be triggered and the RDB file will be cleared.
The execution results are shown in the figure below:
③ Master-slave synchronization triggers
In Redis master-slave replication, when the slave node performs a full replication operation , the master node will execute the bgsave
command and send the RDB file to the slave node. This process will automatically trigger Redis persistence.
4. Configuration instructions
Properly setting the RDB configuration can ensure the efficient and stable operation of Redis. Let’s take a look at the configuration items of RDB?
RDB configuration parameters can be found in the Redis configuration file. The specific contents are as follows:
# RDB 保存的条件 save 900 1 save 300 10 save 60 10000 # bgsave 失败之后,是否停止持久化数据到磁盘,yes 表示停止持久化,no 表示忽略错误继续写文件。 stop-writes-on-bgsave-error yes # RDB 文件压缩 rdbcompression yes # 写入文件和读取文件时是否开启 RDB 文件检查,检查是否有无损坏,如果在启动是检查发现损坏,则停止启动。 rdbchecksum yes # RDB 文件名 dbfilename dump.rdb # RDB 文件目录 dir ./
The more important parameters are as follows:
① save parameter
It is used to configure the parameters that trigger RDB persistence conditions. When the save conditions are met, the data will be persisted to the hard disk.
The default configuration description is as follows:
- save 900 1: Indicates that if at least 1 key value changes within 900 seconds, the data will be persisted to the hard disk;
- save 300 10: Indicates that if at least 10 key values change within 300 seconds, the data will be persisted to the hard disk;
- save 60 10000: Indicates that if at least 10,000 key values change within 60 seconds, the data will be persisted to the hard drive.
② rdbcompression parameter
The default value is yes
which means turning on RDB file compression, Redis will use the LZF algorithm for compression. If you don't want to consume CPU performance for file compression, you can set it to turn off this function. The disadvantage of this is that it requires more disk space to save files.
③ rdbchecksum parameter
Its default value is yes
, which indicates whether to enable RDB file check when writing and reading files, and check whether there is any damage. If If damage is found during startup, the startup will be stopped.
5. Configuration query
You can use commands to query the current configuration parameters in Redis. The format of the query command is: config get xxx
. For example, if you want to get the storage name setting of the RDB file, you can use config get dbfilename
. The execution effect is as shown in the figure below:
To query the file directory of RDB, you can use the command config get dir
. The execution effect is as shown in the figure below:
6. Configuration settings
Set the RDB configuration in the following two ways:
- Manually modify the Redis configuration file;
- Use command line settings, for example, use
config set dir "/usr/data"
is used to modify the RDB storage directory.
Note: The method of manually modifying the Redis configuration file is globally effective, that is, restarting the Redis server setting parameters will not be lost, and using the command modification method, restart the Redis Then it will be lost. However, if you want to manually modify the Redis configuration file to take effect immediately, you need to restart the Redis server, but the command method does not require restarting the Redis server.
Tips: The Redis configuration file is located in the root path of the Redis installation directory, and the default name is redis.conf.
7.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 the 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.
Verify whether the RDB file is loaded
Redis has log information during startup, which will show whether the RDB file is loaded. We execute the Redis startup command: src/redis-server redis. conf
, as shown in the figure below:
It can be seen from the log that the Redis service has loaded the RDB file normally when it started.
Tips: While the Redis server is loading the RDB file, it will be blocked until the loading work is completed.
8.RDB advantages and disadvantages
1) RDB advantages
- RDB content is binary data, takes up less memory, is more compact, and is more suitable as Backup file;
- RDB is very useful for disaster recovery. It is a compact file that can be transferred to the remote server faster for Redis service recovery;
- RDB can improve Redis to a greater extent The running speed is because the Redis main process will fork() a child process every time it is persisted to persist the data to the disk. The Redis main process will not perform operations such as disk I/O;
- With AOF RDB files can be restarted faster than files in the format.
2) Disadvantages of RDB
- Because RDB can only save data for a certain time interval, if the Redis service is accidentally terminated midway, the data for a period of time will be lost. Redis data;
- RDB requires frequent fork() to persist it on disk using child processes. Fork() can be time-consuming if the data set is large, and can cause Redis to stop serving clients for a few milliseconds or even a second if the data set is large and CPU performance is poor.
9. Disable persistence
Disabling persistence can improve the execution efficiency of Redis. If you are not sensitive to data loss, you can execute it while connecting to the clientconfig set save ""
command can disable the persistence of Redis, as shown in the following figure:
10. Summary
Through this article we can get As you know, RDB persistence is divided into two methods: manual triggering and automatic triggering. Its advantage is that the storage file is small and data recovery is faster when Redis starts. The disadvantage is that there is a risk of losing data. Restoring RDB files is also very simple. You only need to put the RDB files in the root directory of Redis, and the data will be automatically loaded and restored when Redis starts.
11. Thinking questions
If the Redis server CPU usage is too high, what may be the cause? You are welcome to write your answers in the comment area.
12. References & Acknowledgments
https://redis.io/topics/persistence
https://blog.csdn.net/ qq_36318234/article/details/79994133
https://www.cnblogs.com/ysocean/p/9114268.html
https://www.cnblogs. com/wdliu/p/9377278.html
This article is reproduced from: https://segmentfault.com/a/1190000021036574
For more redis knowledge, please pay attention to redis introduction Tutorial column.
The above is the detailed content of Learn more about the snapshot method (RDB) in Redis persistence. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information
