当前位置:网站首页>Redis learning journey - persistence

Redis learning journey - persistence

2022-06-13 07:28:00 Zhao JC


Redis Is a memory database , If you do not save the database state in memory to disk , So once the server process exits , The database state in the server also disappears , therefore Redis Provides persistent functionality .

RDB(Redis DataBase)

rdb What is it?

In master-slave replication ,rdb It's standby ! Backup to the slave !
 Insert picture description here

  • The core : Write the data set snapshot in memory to disk within the specified time interval , That's what jargon says Snapshot snapshot , When it recovers, it reads the snapshot file directly into memory .
  • Detailed process :Redis Will create... Separately (fork) A subprocess to persist , Write the data to a temporary file first , When the persistence process is over , Replace the last persistent file with this temporary file . The whole process , The main process is not going to do anything IO Operation of the . This ensures extremely high performance . If large-scale data recovery is needed , And it's not very sensitive to the integrity of data recovery , that RDB The way is better than AOF It's more efficient .RDB The disadvantage is that the data may be lost after the last persistence . Our default is
    RDB, In general, you don't need to modify this configuration !
  • Sometimes in the production environment, we Backup this file !
  • rdb The saved file is dump.rdb All of them are configured in our configuration file !

 Insert picture description here
test :
 Insert picture description here

  • First the dump.rdb File deletion , Then modify it once in a minute , see dump.rdb Whether it is generated , close Redis Then check whether the set value exists

 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

  • Use flushall after , Look at the build dump.rdb file
     Insert picture description here
     Insert picture description here
     Insert picture description here

Trigger mechanism

  • 1、save When the rules are satisfied , Will automatically trigger rdb The rules
  • 2、 perform flushall command , It will trigger our rdb The rules !
  • 3、 sign out redis, There will be rdb file !

How to restore rdb file

  • 1、 Only need to rdb The documents are in us redis Just start the directory ,redis It will check automatically when it starts dump.rdb Recover the data !
  • 2、 See where you need to be
127.0.0.1:6379> config get dir 
1) "dir" 
2) "/usr/local/bin" #  If it exists in this directory  dump.rdb  file , The data in it will be recovered automatically when it is started 

Advantages and disadvantages

advantage :

  • 1、 Suitable for large-scale data recovery !
  • 2、 The integrity of the data is not high !(60 minute 5 Time modification , If 59 It went down in minutes , The last modified data cannot be obtained )

shortcoming

  • 1、 Need a certain time interval process operation ! If redis Unexpected downtime , There is no need to modify the data for the last time !
  • 2、fork Process time , It will occupy a certain amount of memory space !!

AOF(Append Only File)

aof What is it?

  • Schematic diagram

 Insert picture description here

  • The core : Record all our orders ,history, When restoring, execute the file all over again !
  • Detailed process : Log every write operation in the form of a log , take Redis All instructions executed are recorded ( Reading operation does not record ), Only additional documents are allowed But you can't rewrite the file ,redis At the beginning of startup, it will read the file and rebuild the data , In other words ,redis In case of restart, execute the write instruction from the front to the back according to the contents of the log file to complete the data recovery
  • aof What is kept is appendonly.aof file

Related configuration

  • Open or not aof

 Insert picture description here

 Insert picture description here
Not on by default , We need to configure it manually ! We just need to put appendonly Change it to yes It opens. aof!
restart ,redis It's going to take effect !
 Insert picture description here
 Insert picture description here

  • Some writing strategies

 Insert picture description here
 Insert picture description here

  • Rewritten rules
    aof The default is unlimited appending of files , The file will get bigger and bigger !
    If aof File is larger than 64m, It's too big ! fork A new process to rewrite our files !
     Insert picture description here

Fault tolerance mechanism

If this aof There is an error in the file , Now redis Can't it start , We need to fix this aof file
redis Provides us with a tool redis-check-aof --fix

  • modify appendonly.aof

 Insert picture description here

  • Restart the service and report an error

 Insert picture description here

  • use redis-check-aof --fix Tool repair

 Insert picture description here

  • View the repair results

 Insert picture description here

Advantages and disadvantages

# appendfsync always #  Every time you make a change, it will  sync. Consumption performance  
appendfsync everysec #  Once a second  sync, You may lose this 1s The data of !
# appendfsync no #  Don't execute  sync, At this time, the operating system synchronizes its own data , The fastest !

advantage :

  • 1、 Every change is synchronized , The integrity of the document would be better !
  • 2、 Synchronize once per second , One second of data could be lost
  • 3、 No synchronization , The most efficient !
    shortcoming :
  • 1、 Relative to data files ,aof Far greater than rdb, It's faster than rdb slow !
  • 2、Aof It's more efficient than rdb slow , So we redis The default configuration is rdb Persistence !

Redis How to expand the capacity of persistent data and cache ?

  • If Redis Used as a cache , Dynamic capacity expansion using consistent hash
  • If Redis Used as a persistent store , Use Redis Cluster de capacity expansion

Expand

1、RDB Persistence mode can snapshot your data in a specified time interval
2、AOF Persist to record every write to the server , These commands are reexecuted when the server restarts to restore the original data ,AOF Command to Redis Protocol append saves each write to the end of the file ,Redis Also able to AOF The file is rewritten in the background , bring AOF The size of the file should not be too large .
3、 Cache only , If you only want your data to exist when the server is running , You can also use no persistence

4、 Open two persistence methods at the same time

  • under these circumstances , When redis It will be loaded prior to restart AOF File to restore the original data , Because in general AOF The data set saved by the file is better than RDB The data set of the file should be complete .
  • RDB The data is not real-time , When using both, the server will only look for AOF file , Do you want to use only AOF Well ? The author suggests not to , because RDB Better for backing up databases (AOF It's not easy to backup in constant change ), A quick restart , And there won't be AOF May be latent Bug, Keep it as a means in case .
    5、 Performance Suggestions
  • because RDB The documents are for backup purposes only , The advice is only in Slave On persistence RDB file , And as long as 15 One minute backup is enough , Only keep save 900 1
    This rule .
  • If Enable AOF
    , The advantage is that in the worst case, it will only lose no more than two seconds of data , The startup script is simple load Their own AOF Just file it , The first is to bring about continuous IO, Two is AOF
    rewrite At the end of rewrite The blocking caused by writing new data to new files is almost inevitable . As long as the hard disk is licensed , We should try to minimize AOFrewrite The frequency of ,AOF Overridden base size default 64M Is too small , It can be set to 5G above , Default over original size 100% The size rewrite can be changed to the appropriate value .
  • If you don't Enable AOF , Only by Master-Slave Repllcation High availability can also be achieved , Can save a lot of IO, And less rewrite The fluctuation of the system caused by . The price is if Master/Slave At the same time , It will lose more than ten minutes of data , There are also two startup scripts to compare Master/Slave Medium RDB file , Load the newer one , Microblogging is this kind of Architecture .
原网站

版权声明
本文为[Zhao JC]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270548463178.html