当前位置:网站首页>Redis core technology and practice - learning notes (VI) how to achieve data consistency between master and slave Libraries
Redis core technology and practice - learning notes (VI) how to achieve data consistency between master and slave Libraries
2022-07-03 17:51:00 【Tom Kong】
One .Redis With high reliability
- Data loss as little as possible .AOF and RDB Guarantee Less data loss .
- The service should try to Less interruptions . increase Copy redundancy , Put a piece of data at the same time Save in multiple instances , Even if an instance fails , Other instances can provide external services .
How about the data between multiple instances Guarantee consistency
Redis Provides Master slave database mode , To ensure the consistency of data copies , Between master and slave Libraries Read / write separation The way .
Read operations | Both master and slave libraries can receive |
Write operations | Main library execution , The main library will Write synchronization To the slave Library |
Why take Read / write separation The way ?
If both master and slave libraries can receive Client read / write operation , that A data is modified three times before and after , Each modification request is sent to different instances , Execute... On different instances , that The copies of this data on these three instances are inconsistent , When reading this data, it is possible to read the old value . If you have to keep the data consistent on the three instances, you need to lock , Coordinate and modify between instances , It's going to cost extra .
Once the master-slave mode is adopted Read / write separation The pattern of , All data changes will only be made on the main database , There's no need to coordinate three instances . After the main database has the latest data , It will be synchronized to the slave library , such The data of master-slave database is consistent Of .
Two . The first synchronization between master and slave Libraries
When starting multiple Redis At instance time , They communicate with each other through replicaof(Redis 5.0 Before using slave of) Commands form the relationship between the master database and the slave database , After that, the first synchronization of the data will be completed in three stages .
for example , Existing instances 1(ip:127.0.0.1:6379) And examples 2(ip:127.0.0.1:6380)
We are in the instance of 2 On the implementation replicaof 127.0.0.1 6379 After this order , example 2 It becomes an example 1 Slave Library , And from the example 1 Copy data on .
- The first stage is The master-slave library establishes the connection , The process of negotiating synchronization , Is to prepare for full replication . In this step , Establish connection between master library and slave Library , And tell The main library is about to be synchronized , After the main warehouse confirms and replies , The main library can start synchronization .
- Send from the slave to the master pysnc command , Indicates that you want to synchronize data , The master library starts the replication according to the proved parameters .
- psync The command contains the of the main library runID And replication progress offset Two parameters .runId Is each Redis A random generated when the instance starts id, Used to identify this instance .
- When the slave and master are copied for the first time , I don't know the main library runId, So will runId Set to "?".offset This is set to -1, identification The first copy .
- Master library received psync After the order , Will use FULLRESYNC The response command takes two parameters : Main library runId And the current replication progress of the master library offset, Back to the slave Library .
- After responding from the library , These two parameters will be recorded .FULLRESYNC The response indicates that the first replication adopts full replication , The main library will put At present, all data is copied to the slave library .
Second stage
- The master database synchronizes data to the slave database . After receiving the data from the library, load the data locally . This process mainly depends on Memory snapshot generation RDB file .
- Main library execution bgsave command , Generate RDB file , Send the file to the slave Library , Received... From library RDB After the document , Clear the current database first , Again load RDB file , This is from the library through replication of Before the command starts to synchronize with the main library , There may be other data stored . in order to Avoid the impact of previous data , From the library you need Empty the database first .
- When the master database synchronizes data to the slave database, the master database will not be blocked , So that requests can be received normally , But of these requests The write operation is not recorded and will not be recorded to the generated RDB file . To ensure master-slave consistency , The main library will use special replication buffer, Record RDB All writes received after file generation .
The third stage :
- The main library will write the newly received write command in the second stage replication buffer, And send it to the slave library .
- When the master library is finished RDB After the file is sent , Will take this time replication buffer The modification operation in is sent to the slave library , Perform these operations from the library , Realize master-slave synchronization .
3、 ... and . The master-slave cascade mode shares the pressure of the master database during full replication
One time full replication requires two time-consuming operations for the main database : Generate RDB file and transmission RDB file .
If there are a lot of slave Libraries , Full replication with the main database is required , It will lead to The main library is busy fork Child process generation RDB file , Full data synchronization .
fork This operation will Block the main thread from processing normal requests , Which leads to The corresponding application request of the main library slows down . here , transmission RDB Files will occupy the network bandwidth of the main library , It will also put pressure on the use of main library resources .
Lord - from - from
adopt Lord - from - from The pattern generates the master library RDB And transmission RDB The pressure of the , With cascade The way Dispersed To Slave Library On .
When we deploy the master-slave cluster , Manually select one High memory resource allocation Slave Library , be used for Cascade other slave Libraries . then , Then select some from the library , Let them establish a master-slave relationship with the slave library just selected .
replicaof The selected slave Library IP 6379
They are finishing Full synchronization in the first stage in the future , Will always be Maintain a network connection , Through this connection, the main library will The subsequent commands received are synchronized to the slave library . This process is called Long connection command propagation , Avoid frequent The overhead of establishing connections .
Risk point : The network is disconnected or blocked . If the network is disconnected , Command propagation between master and slave libraries is not possible , The slave database data cannot be consistent with the master database , The client may read old data from the library .
Four . The network between master and slave libraries is disconnected and reconnected
Redis2.8 before | Slave Library Re full copy , Spending big . Full replication is to synchronize all data |
Redis2.8 in the future | The master-slave library adopts Incremental replication Keep syncing in the same way . Incremental replication will Master slave operation received during master slave operation Synchronize to slave database . |
Incremental replication , How to keep master-slave libraries synchronized :repl_backlog_buffer buffer
- After the master and slave libraries are disconnected , The main library will send the write commands received during disconnection , write in replication buffer, At the same time, write these commands to repl_backlog_buffer This buffer .
- repl_backlog_buffer It's a Ring buffer , Main library Meeting Record where you write , Slave Library Meeting Record where you read .
- The write and read positions of the master and slave libraries are together , This is their The starting position . With the Lord The library keeps receiving new writes , Its write position in the buffer will gradually deviate from the starting position . We usually use Offset to measure the size of this offset distance .
- The offset corresponding to the main library is master_repl_offset. The more writes the master library receives , The greater the value .
- After copying the write command from the library , Its read position in the buffer also began to gradually lower the starting position , here , Offset copied from library slave_repl_offset It's also increasing . Under normal circumstances, the offsets of the two are basically equal .
Restore connection from master-slave Library :
- The slave library will send to the master library psync command , And put your current slave_repl_offset Send to master library , The master library will judge its own master_repl_offset and salve_repl_offset The gap between .
- Main storehouse handle master_repl_offset and slave_repl_offset Between Command operation synchronization To the slave Library .
because repl_backlog_buffer It's a ring buffer , So when the buffer is full , The main library will continue to write , here , It will override the previous write operations , If reading from the library is slow , This will cause the unread operation from the library to be overwritten by the newly written operation from the main library . This leads to data inconsistency between master and slave databases .
How to avoid ?
adjustment repl_backlog_size This parameter ;
Buffer size = Write command speed of main database * Operation size - Master slave network transmission command speed * Operation size .
repl_backlog_size=2* Buffer size
Related exercises :
The main library writes per second 2000 Operations , The size of each operation is 2KB, The network transmits... Every second 1000 Operations , that 1000 Operations need to be cached , At least it needs to be 2MB Buffer space , Otherwise, the new command will overwrite the old operation . In order to cope with possible sudden pressure , hold repl_backlog_size Set to 4MB. The risk of master-slave inconsistency is greatly reduced .
If the number of concurrent requests increases , Twice the cache can't hold new operation requests , The master-slave database data is inconsistent .
- according to Redis Memory value increases repl_backlog_size
- Use slice clusters to share the request pressure of a single master database
One Redis The instance database should not be too large , The size of an instance can be increased A few GB Appropriate level , Reduce RDB The overhead of file generation, transfer and reload .
After class questions
AOF The recorded operation commands are more complete , Compared with RDB Less data lost , that , Why does replication between master and slave databases not use AOF Well ?
- RDB The content of the document is Compressed binary files ( Different data types have been optimized ), The file is very small , and AOF The file records the command of every write operation , The more write operations file It will become very Big , It also includes the same key Redundant operation .
- Master slave full data synchronization , transmission RDB The file can reduce the consumption of network bandwidth of the main database machine as much as possible , from Library loading RDB When you file , First, the documents are small , Reading the whole file is fast , The second is because RDB Files store binary data , Direct basis RDB The protocol can be used to parse and restore the data , Fast ,AOF You need to execute each write command in turn , The process is lengthy and the recovery speed is slow , So use RDB The cost of master-slave full synchronization is the lowest .
- Use AOF Do full synchronization , signify Must open AOF function , open AOF Select the file disk brushing strategy , Improper choice can seriously affect Redis performance . and RDB The snapshot generation is triggered only when scheduled backup and master-slave full synchronization of data are required . Many business scenarios that are not sensitive to data loss , In fact, you don't need to open AOF.
Extracurricular related issues :
1. Not after the master-slave library is disconnected , The main library will write the write operation to repl_backlog_buffer, As long as the library exists , This repl_backlog_buffer There will be . All write commands of the master library will be transmitted in except to the slave library repl_backlog_buffer Record a copy of , cached , Only these commands are pre cached , When disconnected from the library , Resend from library psync $master_runid $offset The main library is from $offset In the repl_backlog_buffer Find the location disconnected from the library and send from $offset After that Incremental data Just take it from the library .
边栏推荐
- [LINUX]CentOS 7 安装MYSQL时报错“No package mysql-server available“No package zabbix-server-mysql availabl
- Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
- WEB-UI自动化测试-最全元素定位方法
- c# . Net tool ecosystem
- 互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
- Design limitations of structure type (struct)
- QT学习日记9——对话框
- 面试官:值为 nil 为什么不等于 nil ?
- 数学公式(测试)
- Leetcode 108 converts an ordered array into a binary search tree -- recursive method
猜你喜欢
Micro service component sentinel console call
互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
Investigation on the operation prospect of the global and Chinese Anti enkephalinase market and analysis report on the investment strategy of the 14th five year plan 2022-2028
Hongmeng fourth training
Research on Swift
UE4 official charging resources, with a total price of several thousand
微服务组件Sentinel控制台调用
Getting started with deops
1147_ Makefile learning_ Target files and dependent files in makefile
Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
随机推荐
[combinatorics] recursive equation (special solution example 1 Hannover tower complete solution process | special solution example 2 special solution processing when the characteristic root is 1)
SSL / bio pour OpenSSL Get FD
Write a program to process a list container of string type. Find a special value in the container 9.27: and delete it if found. Rewrite the above procedure with deque container.
MySQL has been stopped in the configuration interface during installation
[combinatorics] recursive equation (the non-homogeneous part is an exponential function and the bottom is the characteristic root | example of finding a special solution)
Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
解决Zabbix用snmp监控网络流量不准的问题
Analysis report on production and marketing demand and investment forecast of China's PVC industry from 2021 to 2026
MySQL grouping query
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
How to install PHP on Ubuntu 20.04
Servlet specification Part II
QT学习日记9——对话框
Distributed task distribution framework gearman
Website with JS doesn't work in IE9 until the Developer Tools is activated
[Yu Yue education] family education SPOC class 2 reference materials of Shanghai Normal University
[mathematical logic] equivalent calculus and reasoning calculus of predicate logic (individual word | predicate | quantifier | predicate logic formula | two basic formulas | proposition symbolization
Play with fancy special effects. This AE super kit is for you
Leetcode540: a single element in an ordered array
分布式的任务分发框架-Gearman