当前位置:网站首页>Interviewer: you said you are proficient in redis. Have you seen the persistent configuration?
Interviewer: you said you are proficient in redis. Have you seen the persistent configuration?
2022-06-24 21:57:00 【InfoQ】
We have already introduced
Redis Commands for five data types
And
The basic configuration of the configuration file
, Today, let's uncover it from two aspects: Theory and configuration
Redis A lasting veil of mystery .
The so-called persistence can be simply understood as the process of saving the data in memory to the hard disk for storage . The persistent data can still be accessed after system restart or downtime , Ensure the security of data .
Redis There are two persistence schemes , One is snapshot (
SNAPSHOTTING), abbreviation
RDB; One is append only mode (
APPEND ONLY MODE), be called AOF. Next, let's take a look at their use and precautions .
RDB
RDB by
Redis DataBase Abbreviation , yes
Redis Default persistence scheme . It can snapshot memory data sets within a specified time interval (
snapshot) Write to disk , Snapshot files will be restored (
dump.rdb ) Read back memory .

Let's start with the... In the configuration file
SNAPSHOTTING:
The configuration file
save
<seconds> <changes>
In the given
Number of seconds
Inside , If you execute on the database
Write operand
Reach the set value , Then synchronize the data to the data file . Support multiple conditions ,
Redis Three conditions are provided in the default configuration file :
save 900 1 //900s There are 1 A change
save 300 10 //300s There are 10 A change
save 60 10000 //60s There are 10000 Changes
Be careful
: If you don't want to use
RDB programme , You can put
save "" The comment opens , Note out the three above .
stop-writes-on-bgsave-error yes
When
bgsave When something goes wrong ,
Redis Whether to stop executing the write command ;
- If
yes, When there is a problem with the hard disk ,RedisWill stop accepting writes , So we can find out in time , Avoid massive loss of data ;
- If
no, beRedisignorebgsaveContinue to execute the write command .
If it has been set to
Redis Proper monitoring and persistence of the server , That is, other means are used to discover and control data integrity , You may want to disable this feature , So that even on disk 、 When there is a problem with permissions ,
Redis Still working .
Be careful
: If the background save process starts working again ,
Redis Will automatically allow writing again .
rdbcompression yes
Specifies whether to store to the local database
Compress
(
Redis use
LZF Compress ) data , The default is
yes. If in order to save money
CPU Time , This option can be turned off , But it will cause the database file to become huge .
rdbchecksum yes
from
RDB edition
5 Start , After storing the snapshot , You can also use
CRC64 Algorithm to check data ,
CRC64 The verification is placed at the end of the file . After opening , Save and load
RDB Files will be added about
10% Performance consumption of , If you want to get the maximum performance improvement , You can turn it off .
Ban
The checksum
Created
RDB The check sum of the file is zero , This will tell the loading code to skip the check .
dbfilename dump.rdb
Specifies the local database file name , Automatically load in after restart
Memory
, Do it manually
save The order will take effect immediately .
Big pit, please pay attention to
:
flushall、
shutdown All commands will be cleared and submitted to
dump.rdbdir ./
Specifies the local database hosting directory .
theory
Operation mode
- When
RedisNeed to savedump.rdbWhen you file , It calls system functionsfork(), Create a subprocess ( Completely consistent with the main process );
- The child process writes the data set to a temporary file
RDBin ;
- When the subprocess completes the new
RDBWhen the file is written ,RedisNew useRDBReplace the original file withRDBfile , And delete the oldRDBfile .
This way of working makes
Redis Can be copied from write time (
copy-on-write) Benefit from the mechanism .
How to trigger RDB snapshot
- The default snapshot configuration in the configuration file ;
- command
save( Blocking , Just save the snapshot , Other waiting ) Or is itbgsave( asynchronous ) command , Snapshots can also respond to client commands ;
- perform
flushallcommand , Clear all data in the database , It doesn't make much sense ;
- perform
shutdowncommand , Make sure that the server is shut down properly without losing any data , It doesn't make much sense .
adopt RDB File recovery data
In actual development , Generally, the physical hard disk is damaged , So we will choose backup
dump.rdb file . Will back up
dump.rdb File copy to
redis Of the installation directory
bin Under the table of contents , restart
redis The service can be .
advantage
RDBIt's a very compact file , Very suitable for data set backup ;
RDBIt's a compact single file , It's easy to transfer to another remote data center or Amazon's S3( It could be encrypted ), Ideal for disaster recovery ;
RedisThe main process of is not runningI/Ooperation , Ensures extremely high performance ;
- Suitable for large-scale data recovery , If the requirements for data integrity and consistency are not high ,
RDBThanAOFIt's more efficient .
shortcoming
- stay
RedisIn case of unexpected downtime , You may lose a few minutes of data ;
RDBNeed to oftenforkSubprocess to save the data set to the hard disk , When the data set is large ,forkThe process is very time consuming , May lead toRedisCan't respond to client requests in milliseconds . If the data set is huge andCPUWhen the performance is not very good , This situation will continue 1 second ;AOFAlso neededfork, However, you can adjust the frequency of rewriting log files to improve the durability of data sets .
AOF
In order to solve
RDB The problem of losing too much data during downtime , from
1.1 Version start ,
Redis Added a kind of
durable Persistence of :
AOF.
AOF yes
Append Only File Abbreviation , Not on by default .
AOF Log every write operation in the form of a log , You can only append files, but you can't overwrite files , These commands are reexecuted when the server restarts to restore the original data .
Let's take another look at... In the configuration file
APPEND ONLY MODE:
The configuration file
appendonly no
The default is off , Change it to
yes Turn on persistence .
AOF and
RDB Can be enabled at the same time without problems .
appendfilename "appendonly.aof"
File default name , Start to create . load
Precede
dump.rdb file
appendfsync
Synchronization strategies : System function
fsync() Tell the operating system to actually write data on the disk .
Redis Support three different modes
appendfsync always // Every time there is a data change, it will be recorded to the disk immediately , Poor performance but good data integrity
appendfsync everysec // Default recommendation , Asynchronous operations , Record every second , If it goes down , Yes 1 Data loss in seconds
appendfsync no // Out of sync , Refresh data only when the operating system needs it
To understand the next configuration , First of all “ Log to rewrite ” Principle :
rewrite
because
AOF The method is to append the command to the end of the file , So as the number of write commands increases ,
AOF The size of the file will become larger and larger . To avoid this , New rewrite mechanism : You can do this without interrupting the service client , Yes
AOF Document reconstruction (
rebuild).
Override trigger :
Through execution
bgrewriteaof command , You can generate a new
AOF file , This file contains the data needed to rebuild the current dataset
least
command .
Redis 2.2 This command needs to be executed manually ,
Redis 2.4 Can be triggered automatically by modifying the configuration file ( The configuration involves ).
Rewriting principle :
RedisExecute system functionsfork(), Create a subprocess ( Completely consistent with the main process );
- The subprocess begins to change
AOFThe contents of the file are written to a temporary file ;
- For all newly executed write commands , The parent process accumulates them in a memory cache , While adding these changes to the existing
AOFEnd of file , So even if there's a stoppage in the middle of the rewrite , The existingAOFFiles are also safe ;
- When the subprocess finishes rewriting , It sends a signal to the parent process , After receiving the signal, the parent process , Append all data in the memory cache to the new
AOFEnd of file .
RedisAtomically replace old files with new ones , After that, all commands will be added directly to the newAOFEnd of file .
no-appendfsync-on-rewrite no
When we execute the main process at the same time
Write operations
And subprocess
rewrite
In operation , Both operate on disks , Rewriting often involves a lot of disk operations , This will cause the main process to write
aof Blocking occurs when the file is .
To solve this problem ,
no-appendfsync-on-rewrite The parameters come out .
- If the parameter is set to
no, It's the safest way , No loss of data , But endure the problem of congestion ;
- If set to
yes, This is equivalent to puttingappendfsyncSet tono, This indicates that no disk operation was performed , Just write to the buffer . So this doesn't cause congestion ( Because there are no competing disks ), But if this timeredisHang up , You lose data . How much data is lost ? staylinuxUnder the default settings of the operating system , The most you can lose 30s The data of .
therefore , If the application system can't stand the delay , And can tolerate a small amount of data loss , Is set to
yes; If the application system can't tolerate data loss , Is set to
no.
auto-aof-rewrite-percentage 100
Rewrite percentage , The default is... After last rewriting
aof Double the file size .
auto-aof-rewrite-min-size 64mb
The minimum value of the override trigger :64mb.
according to
auto-aof-rewrite-min-size and
auto-aof-rewrite-percentage Parameter determination of automatic trigger time .
Redis It will record the last time it was rewritten
AOF size , The default configuration is when
AOF The file size is last
rewrite Double the size and the file is larger than
64M Trigger when .
Large Internet companies are generally
3G start
aof-load-truncated yes
When
AOF When the file is truncated , namely
AOF The last command in the file is incomplete , If you start at this point
Redis, Will
AOF Data is loaded back into memory , At this point, a problem occurs .
- yes: Load a truncated
AOF,RedisThe server started issuing logs , Notify the user of this event ;
- no: The server will abort with an error , Refuse to start .
When we learned
AOF When the file reports an error , You can use the following methods to fix the wrong
AOF file :
- For the existing
AOFFile to create a backup ;
- Use
RedisIncidentalredis-check-aofcommand , To the originalAOFFile for repair ;
redis-check-aof –fix
redis-check-aof --fix appendonly.aofRepair order , Kill all non-conforming grammar
- (Optional) Use
diff -uCompare the repairedAOFDocuments and originalAOFBackup of files , Look at the differences between the two files ;
- restart
RedisThe server , Wait for the server to load the repairedAOFfile , And data recovery .
aof-use-rdb-preamble yes
In rewriting
AOF When you file ,
Redis In the
AOF Use... In the document
RDB Leading , To speed up rewriting and recovery . When this option is enabled , Rewrite the
AOF The file consists of two different sections :
RDB file、
AOF tail load
Redis when , identifies
AOF Document to
Redis
Start of string , And load prefixed
RDB file , Then continue loading
AOF The tail .
theory
advantage
- Higher data integrity and consistency ,
AOFBy using different strategies , At most 1 Second data ;
AOFA file is a log file that only appends , No need to writeseek;
RedisCan be inAOFWhen the file size becomes too large , Automatically in the backgroundAOFRewrite , Rewriting is absolutely safe ;
AOFThe write operation of the file record is performed inRedisThe format of the protocol is saved , Easy to read , Easy to analyze documents ;
shortcoming
- For the same dataset ,
AOFThe volume of the file is usually larger thanRDBVolume of file ;
- According to the
fsyncStrategy ,AOFMay be slower thanRDB.
In general , Per second
fsync Performance is still very high , Shut down
fsync It can make
AOF Speed and
RDB As fast as , Even under high load . But when dealing with large write loads ,
RDB More guaranteed maximum delay time (
latency).
Comparison and summary
How to choose which persistence method to use ?
Generally speaking , If you want to achieve enough
PostgreSQL Data security of , Two persistence functions should be used at the same time .
If you are very concerned about data , But it can still withstand data loss within a few minutes , Then you can only use
RDB Persistence .
because AOF Persistence is better in real time , That is, less data is lost when the process exits unexpectedly , therefore
AOF Is currently the
Main stream
Persistence of .
Many users only use
AOF Persistence , But we don't recommend it : Because of timing generation
RDB snapshot (
snapshot) Very convenient for database backup , also
RDB Data set recovery is also faster than
AOF Fast recovery .
AOF and RDB The interaction between
The version number is greater than or equal to
2.4 Of
Redis in ,
BGSAVE During execution , Can't execute
BGREWRITEAOF . On the other hand , stay
BGREWRITEAOF During execution , You can't do it
BGSAVE. This can prevent two
Redis The background process does a lot of work on the disk at the same time
I/O operation .
If
BGSAVE Being implemented , And the user calls
BGREWRITEAOF command , Then the server will reply to the user with a
OK state , And tell the user
BGREWRITEAOF Has been scheduled to execute : once
BGSAVE completion of enforcement ,
BGREWRITEAOF It will officially start .
When
Redis Startup time , If
RDB Persistence and
AOF Persistence is turned on , Then the program will give priority to
AOF File to recover the dataset , because
AOF The data stored in the file is usually the most complete .
Backup redis data
- Create a recurring task (
cron job), There will be one... Per hourRDBFile backup to a folder , And every day there will be oneRDBFile backup to another folder ;
- Make sure that the backup of the snapshot has the corresponding date and time information , Every time you execute a regular task script , Use
findCommand to delete expired snapshots ;
- At least once a day , take
RDBBack up outside your data center , Or at least back it up to youRedisOutside the physical machine of the server .
Performance Suggestions
In practice , because
RDB The documents are for backup purposes only , The advice is only in
slave On persistence
RDB file , And it just needs 15 One minute backup is enough , Only keep
save 900 1 This rule .
If open
AOF, The advantage is that in the worst case, it will only be lost no more than 2 Second 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
AOF rewrite The frequency of ,
AOF Overridden base size default
64M Is too small , It can be set to
5G above . The default size exceeds the original size 100% When overridden, it can be changed to the appropriate value .
If you don't turn it on
AOF, Only by
Master-Slave Replication 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 .
o Q Will continue to update java Practical articles , If you have a different opinion or a better one idea, Welcome to contact ah Q.
【 o Q Say code 】, Notable official account
The style of the article is changeable , The pictures are easy to understand , The story is vivid and interesting , Let's talk about technology !
边栏推荐
猜你喜欢
随机推荐
应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案
并查集+建图
[notes of Wu Enda] multivariable linear regression
Make tea and talk about heroes! Leaders of Fujian Provincial Development and Reform Commission and Fujian municipal business office visited Yurun Health Division for exchange and guidance
【无标题】
传输层 udp && tcp
《各行业零代码企业应用案例集锦》正式发布
[notes of Wu Enda] convolutional neural network
印刷行业的ERP软件的领头羊
基于kruskal的最小生成树
力扣每日一题-第26天-496.下一个更大元素Ⅰ
如何做到全彩户外LED显示屏节能环保
火狐拖放后,总会默认打开百度搜索,如果是图片,则会打开图片。
手动事务的几个类
The most important thing at present
02---纵波不可能产生的现象
leetcode_ one thousand three hundred and sixty-five
TKKC round#3
机器学习:梯度下降法
【吴恩达笔记】机器学习基础



![[notes of Wu Enda] convolutional neural network](/img/19/2cac17010c29cbd5ba245de105d6c1.png)





