first, the origin of demand
Most of the internet business is "read more write less" scenario, database level, read performance is often a bottleneck. For example, the industry usually adopts the database schema of "one master, multiple slave, read and write separation, redundant reading library" to improve the reading performance of the database.
One potential disadvantage of this architecture is that it is possible for business parties to read old data that is not up to date:
(1) The system first writes to the Db-master, writes the main library
(2) A short period of time for a read operation, read from the library, when the master-slave synchronization is not completed, so read to an old data
(3) Master-Slave synchronization completed
There is no way to solve or alleviate this kind of "due to master-slave delay caused by reading old data" problem, this is the focus of this article to discuss the issue.
Ii. Programme one (semi-synchronous replication)
inconsistency is due to the completion of the write, master-slave synchronization has a time difference, assuming that it is 500ms, this time difference has read requests fell from the library generated . Is there any way to do this, and when the master-slave synchronization is complete, the write request on the main library returns? The answer is yes, that's what you often call "semi-synchronous Replication" Semi-sync:
(1) The system first writes to the Db-master, writes the main library
(2) when the master-slave synchronization is completed, the request to write the main library returns
(3) read from the library, read the latest data (if the read request is completed first, write the request to complete, read "then" the latest data)
Advantages of the scheme: using the database native function, relatively simple
Scenario Disadvantage : The write request latency of the main library grows and throughput decreases
Three
Scenario two (forced read main library)
If you do not use the "increase from library" way to increase the read performance of the system, can read and write to the main library, so that there is no inconsistency:
Advantages of the scheme: No system modification is required on "consistency"
Solution Disadvantage : Only through the cache to improve the system's read performance, here to do the system transformation
Iv. Programme III (database middleware)
If you have database middleware, all database requests go to the middleware, this is the problem that the master never agrees:
(1) All the reading and writing go to the database middleware, typically, write requests are routed to the main library, and read requests are routed to the slave library
(2) record all the keys routed to the Write library, in the experience master-Slave synchronization time window (assuming 500ms), if there is a read request to access the middleware, it is possible from the library or the old data, the key to the read request on the route to the main library
(3) After the master-slave synchronization time is over, the read request of the corresponding key continues to be routed to the slave library
Program Benefits : Guaranteed Absolute Consistency
Solution Disadvantage : High cost of database middleware
Five
scenario four (cache record write key method)
Since the cost of database middleware is relatively high, is there a lower cost scheme to record a write request on one key of a library? It is easy to think of using caching when a write request occurs:
(1) A key on a library to write, recorded in the cache, and set the "Experience master and Slave synchronization Time" cache time-out, such as 500ms
(2) Modify the database
And when the read request occurs:
(1) First to the cache to see, corresponding to the library key has no relevant data
(2) If the cache hit, there is the relevant data, indicating that a write operation has occurred on this key, it is necessary to route the request to the main library to read the latest data
(3) If the cache miss, indicating that there have been no recent write operations on this key, the request is routed to the slave library, continue to read and write separation
Solution Advantages : Lower cost compared to database middleware
Scenario Disadvantage : In order to ensure "consistency", a cache component is introduced, and the database is read and written with one more step cache operation
Vi. Summary
In order to solve the problem of reading old data from master-slave database, there are four kinds of common schemes:
(1) Semi-synchronous replication
(2) Mandatory Read Master
(3) Database middleware
(4) Cache record write key
Original link: https://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=2651959442&idx=1&sn= Feb8ff75385d8031386e120ef3535329&scene=21#wechat_redirect
4 ways to optimize DB master-Slave conformance architecture