"58 Shen Jian Architecture Series" DB master-Slave Conformance architecture Optimization 4 ways

Source: Internet
Author: User

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.

Scenario 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

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

Scenario three (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

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

Summarize

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

The first 3 programmes were share on the database Conference (DTCC2016) this year, and relevant materials can be downloaded online. The 4th option is a good way for other classmates to share on the scene, thanks to the classmate.

"The article is reproduced from the public number" architect's Road "

"58 Shen Jian Architecture Series" DB master-Slave Conformance architecture Optimization 4 ways

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.