当前位置:网站首页>Redis persistence mechanism
Redis persistence mechanism
2022-07-28 03:22:00 【March never dies】
Redis Persistence mechanism
Redis There are two ways to persist :RDB and AOF
RDB
RDB The principle is that Redis Timing of data recording in memory dump To disk .
1> The snapshot file is called RDB file , By default, it is saved in the current running Directory , Also available at redis.conf Middle configuration
# Is it compressed? , It is not recommended to open , Compression also consumes cpu, Disks are worthless
rdbcompression yes
#RDB File name
dbfilename dump.rdb
# The path and directory where the file is saved
dir ./
2> RDB When will it be executed ?
Automatic triggering :
The default is when the service stops :Redis During shutdown, it will be executed once RDB, When Redis After instance failure and restart , Read snapshot file from disk , Restore data .
save m n : stay m Seconds , If there is n The keys change , Will trigger persistence automatically , adopt bgsave perform , If you set multiple 、 As long as its — It will trigger , The configuration file has a default configuration ( You can comment out )
save 900 1 #900 Seconds , If at least 1 individual key Be modified , execute bgsave
save 300 10 #300 Seconds , If at least 10 individual key Be modified , execute bgsave
save “” # Ban RDBflushall: For emptying redis All databases ,flushdb To empty the current redis Data in the library ( The default is 0 The database ), It will be emptied RDB file , It also generates dump.rdb、 Content is empty
Master slave synchronization : It will be triggered automatically during full synchronization bgsave command , Generate rdb Send to slave
Manual trigger :
[[email protected] ~]# redis-cli
127.0.0.1:6379> save
127.0.0.1:6379> bgsave
Background saving started
- save command , send Redis In a blocking state , until RDB Persistence complete , Will respond to commands from other clients .
- bgsave command ,fork Create a child process to perform persistence , The main process is only in fork There is a brief blockage in the process , After the child process is created , The main process can respond to client requests . The subprocess writes the data set to the temporary file first , After writing successfully , Replace the previous file , Compress storage with binary .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-aSYTRkbb-1658827183649)(network-img/image-20220726144214834.png)]](/img/c1/d9b6171be452bc7f7f9e05e7e29d2a.png)
fork Underlying implementation :
The data of process operation is stored in physical memory , But all processes cannot directly manipulate physical memory , Instead, the operating system allocates virtual memory to each process , Virtual memory maps to physical memory , Processes can only manipulate virtual memory , The operating system maintains virtual memory through page tables , The process reads and writes data to physical memory based on the page table .fork The process is not to copy a copy of the memory data to the child process , This slow speed affects the efficiency of the main process , Instead, copy the page table of the main process to the child process , And set the data to read-only , In this way, the child process can also operate the same piece of physical memory as the main process , So as to realize the space sharing with the main process . There is no need to copy all data to child processes , Fast , The main process has a short blocking time . Then the subprocess can read the data in memory according to the page table and write it to disk , Replace the old backup file .
When the sub process is backed up, the main process changes the data process , It's using copy-on-write technology :
- When the main process performs a read operation , Access shared memory
- When the main process performs a write operation , Will involve the memory page of the write operation , Write on the newly copied memory page
RDB The way bgsave Basic flow :
- fork The main process gets a child process , Shared memory space
- The subprocess reads the memory data and writes new data RDB file
- New use RDB Replace the old file with RDB file .
RDB The shortcomings of ?
because RDB Long execution interval , two RDB In case of downtime when writing data between , There is a risk of data loss
fork Subprocesses 、 Compress 、 Write RDB The files are time-consuming
AOF
AOF Its full name is Append Only File( Additional documents ).Redis Each write command will be recorded in an additional way AOF file
1> AOF The default is off , Need modification redis.conf Configuration file to open AOF:
# Open or not AOF function , The default is no
appendonly yes
#AOF The name of the document
appendfilename "appendonly.aof"
2> AOF Synchronization strategies , It can be done by redis.conf File to configure :
# Indicates that every time a write command is executed , Immediately record to AOF file , Lose at most — strip
appendfsync always
# After the write command is executed, put it first AOF buffer , Then it means every 1 Seconds to write buffer data to AOF file , Is the default scheme , Once the system goes down , Then the modified data will be lost in one second
appendfsync everysec
# After the write command is executed, put it first AOF buffer , It's up to the operating system to decide when to write the contents of the buffer back to disk , More data may be lost
appendfsync no
| Configuration item | When to brush the disk | advantage | shortcoming |
|---|---|---|---|
| Always | Synchronous brush set | High reliability , Hardly lose data | High performance impact |
| everysec | Swipe the disk every second | Moderate performance | At most 1S data |
| no | Operating system control | Best performance | Poor reliability , A large amount of data may be lost |
3> AOF rewrite :AOF Because it's a record command , File than RDB Much bigger . and AOF Will record for the same key Multiple write operations , But only the last write operation makes sense . Through execution bgrewriteaof command , It can make AOF The file performs the rewriting function , Achieve the same effect with the least number of commands .

