当前位置:网站首页>How does redis implement persistence? Explain in detail the three triggering mechanisms of RDB and their advantages and disadvantages, and take you to quickly master RDB
How does redis implement persistence? Explain in detail the three triggering mechanisms of RDB and their advantages and disadvantages, and take you to quickly master RDB
2022-07-23 13:07:00 【I am a cabbage】
Hello everyone , I'm cabbage . This article mainly explains Redis Persistence in , Explain in detail RDB Characteristics of persistence function , If you get something after reading the article , You can support bloggers three times in a row ~, Hee hee .
List of articles
One 、 Preface
- Everybody knows Redis Is a memory database , All data is stored in memory , If persistence is not configured , When we close redis After the server restarts again, all the data is lost , So it needs to be turned on redis Persistence of , Save data to disk , Guaranteed at redis After restart , Data can be recovered from disk .
Two 、 Introduction to persistence
What is persistence :Use permanent storage media ( For example, hard disk 、mysql database ) Save the data , The working mechanism of restoring the saved data at a specific time is called persistence .Why persistence :Prevent accidental loss of data , Ensure data security .Redis What are the ways to achieve persistence ?Redis There are two ways to achieve persistence —— RDB(Redis DataBase) and AOF(Append Of File)
3、 ... and 、Redis Persistence
RDB:Save the current data state , Snapshot form , Store data results , The storage format is simple , The focus is on data

AOF:Save the operation process of data , Log form , Store operation procedures , The storage format is complex , The focus is on the operation of data

Four 、RDB Mechanism
1、 Trigger mechanism
- RDB There are three triggering mechanisms , Namely :
save Trigger、bgsave TriggerandAutomatic triggering.
1.1、save Trigger
command :
127.0.0.1:6379> set name cabbage
OK
127.0.0.1:6379> save
OK
save Instruction related configuration :
dbfilename dump.rdb
explain : Set the local database file name , The default value is dump.rdb
Experience : Usually set to dump- Port number .rdbdir
explain : Set up storage .rdb Path to file
Experience : It is usually set to a directory with large storage space , Directory name datardbcompression yes
explain : Set whether to compress data when storing to local database , The default is yes, use LZF Compress
Experience : It is usually on by default , If set to no, Can save CPU The elapsed time , But it makes the stored files bigger ( huge )rdbchecksum yes
explain : Set whether to RDB File format verification , The verification process is carried out in the process of writing and reading files
Experience : It is usually on by default , If set to no, It can save the reading and writing process about 10% Time consuming , However, there is a certain risk of data corruption
Operational analysis :
This way will block the current Redis The server , perform save During the order ,Redis Can't handle other orders , until RDB Until the process is complete , If there is an old RDB file , Just replace the old one with the new one , It is likely to cause long-term congestion , Online environment is not recommended .

1.2、bgsave Trigger
command :
127.0.0.1:6379> set name cabbage
OK
127.0.0.1:6379> BGSAVE
Background saving started
bgsave Instruction related configuration :
stop-writes-on-bgsave-error yes
explain : If there is an error in the background stored procedure , Whether to stop the save operation
Experience : It is usually on by default
Operational analysis :
When the order is executed ,Redis The snapshot operation will be performed asynchronously in the background , The snapshot can also respond to client requests . The specific process is Redis Process execution fork Action create subprocess ,RDB The persistence process is the responsibility of the subprocess , It will automatically end when it is finished . The blockage only happens in fork Stage , The average time is very short .bgsave So is the order Redis Inside RDB The default mode of operation .

Be careful : bgsave The order is for save Optimization of blocking problem .Redis It's all about RDB All operations use bgsave The way ,save The command can be discarded .
1.3、 Automatic triggering
To configure :save second changeseffect :Meet the time limit key When the number of changes reaches the specified number, persistence is carried outParameters :second: Monitor the time frame ,changes: monitor key Amount of changeLocation :stay conf File to configure- Example

What configuration means :
900 Write at least once in seconds 、300 At least within seconds 10 Write operations 、60 Occurs at least in seconds 10000 Write operations ; As long as any condition is satisfied , Will trigger bgsave
Be careful :
- save The configuration should be set according to the actual business situation , If the frequency is too high or too low, performance problems will occur , The results can be catastrophic
- save In the configuration, for second And changes Settings usually have complementary correspondence , Try not to set up an inclusive relationship
- save After the configuration is started, the execution is bgsave operation
2、 Way comparison
| The way | save Instructions | bgsave Instructions |
|---|---|---|
| Reading and writing | Sync | asynchronous |
| Blocking client instructions | yes | no |
| Extra memory consumption | no | yes |
| Start a new process | no | yes |
3、RDB advantage
- RDB Is a compact binary , High storage efficiency
- RDB What's stored inside is redis A snapshot of data at a point in time , Perfect for data backup , Full scale replication and other scenarios
- RDB It's faster to recover data than AOF Much faster
4、RDB shortcoming
- RDB Whether it's executing instructions or using configuration , Unable to achieve real-time persistence , There is a high probability of data loss
- bgsave The instructions are executed every time they run fork Action create subprocess , To sacrifice some performance
- Redis In many versions of RDB Unified version of file format , There may be incompatibility of data formats among different versions of services
Thank you for reading , Progress together , Hee hee ~
边栏推荐
- RHCSA--文件內容瀏覽、cut、uniq、sort、.tr命令使用
- PPP configuration instance learning record
- C # enter a letter and judge its case
- HCIA----07 ACL-Net
- Summary of basic SQL operations
- OpenVPN deployment
- 信号完整性(SI)电源完整性(PI)学习笔记(三十二)电源分配网路(四)
- Build FRPC client in NAS [super brainless]
- 雷达导论PART VII.4 SAR系统设计
- Rhcsa - - parcourir le contenu du fichier, couper, uniq, trier, utiliser les commandes.tr
猜你喜欢
随机推荐
RHCSA--文件內容瀏覽、cut、uniq、sort、.tr命令使用
数据进行脱敏
课程设计-推箱子C#(win form)
Telnet 配置实例学习记录
ACL——net
Frame relay network configuration example learning record
使用vscode进行远程编辑和调试
静态路由的搭建
4D毫米波雷达硬件系统架构
ACL 配置实例学习记录
Secret key remote login server to realize secret free login
2020-10-16
Copy, paste and drag files between VMware virtual machine and host
Rhcsa - - parcourir le contenu du fichier, couper, uniq, trier, utiliser les commandes.tr
HCIA----07 ACL-Net
单臂路由配置实例学习记录
录入数学公式至mark down文档的方法
C#输出斐波那契数列
虚拟内存技术的来龙去脉(上)
Query the cross compiled executable dependency Library









