Four Methods for DB master-slave consistency Architecture Optimization and four methods for db master-slave consistency

Source: Internet
Author: User

Four Methods for DB master-slave consistency Architecture Optimization and four methods for db master-slave consistency
I. Origin of demand

Most Internet businesses use "read-write-less" scenarios. At the database level, read performance often becomes a bottleneck. For example, the industry usually uses "one master and multiple slave databases, read/write separation, and redundant reading databases"Database architectureTo improve the read performance of the database.


Potential of this architectureDisadvantagesYes, the business party may not read the latest old data:


(1) the system first performs a write operation on the DB-master to write to the master database.

(2) in a short period of time, a read operation is performed concurrently to read the slave database. At this time, the master-slave synchronization is not completed, so an old data is read.

(3) master-slave synchronization is completed

Is there a way to solve or mitigate such problems?"Reading old data due to Master/Slave latency"This is a concentrated issue in this article.

Ii. solution 1 (semi-synchronous replication)

The inconsistency is caused by the time difference between the master and slave Synchronization After the write is complete. For example, if the time difference is 500 ms, the time difference is generated when the read request falls to the slave database.. Is there a way to do this? After the master-slave synchronization is complete, will write requests on the master database be returned? The answer is yes, that is, "semi-synchronous replication" semi-sync:


(1) the system first performs a write operation on the DB-master to write to the master database.

(2) After the master-slave synchronization is completed, the request to write the master database is returned.

(3) read from the slave database and read the latest data (if the Read Request is completed first and the write request is completed, the latest data is read)

Advantages: Simple to use database native functions

Disadvantages: The Write Request latency of the master database increases and the throughput decreases.

III, Solution 2 (Force reading the master database)

If you do not use the "add slave Database" method to increase the read performance of the system, the read and write operations can all be performed on the master database, so that no inconsistency will occur:


Advantages: System transformation is not required for "consistency"

Disadvantages: Only the cache can be used to improve the system's read performance. system transformation is required here.

Iv. solution 3 (Database middleware)

If database middleware is available, all database requests are sent through middleware. the inconsistency between the master and slave databases can be solved as follows:


(1) All reads and writes are performed in the database middleware. Generally, write requests are routed to the master database, and read requests are routed to the slave database.

(2) Record all the keys routed to the write database. In the time window of the master-slave synchronization experience (assumed to be 500 ms), if there is a Read Request access middleware, at this time, it is possible to route the read requests on the key from the database or the old data to the master database.

(3) After the master-slave synchronization time is over, the read requests of the corresponding key are routed to the slave database.

Advantages: Can guarantee absolute consistency

Disadvantages: High cost of database Middleware

V, Solution 4 (Cache record write key method)

Since the cost of database middleware is relatively high, is there a lower-cost solution to record a write request on a key of a database? It is easy to think of using the cache. When a write request occurs:


(1) Write a key in a database, record it in the cache, and set the cache timeout time for "experience master-slave synchronization time", for example, 500 ms

(2) modify the database

 

When a read request occurs:


(1) first go to the cache to check whether the corresponding key of the database has relevant data.

(2) If the cache hit contains related data, it indicates that a write operation has just occurred on the key. At this time, you need to route the request to the master database to read the latest data.

(3) If the cache miss occurs, it indicates that no write operation has been performed on this key recently. At this time, the request is routed to the slave database and read/write splitting continues.

Advantages: Relatively low cost compared with database Middleware

Disadvantages: To ensure "consistency", a cache component is introduced and one more cache operation is performed during database read/write operations.

 

Vi. Summary

To solve the problem of reading old data from a master-slave database, there are four common solutions:

(1) semi-synchronous Replication

(2) force read Master

(3) database Middleware

(4) cache record write key

 

 

Https://mp.weixin.qq.com/s? _ Biz = MjM5ODYxMDA5OQ ==& mid = 2651959442 & idx = 1 & sn = feb8ff75108d8031386e120ef35000029 & scene = 21 # wechat_redirect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.