当前位置:网站首页>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 !
边栏推荐
- 搜索二维矩阵[二分巧用 + 记录不同于插入二分的解法]
- Why does Dell always refuse to push the ultra-thin commercial notebook to the extreme?
- Install mysql5.6 under linux64bit - the root password cannot be modified
- 归并排序模板 & 理解
- Use redis' sorted set to make weekly hot Reviews
- Tencent has completed the comprehensive cloud launch to build the largest cloud native practice in China
- 新一代可级联的以太网远程I/O数据采集模块
- 1. 封装自己的脚手架 2.创建代码模块
- Deep learning LSTM model for stock analysis and prediction
- Reverse ordinal number by merge sort
猜你喜欢
随机推荐
‘distutils‘ has no attribute ‘version
The optimism about technology is making Dell achieve more than expected
归并排序模板 & 理解
结合实操带你吃透Redis持久化
Bi-sql top
VB learning notes
这个国庆!腾讯云WeCity陪您一同出行,点亮城市地标...
Convert MySQL query timestamp to date format
PHP 利用getid3 获取mp3、mp4、wav等媒体文件时长等数据
Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
Tencent cloud wecity Industry joint collaborative innovation to celebrate the New Year!
ICML2022 | 用神经控制微分方程建立反事实结果的连续时间模型
天书夜读笔记——反汇编引擎xde32
脱氧核糖核酸酶I中英文说明书
Elastase instructions in Chinese and English
Why does Dell always refuse to push the ultra-thin commercial notebook to the extreme?
[live review] 2022 Tencent cloud future community city operator recruitment conference and SaaS 2.0 new product launch!
腾讯云WeCity解决方案
Cloud development technology summit · public welfare programming challenge [hot registration]!
Tianshu night reading notes -- disassembly engine xde32









