当前位置:网站首页>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 .


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

 Insert picture description here

  • 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

 Insert picture description here

Four 、RDB Mechanism

1、 Trigger mechanism

  • RDB There are three triggering mechanisms , Namely :save Trigger bgsave Trigger and Automatic 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 .rdb
  • dir
    explain : Set up storage .rdb Path to file
    Experience : It is usually set to a directory with large storage space , Directory name data
  • rdbcompression 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 .

 Insert picture description here

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 .

 Insert picture description here

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 changes
  • effect : Meet the time limit key When the number of changes reaches the specified number, persistence is carried out
  • Parameters :second: Monitor the time frame ,changes: monitor key Amount of change
  • Location : stay conf File to configure
  • Example
     Insert picture description here

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 ~

原网站

版权声明
本文为[I am a cabbage]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230547375354.html