"Mysql - which pits have separate read and write? "

A: separate read and write

  - concept

    -  to read and write separation main goal is to share the pressure of the main library .

 

  - The basic architecture

    -     - 

 

II: two read-write separate architectural features

  - Client direct scheme

    - because the less one proxy forwarding, so the query performance a little bit better, and the overall structure is simple, troubleshoot problems more easily.

    - but this scheme, due to understand the back-end deployment details, so standby switching, database migration and other operations in the event of the time, the client will be perceived to, and need to adjust the database connection information .

    - You might think that the client too much trouble, a lot of redundant information, ugly architecture.

    - In fact, it may not, in general such a framework must be accompanied by a responsible for managing the back-end components, such Zookeeper, try to make the business end only focus on business logic development.

 

  - proxy program

    - with proxy architecture for the client more friendly . The client does not need to focus on the details of the back-end , connection maintenance, back-end information and maintenance work is done by the proxy.

    - but then, the requirements for the back-end maintenance team will be higher. Moreover, proxy also need to have high-availability architecture.

    - Therefore, the overall band proxy architecture is relatively complex. 

 

Three: What is "overdue read"?

  -   " In an expired state of the system will read from the library " phenomenon, let us call it " expire read ."

 

Four: Processing "expired Reading" program?

  - Strong take the main library program

  - sleep program;

  - Analyzing standby program without delay;

  - semi-sync with the program;

  - other primary site database programs;

  - and so GTID program. 

 

Five: go strong main library program

  - Principle

    -  Forced to go it is actually the main library program, the queries do classification .

    - to have to get the latest result of the request, forcing sends it to the main library.

      - For example, on a trading platform, the seller after release of goods, is about to return to the main page to see if the release of goods successful.

      - Well, this request needs to get the latest results, it must take the main library.

    - For a request to read the old data can only be sent to it from the library. 

      -  In this trading platform, buyers to visit the shop page, even a few seconds later to see the latest release of goods, it is also acceptable. So, such requests can go from the library.

 

  - Problem

    - The program's biggest problem is that sometimes you'll run into "all queries can not be expired read" demands , such as some financial business.

    - In this case, you have to give up separate read and write, all the pressure in the main library to read and write, it is equivalent to giving up the scalability.

 

Six: sleep program

  - Principle

    -  the main library update after reading from the library to sleep before it . Specific program is similar to the implementation of a select sleep (1) command.

    -  Assuming that this scheme is that in most cases the primary and delay within one second, do a sleep can make a big probability to get the latest data.

      - such as after the client complete the next one to do 1s of loading, in fact, is in effect from the library waiting for the 1s.

 

  - Problem

    - sleep program does solve the problem of overdue read certain scenes.

    - but, strictly speaking, this program problems is inaccurate.

    - The imprecise contains two meanings:

      - if the query request would be 0.5 seconds to get the correct result from the library, and the like will be 1 second;

      - If the delay is more than one second, there will still expire read. 

 

Seven: Analyzing standby program without delay

  - Principle

    - By show slave status; results

    - 

 

  - Comparative  seconds_behind_master standby without delay determination (accuracy S)

    -  before each execution of the query request from the library, first determine whether seconds_behind_master already equal to zero.

    - if not equal to 0, it must wait until this parameter to 0 to execute queries.

 

  - Comparative standby point is determined without delay

    -  Master_Log_File and Read_Master_Log_Pos, represents the latest site of the main library read;

    -  Relay_Master_Log_File and Exec_Master_Log_Pos, represents the latest sites prepared by the library to perform.

    -  If

      - Master_Log_File == Relay_Master_Log_File

      - Read_Master_Log_Pos == Exec_Master_Log_Pos

      -  which sets of values are identical, it indicates that the received synchronization log has been completed.

 

  - 对比  GTID 判断主备无延迟

    - Auto_Position=1 ,表示这对主备关系使用了 GTID 协议。

    - Retrieved_Gtid_Set,是备库收到的所有日志的 GTID 集合;

    - Executed_Gtid_Set,是备库所有已经执行完成的 GTID 集合。

    - 如果这两个集合相同,也表示备库接收到的日志都已经同步完成。 

 

  - 小结

    - 虽然等待无延迟是可以解决问题,但是可能存在主备一直不一致,导致备库无法读取的问题。

    - 对比位点和对比 GTID 这两种方法,都要比判断 seconds_behind_master 是否为 0 更准确。

    - 这几种办法并没有达到 “精确” 的程度,可能存在  主库已经执行,但是还没有发送给备库的情况,导致过期读。(通过 semi-sync 解决)

 

八: 等主库点位方案 / 等GTID方案

  - 这两个方案都是 在备库执行,等待一定时间,如果在时间内 主库点位 / GTID 同步,则在备库执行,否则到主库执行。

 

九:小结

  - 这几种方案中,有的方案看上去是做了妥协,有的方案看上去不那么靠谱儿,但都是有实际应用场景的,你需要根据业务需求选择。

  - 即使是最后等待位点和等待 GTID 这两个方案,虽然看上去比较靠谱儿,但仍然存在需要权衡的情况。

    - 如果所有的从库都延迟,那么请求就会全部落到主库上,这时候会不会由于压力突然增大,把主库打挂了呢?

  - 其实,在实际应用中,这几个方案是可以混合使用的。

  - 比如,先在客户端对请求做分类,区分哪些请求可以接受过期读,而哪些请求完全不能接受过期读;然后,对于不能接受过期读的语句,再使用等 GTID 或等位点的方案。

  - 但话说回来,过期读在本质上是由一写多读导致的。

  - 在实际应用中,可能会有别的不需要等待就可以水平扩展的数据库方案,但这往往是用牺牲写性能换来的,也就是需要在读性能和写性能中取权衡。 

Guess you like

Origin www.cnblogs.com/25-lH/p/11057936.html