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!

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于实现秒杀的相关内容,包括了秒杀逻辑、存在的链接超时、超卖和库存遗留的问题,下面一起来看一下,希望对大家有帮助。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
