当前位置:网站首页>Redis persistence mechanism

Redis persistence mechanism

2022-07-06 22:50:00 InfoQ


How to guarantee  Redis  Data can be recovered by restarting after being hung up ?

Many times we need to persist data, that is, write the data in memory to the hard disk , Most of the reason is to reuse the data later ( For example, restart the machine 、 Recover data after machine failure ), Or backup data to a remote location to prevent system failure .
Redis  differ  Memcached  It's very important that ,Redis  Support persistence , And it supports two different persistence operations .
Redis  One of the persistence methods of is snapshot (snapshotting,RDB), Another way is to just append files (append-only file, AOF)
. These two methods have their own advantages , I'll elaborate on what these two persistence methods are , How to use it? , How to choose your own persistence method .

What is?  RDB  Persistence ?

Redis  You can create a snapshot to get a copy of the data stored in memory at a certain point in time .Redis  After creating the snapshot , You can back up the snapshot , You can replicate the snapshot to another server to create a server copy with the same data (Redis  Master-slave structure , Mainly used to improve  Redis  performance ), You can also leave the snapshot in place for use when you restart the server .
Snapshot persistence is  Redis  The default persistence method , stay  
redis.conf
  The configuration file has this configuration by default :
save 900 1 # stay 900 second (15 minute ) after , If at least 1 individual key change ,Redis Will trigger automatically BGSAVE Command create snapshot .

save 300 10 # stay 300 second (5 minute ) after , If at least 10 individual key change ,Redis Will trigger automatically BGSAVE Command create snapshot .

save 60 10000 # stay 60 second (1 minute ) after , If at least 10000 individual key change ,Redis Will trigger automatically BGSAVE Command create snapshot .

What is?  AOF  Persistence ?

Compared to snapshot persistence ,AOF  Persistence is better in real time , So it has become a mainstream persistence solution . By default  Redis  It's not turned on  AOF(append only file) Persistence of the way , Can pass  appendonly  Parameter on :
appendonly yes
Turn on  AOF  Every execution will change after persistence  Redis  Commands for data in ,Redis  The command will be written to the memory cache  
server.aof_buf
  in , And then based on  
appendfsync
  Configuration to determine when to synchronize it to the disk  AOF  file .
AOF  The location and  RDB  The location of the files is the same , It's all through  dir  Parameter setting , The default file name is  
appendonly.aof
.
stay  Redis  There are three different profiles for  AOF  Persistence mode , They are :
appendfsync always # Write every time a data change occurs AOF file , This will seriously reduce Redis The speed of
appendfsync everysec # Sync every second , Explicitly synchronize multiple write commands to the hard disk
appendfsync no # Let the operating system decide when to synchronize
In order to balance data and write performance , Users can consider  
appendfsync everysec
  Options  , Give Way  Redis  Synchronize once per second  AOF  file ,Redis  Performance is almost unaffected . And even if there is a system crash , Users can only lose data generated within one second at most . When the hard disk is busy performing write operations ,Redis  It will also gracefully slow down its own speed to adapt to the maximum write speed of the hard disk .
expand : Interview questions :AOF Rewrite mechanism ,redis The interview will be asked !!!
原网站

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