当前位置:网站首页>Combined with practice, you will understand redis persistence
Combined with practice, you will understand redis persistence
2022-06-25 01:29:00 【Logic magic code】
Click on the card Focus on 、 Roof placement official account
Technical dry cargo , Timely delivery !
So called persistence , Is to write the contents of the cache to the disk for permanent storage ( You don't delete , If the disk is not bad, it is permanent )
RDB
RDB yes Redis Default persistence scheme .RDB snapshot (Redis DataBase): When satisfied Under certain conditions When , Will write the data in the current memory to disk , Generate a snapshot file dump.rdb.
Paste configuration first :
# File path ,
dir ./
# File name
dbfilename dump.rdb
# Whether it is LZF Compress rdb file
rdbcompression yes
# Turn on data verification sht
rdbchecksum yes
Redis The restart will go through dump.rdb File recovery data . that Certain conditions What is? ? When to write rdb file ?
Automatic triggering
Configuration rules trigger
redis.conf, SNAPSHOTTING To configure , It defines the trigger Save data to disk Trigger frequency . If you don't need to RDB programme , notes Or configure it as an empty string " " .
save 900 1 # 900 At least one in two seconds key Be modified ( Including adding )
save 300 10 # 300 At least in seconds 10 individual key Be modified
save 60 10000 # 60 At least in seconds 10000 individual key Be modified
Note that the above configuration does not conflict , As long as any one is satisfied, it will trigger .
shutdown Trigger , Make sure the server is shut down , It will not cause data loss when closing
Manual trigger
If we need to restart services or migrate data , At this time, it needs to be triggered manually RDB Snapshot save . therefore redis Also provided 2 A manual save RDB Snapshot instructions .
Save save Blocking the current... While generating the snapshot Redis The server ,Redis Can't handle other orders . If there is more data in memory , Can cause Redis A long block . This command is not recommended for production environments . To solve this problem ,Redis Provides a second way . bgsave perform bgsave when ,Redis The snapshot operation will be performed asynchronously in the background , The snapshot can also respond to client requests . The specific operation is Redis Process execution fork( Create process functions ) Action create subprocess (copy-on-write),RDB The persistence process is the responsibility of the subprocess , It will automatically end when it is finished . It doesn't record fork The subsequent orders . The blockage only happens in fork Stage , The average time is very short . use lastsave Command to view the last time a snapshot was successfully generated .
Show it once RDB Snapshot is a process of restoring data !( My snapshot is src below )
We first add a copy of the data and back it up to RDB:
redis> set k1 1
redis> set k2 2
redis> set k3 3
See if the data exists :
redis>keys *
We carry out shutdown Operation trigger RDB snapshot :( Simulate accidental power failure )
redis> shutdown
To the existing RDB Data backup cp:
redis>cp dump.rdb dump.rdb.bak
start-up redis:
redis> src/redis-server& redis.conf
The data is still there , Now simulate data loss ( Clear it by hand )
redis> flushall
Stop and restart the server
redis> shutdown
redis> src/redis-server& redis.conf
It is found that the data has been lost , We now need to recover from our backup data , Shut down first
redis> shutdown
Delete the original RDB The backup data cd
redis>rm dump.rdb
Rename the backup data dump.rdb
mv dump.rdb.bak dump.rdb
Restart the service
src/redis-server& redis.conf
Take a look at the data by yourself ( Manual funny )
We know RDB The principle logic of the realization of , So let's analyze RDB What are the advantages and disadvantages .
advantage
RDB It's a very compact (compact type ) The file of , It has been saved. redis Data set at a certain point in time . This file is ideal for backup and disaster recovery . Generate RDB When you file ,redis The main process will fork() Reprocess a subprocess to handle all the saving work , The main process does not need any disks IO operation . RDB Speed ratio when recovering large data sets AOF It's faster to recover .
Inferiority
RDB Modal data cannot be persisted in real time / Second persistence . because bgsave Run every time fork Action create subprocess , Frequent execution cost is too high . Make a backup at regular intervals , So if redis accident down If you drop it , All changes since the last snapshot will be lost ( Data is missing ).
In view of the disadvantages ,redis It also provides a persistence mechanism , Namely AOF Persistence mechanism !
AOF
AOF yes Append Only File For short , Load only files , This program is in redis Not on by default .
AOF How to recover ?
AOF Log every write operation , And add it to the file .
After opening , Make changes Redis When data is ordered , Will write the command to AOF In file .Redis During the restart, the write instructions will be executed from the front to the back according to the contents of the log file to complete the data recovery .
So where does it start , What are the characteristics ?
Or paste configuration : The configuration file redis.conf
# switch
appendonly no
# file name
appendfilename "appendonly.aof"
Synchronization mechanism
AOF, Each write operation will be recorded , So here comes the question ? Do I have to interact with the disk every time I operate the command ? Of course not , therefore ,redis Support several strategies , It's up to you to decide whether you want to interact with the disk every time .
appendfsync always Indicates that every write is executed fsync( Refresh ) function
appendfsync everysec Once a second fsync function Default 1s once , At most 1s The loss of
appendfsync no Data synchronization to disk is guaranteed by the operating system , The fastest
Rewrite mechanism (rewrite)
Why rewrite ?
such as , We use distributed locks , Instruction is setnx, Then set the expiration date ; Because the command is appended every time . that 1 After year ,10 After year , We're going to find this AOF The file is full of such instructions !!
One of me 1T Of AOF file , It's all such instructions . So we need to rewrite .
AOF File rewriting is not about rearranging the original file , Instead, read the existing key value pairs of the server directly , Then use one command to replace the previous commands recording this key value pair , Create a new file and replace the original one AOF file .
How to rewrite :
4.0 Before the release , Is to compare instructions and remove invalid instructions
If :aof There are several instructions in the document :
redis> lpush huihuilist a
redis> lpush huihuilist b c d
redis> lpop huihuilist
It will be rewritten as
redis> lpush huihuilist a b c
Later it was found that , This method is inefficient , It needs a zone by zone comparison !
therefore ,4.0 And then introduced RDB Follow AOF Mixed mode , Give Way RDB Follow AOF Use it together , because RDB Fast , So let's introduce this feature , according to RDB Pattern , Overwrite the previous instructions in binary form to aof, Those written later continue to pursue to the back of the file .( Mixed mode is on by default , You can also turn off )
aof-use-rdb-preamble yes // Open or not RDB And AOF Mixed mode
When to rewrite ?
The configuration file redis.conf
# Override trigger mechanism
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
// In the beginning aof The file must reach this file size before triggering , Each subsequent rewrite will not be based on this variable
Explain the meaning of the above configuration :
stay aof The file is less than 64mb Do not rewrite when , When they arrive in 64mb When , Just rewrite it once .
The rewritten aof The document could be 20mb. It's configured auto-aof-rewrite-percentag by 100, namely aof Here is the document 40mb When , Start rewriting again . And so on .
We know AOF Implementation principle of , Let's analyze its advantages and disadvantages .
advantage
It can guarantee data security to the greatest extent , Even with the default configuration everysec, At best, it will only cause 1s Data loss for .
shortcoming
Data volume ratio RDB It's much bigger. , So there's no performance RDB good , There is no performance guarantee !
What kind of persistence should we use in our daily development ?
If we can tolerate the loss of data in a short period of time , There is no doubt about using RDB It's the best , Timed generation RDB snapshot , Very convenient for database backup , also RDB Data set recovery is also faster than AOF Fast recovery .
author : Xiao Zhao, who is striving for millions of annual salaryhttps://blog.csdn.net/weixin_44688973/article/details/125335060
Previous recommendation
official account “ Logic magic code ” The published content indicates the source of , All rights reserved ( Those whose copyright cannot be verified or whose source is not indicated all come from the Internet , Reprinted , The purpose of reprinting is to convey more information , The copyright belongs to the original author . If there is any infringement , Please contact the , The author will delete the first time !
边栏推荐
- Hands on data analysis data modeling and model evaluation
- Ecological escort cloud service providers wave "Intel flag"
- 胰蛋白酶中英文说明书
- php easywechat 和 小程序 实现 长久订阅消息推送
- AutoCAD - two extension modes
- 安超云:“一云多芯”支持国家信创政务云落地
- Texture enhancement
- 【直播回顾】2022腾讯云未来社区城市运营方招募会暨SaaS 2.0新品发布会!
- Unity C# 网络学习(六)——FTP(二)
- Transformers 库的基本使用
猜你喜欢

Cobalt strike installation tutorial

Programmer: did you spend all your savings to buy a house in Shenzhen? Or return to Changsha to live a "surplus" life?

Pbcms adding cyclic digital labels

数组中关于sizeof()和strlen

Reading notes at night -- deep into virtual function

百度语音合成语音文件并在网站中展示

"One good programmer is worth five ordinary programmers!"

新一代可级联的以太网远程I/O数据采集模块

汇编语言(3)16位汇编基础框架与加减循环

AutoCAD - two extension modes
随机推荐
php easywechat 和 小程序 实现 长久订阅消息推送
C language boundary calculation and asymmetric boundary
腾讯云WeCity解决方案
带马尔科夫切换的正向随机微分方程数值格式模拟
Basic knowledge of assembly language (2) -debug
[live review] 2022 Tencent cloud future community city operator recruitment conference and SaaS 2.0 new product launch!
Fan benefits, JVM manual (including PDF)
Super detailed description and derivation of convolution and deconvolution (deconvolution is also called transpose convolution and fractional step convolution)
期望与方差
天书夜读笔记——内存分页机制
梦想CAD云图与GIS结合演示
纹理增强
天书夜读笔记——8.4 diskperf反汇编
Yasea APK Download Image
Abnova 5-methylcytosine polyclonal antibody
Powerbi - for you who are learning
AutoCAD - two extension modes
Assembly language (4) function transfer parameters
Transformers 库的基本使用
动手学数据分析 数据建模和模型评估