当前位置:网站首页>Redis learning journey - persistence
Redis learning journey - persistence
2022-06-13 07:28:00 【Zhao JC】
Redis Learning journey -- Persistence
Redis Is a memory database , If you do not save the database state in memory to disk , So once the server process exits , The database state in the server also disappears , therefore Redis Provides persistent functionality .
RDB(Redis DataBase)
rdb What is it?
In master-slave replication ,rdb It's standby ! Backup to the slave !
The core : Write the data set snapshot in memory to disk within the specified time interval , That's what jargon says Snapshot snapshot , When it recovers, it reads the snapshot file directly into memory .
- Detailed process :Redis Will create... Separately (fork) A subprocess to persist , Write the data to a temporary file first , When the persistence process is over , Replace the last persistent file with this temporary file . The whole process , The main process is not going to do anything IO Operation of the . This ensures extremely high performance . If large-scale data recovery is needed , And it's not very sensitive to the integrity of data recovery , that RDB The way is better than AOF It's more efficient .RDB The disadvantage is that the data may be lost after the last persistence . Our default is
RDB, In general, you don't need to modify this configuration ! - Sometimes in the production environment, we Backup this file !
- rdb The saved file is
dump.rdb
All of them are configured in our configuration file !
test :
- First the dump.rdb File deletion , Then modify it once in a minute , see dump.rdb Whether it is generated , close Redis Then check whether the set value exists
- Use flushall after , Look at the build dump.rdb file
Trigger mechanism
- 1、save When the rules are satisfied , Will automatically trigger rdb The rules
- 2、 perform flushall command , It will trigger our rdb The rules !
- 3、 sign out redis, There will be rdb file !
How to restore rdb file
- 1、 Only need to rdb The documents are in us redis Just start the directory ,redis It will check automatically when it starts dump.rdb Recover the data !
- 2、 See where you need to be
127.0.0.1:6379> config get dir
1) "dir"
2) "/usr/local/bin" # If it exists in this directory dump.rdb file , The data in it will be recovered automatically when it is started
Advantages and disadvantages
advantage :
- 1、 Suitable for large-scale data recovery !
- 2、 The integrity of the data is not high !(60 minute 5 Time modification , If 59 It went down in minutes , The last modified data cannot be obtained )
shortcoming
- 1、 Need a certain time interval process operation ! If redis Unexpected downtime , There is no need to modify the data for the last time !
- 2、fork Process time , It will occupy a certain amount of memory space !!
AOF(Append Only File)
aof What is it?
- Schematic diagram
The core : Record all our orders ,history, When restoring, execute the file all over again !
- Detailed process : Log every write operation in the form of a log , take Redis All instructions executed are recorded ( Reading operation does not record ), Only additional documents are allowed But you can't rewrite the file ,redis At the beginning of startup, it will read the file and rebuild the data , In other words ,redis In case of restart, execute the write instruction from the front to the back according to the contents of the log file to complete the data recovery
aof What is kept is appendonly.aof file
Related configuration
- Open or not aof
Not on by default , We need to configure it manually ! We just need to put appendonly Change it to yes It opens. aof!
restart ,redis It's going to take effect !
- Some writing strategies
- Rewritten rules
aof The default is unlimited appending of files , The file will get bigger and bigger !
If aof File is larger than 64m, It's too big ! fork A new process to rewrite our files !
Fault tolerance mechanism
If this aof There is an error in the file , Now redis Can't it start , We need to fix this aof file
redis Provides us with a tool redis-check-aof --fix
- modify appendonly.aof
- Restart the service and report an error
- use redis-check-aof --fix Tool repair
- View the repair results
Advantages and disadvantages
# appendfsync always # Every time you make a change, it will sync. Consumption performance
appendfsync everysec # Once a second sync, You may lose this 1s The data of !
# appendfsync no # Don't execute sync, At this time, the operating system synchronizes its own data , The fastest !
advantage :
- 1、 Every change is synchronized , The integrity of the document would be better !
- 2、 Synchronize once per second , One second of data could be lost
- 3、 No synchronization , The most efficient !
shortcoming : - 1、 Relative to data files ,aof Far greater than rdb, It's faster than rdb slow !
- 2、Aof It's more efficient than rdb slow , So we redis The default configuration is rdb Persistence !
Redis How to expand the capacity of persistent data and cache ?
- If Redis Used as a cache , Dynamic capacity expansion using consistent hash
- If Redis Used as a persistent store , Use Redis Cluster de capacity expansion
Expand
1、RDB Persistence mode can snapshot your data in a specified time interval
2、AOF Persist to record every write to the server , These commands are reexecuted when the server restarts to restore the original data ,AOF Command to Redis Protocol append saves each write to the end of the file ,Redis Also able to AOF The file is rewritten in the background , bring AOF The size of the file should not be too large .3、 Cache only , If you only want your data to exist when the server is running , You can also use no persistence
4、 Open two persistence methods at the same time
- under these circumstances , When redis It will be loaded prior to restart AOF File to restore the original data , Because in general AOF The data set saved by the file is better than RDB The data set of the file should be complete .
- RDB The data is not real-time , When using both, the server will only look for AOF file , Do you want to use only AOF Well ? The author suggests not to , because RDB Better for backing up databases (AOF It's not easy to backup in constant change ), A quick restart , And there won't be AOF May be latent Bug, Keep it as a means in case .
5、 Performance Suggestions - because RDB The documents are for backup purposes only , The advice is only in Slave On persistence RDB file , And as long as 15 One minute backup is enough , Only keep save 900 1
This rule . - If Enable AOF
, The advantage is that in the worst case, it will only lose no more than two seconds of 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 AOFrewrite The frequency of ,AOF Overridden base size default 64M Is too small , It can be set to 5G above , Default over original size 100% The size rewrite can be changed to the appropriate value . - If you don't Enable AOF , Only by Master-Slave Repllcation 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 , Microblogging is this kind of Architecture .
边栏推荐
- 理財產品連續幾天收益都是零是怎麼回事?
- Try to use renderdoc to view the shader code of UE
- SDN basic overview
- Real time lighting of websocket server based on esp32cam
- 检测循环数“142857“
- Department store center supply chain management system
- Postgraduate entrance examination English
- Relevant knowledge under WinForm
- Deploy RDS service
- Through the function seaborn cubehelix_ Palette build order palette
猜你喜欢
Socket programming 2:io reuse (select & poll & epoll)
Evolution in the digital age
TCP协议的三次握手过程和四次挥手过程以及为什么要这样? ------一二熊猫
redis-1. Install redis with pictures and texts
How idea breaks point debugging
Real time lighting of websocket server based on esp32cam
Tidb data migration (DM) Introduction
Related operations under Oracle Database
比较DFS和BFS的优点和缺点及名称词汇
微隔离(MSG)
随机推荐
Database outline
Raspberry school advanced development - "writing of IO port driver code" includes bus address, physical \u virtual address and bcm2835 chip manual knowledge
25 domestic and foreign literature databases
What languages can be decompiled
【splishsplash】重复输出splashsurf的脚本
powerdisgner逆向生成oracle数据模型
Relevant knowledge under WinForm
考研英语
RT-Thread 模拟器 simulator LVGL控件:button 按钮样式
Local file upload FTP or remote directory
. Net code to implement get request and post request
Socket programming 2:io reuse (select & poll & epoll)
First graphical interface (modified version)
8. process status and transition
6. system call
Why should two judgment expressions in if be written in two lines
The password does not take effect after redis is set
RT thread simulator lvgl control: switch switch button control
关于oracle的函数。
About database: pgadmin4 editing SQL window