当前位置:网站首页>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.rdbAll 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 .
边栏推荐
- Mui mixed development - when updating the download app, the system status bar displays the download progress
- Learning notes of balanced binary tree -- one two pandas
- How to stop PHP FPM service in php7
- SDN basic overview
- 对绘制丘岭密度图ridge plot的详细说明、重叠核密度估计曲线overlapping densities、FacetGrid对象、函数sns.kdeplot、函数FacetGrid.map
- Login registration
- P6154 wandering (memory search
- P1434 [show2002] skiing (memory search
- C language: how to give an alias to a global variable?
- 全志V3S环境编译开发流程
猜你喜欢

oracle问题,字段里面的数据被逗号隔开,取逗号两边数据

redis-1. Install redis with pictures and texts

Compilation and development process of Quanzhi v3s environment

Un des backtraders du cadre de quantification lit l'analyseur

Raspberry school advanced development - "writing of IO port driver code" includes bus address, physical \u virtual address and bcm2835 chip manual knowledge

Compare advantages and disadvantages of DFS and BFS and name vocabulary

对绘制丘岭密度图ridge plot的详细说明、重叠核密度估计曲线overlapping densities、FacetGrid对象、函数sns.kdeplot、函数FacetGrid.map

RT-Thread 模拟器 simulator LVGL控件:button 按钮样式

How worker threads in the thread pool are recycled

Department store center supply chain management system
随机推荐
C # related knowledge points
How idea breaks point debugging
mysql中时间字段 比较时间大小
Login registration
EF CORE执行SQL语句
Real time lighting of websocket server based on esp32cam
About database: pgadmin4 editing SQL window
Issues related to C # delegation and events
关于c#委托、事件相关问题
How is it that the income of financial products is zero for several consecutive days?
考研英语
Personal JS learning notes
5. interrupts and exceptions
FTP_ Manipulate remote files
redis-4. Redis' message subscription, pipeline, transaction, modules, bloom filter, and cache LRU
redis-0. Introduction to redis and NiO principle (random talk)
25个国内外文献数据库
redis-2. Redis string type & bitmap
A solution to the problem that there is always a newline character when C merges multiple RichTextBox contents
快速排序