Redis It will also be automatically rewritten when the threshold is triggered AOF file . The threshold can also be in redis.conf Middle configuration :
#AOF If the file grows more than the last time, the rewriting will be triggered
auto-aof-rewrite-percentage 100
#AOF What is the minimum size of the file to trigger rewriting
auto-aof-rewrite-min-size 64mb
Mixed mode
AOF | RDB contrast :
| RDB | AOF | |
|---|---|---|
| Persistence mode | Snapshot the whole memory regularly | Record every command executed |
| Data integrity | Incomplete , Lost between backups | Relatively complete , Depending on the disk brushing strategy |
| file size | There will be compression , Small file size | Record orders , Large file size |
| Downtime recovery speed | Soon | slow |
| Data recovery priority | low , Data integrity is not as good as AOF | high , Higher data integrity |
| System resource usage | high , A lot of CPU And memory consumption .fork Subprocesses 、 Compress 、 Write RDB The files are time-consuming , When the data set is large , It may cause the whole server to stop serving for hundreds of milliseconds , even to the extent that 1 Second . | low , Mainly disk IO resources , but AOF Rewriting takes up a lot of CPU And memory resources |
| Use scenarios | Can tolerate several minutes of data loss , Pursue faster startup speed | Scenarios that require high data security |
RDB and AOF Each has its own advantages and disadvantages , Use RDB Persistence carries the risk of data loss , But the recovery speed is fast , While using AOF Persistence ensures data integrity , But it will be slow to recover data . So from the redis4 After that, a new hybrid AOF and RDB The pattern of :RDB Make a full backup , stay RDB Write operations during backup are recorded to AOF In file , namely AOF Do incremental persistence .
When the rewriting policy is satisfied or the rewriting is triggered manually ,fork A child thread stores memory data in RDB Write in binary format AOF File header , Those that are executed after the rewrite operation is executed redis command , with AOF The persistence method is appended to AOF End of file .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-QNZdLzfL-1658827183650)(network-img/image-20220726170705992.png)]](/img/88/73a90f29bbe81889f04e3c9bd1f526.png)
In this case , When the service is restarted, it will pass RDB File to rebuild content , Reuse AOF To re execute the recent write instruction , The data integrity is guaranteed , It also improves the performance of recovery .
aof-rdb The persistence mechanism is not enabled by default , Need to be in redis.conf Configuration is enabled in the configuration file , And open AOF Persistence .
# Hybrid persistence switch
aof-use-rdb-preamble yes
边栏推荐
- 【2022 牛客第二场J题 Link with Arithmetic Progression】三分套三分/三分极值/线性方程拟合最小二乘法
- max_pool2d(): argument ‘input‘ (position 1) must be Tensor, not NoneType
- 酒店vr全景展示拍摄提供更多合作和洽谈的机会
- Thread Foundation
- 机器人工程是否有红利期
- 数据湖(十七):Flink与Iceberg整合DataStream API操作
- QFileDevice、QFile、QSaveFile、QTemporaryFile
- 关于权重衰退和丢弃法
- 20 soul chicken soup beautiful sentences, sentence by sentence warm heart!
- C # set TextBox control not editable
猜你喜欢

MySQL事务的ACID特性及并发问题实例分析

工程地质实习-工程地质 题集

Win11黑色桌面背景如何解决?

Color recognition method and exploration based on MATLAB

【SAML SSO解决方案】上海道宁为您带来SAML for ASP.NET/SAML for ASP.NET Core下载、试用、教程

静态博客搭建工具汇总

Tungsten Fabric SDN — BGP as a Service

Original title of Blue Bridge Cup

Stm32f407 ------- FPU learning

The test post changes jobs repeatedly, jumping and jumping, and then it disappears
随机推荐
Color recognition method and exploration based on MATLAB
Qt官方示例:Fridge Magnets Example(冰箱贴)
阿里云国际版邮件服务套餐购买流程
Which of the four solutions of distributed session do you think is the best?
Web server
谈一谈百度 科大讯飞 云知声的语音合成功能
c#——switch case语句
Stop paging with offset and limit. The performance is too poor!
Redis实现分布式锁
[2022 Niuke multi school 2 K link with bracket sequence I] bracket linear DP
The digital twin smart building visualization platform realizes the integration of enterprise and public services in the park
力扣(LeetCode)208. 实现 Trie (前缀树)(2022.07.27)
《工程电磁场导论》课后习题附答案
stm32F407-------FPU学习
Shell:资源监控脚本和高负载报警
The object array is converted to string and then separated by strings, including the competition to select the children of a field, or the sum,
Decision tree and random forest learning notes (1)
决策树与随机森林学习笔记(1)
静态博客搭建工具汇总
Uniapp——拨打电话、发送短信