当前位置:网站首页>Database query optimization: master-slave read-write separation and common problems
Database query optimization: master-slave read-write separation and common problems
2022-06-28 01:39:00 【pbrong】
1. Basic principle of master-slave read-write separation
The access model of most systems is to read more and write less , The gap in the number of read and write requests can be several orders of magnitude .
According to some cloud vendors Benchmark Result , stay 4 nucleus 8G Running on the machine MySQL 5.7 when , Probably support 500 Of TPS and 10000 Of QPS.
When a single machine MySQL Unable to withstand excessive QPS when , Can form MySQL The master-slave read-write cluster is separated to allocate read requests to multiple slave nodes , To achieve horizontal expansion .
1.1. Master slave replication process
- Main library db Update event for (update、insert、delete) Written to binlog
- Initiate connection from library , Connect to the main library
- At this time, the main library creates a binlog dump thread, hold binlog Content sent from library
- After starting from the library , Create a I/O Threads , Read from the main library binlog Content and write to the relay log (relay log)
- And create a SQL Threads , from relay log Read the contents inside , from Exec_Master_Log_Pos The location starts executing the read update event , Write the update to slave Of db

After master-slave replication , We can write only to the main library when writing , Read only when reading data from the library , In this way, even if the write request will lock the table or lock the record , Nor will it affect the execution of read requests . At the same time , In the case of large read traffic , We can deploy multiple slave libraries to share the read traffic , This is what is said “ One master, many followers ” Deployment way .
1.2. Whether the slave node can be expanded infinitely
To cope with higher database reads QPS, Is it possible to expand the slave nodes infinitely to resist a large number of concurrency ? The answer is no .
Because as the number of slave libraries increases , Connected from the library IO More threads , The main library needs to create as many log dump Thread to handle the copy request , For the main database, the resource consumption is relatively high , At the same time, it is limited by the network bandwidth of the main database , So in practice , Generally, a main library can hang up to 3~5 A slave .
2. Master slave read / write separation mode -SDK And independence Proxy Deploy
The master-slave replication technology is used to replicate data to multiple nodes , It also realizes the separation of database reading and writing , At this time , The use of databases has changed . In the past, only one database address was needed , Now you need to use one master address and multiple slave addresses , And you need to distinguish between write operations and query operations , The complexity will increase more . In order to reduce the complexity of implementation , Many database middleware have emerged in the industry to solve the problem of database access , These middleware can be divided into two categories :SDK Class and independent Proxy Deployment class .
2.1.SDK class
Taobao TDDL( Taobao Distributed Data Layer) For and Sharding—JDBC As a representative , Embedded in the form of code running inside the application . You can think of it as a proxy for a data source , Its configuration manages multiple data sources , Each data source corresponds to a database , Maybe the main library , Maybe from the library .
When there is a database request , Middleware will SQL Statement to a specified data source to process , Then return the processing result to .SDK The advantage is that it is easy to use , No extra deployment costs , The disadvantage is that it is highly bound to the programming language , such as TDDL and Sharding-JDBC Only in Java Use in , It is also difficult to upgrade the version .
2.2.Proxy class
The other is the separately deployed agent layer scheme , This kind of scheme represents more , Such as the early Alibaba open source Cobar, be based on Cobar Developed Mycat,360 Open source Atlas, Meituan's open source is based on Atlas Developed DBProxy wait .
This kind of middleware is deployed on a separate server , Business code uses it as if it were using a single database , In fact, it manages many data sources internally , When there is a database request , It will be right SQL Make necessary rewriting of the statement , Then send it to the specified data source .
It generally uses standard MySQL Communication protocol , So it can support multiple languages . Because it is deployed independently , Therefore, it is more convenient to maintain and upgrade , It is suitable for large and medium-sized teams with certain operation and maintenance ability . Its flaws are all SQL Statements need to cross the network twice : From application to agent layer and from agent layer to data source , So there will be some loss in performance .
3. Problems and solutions of master-slave read-write separation
The master-slave read-write separation can make the database bear more reading pressure , But problems will follow : The master-slave replication is asynchronous , There are delays : The slave database consumes transit logs faster than the master database produces logs binlog It's slow .
Generally speaking , You can try to improve the slave library specification (CPU、 Memory ) To reduce the delay time , But objectively speaking, the master-slave delay will certainly exist , Especially when there is a large amount of writing , There is a problem at this time :
Read data from the library immediately after the data is written to the main library , At this point, the data may not have been copied , Cause data acquisition failure or data inconsistency .
The main idea of the solution to the inconsistency of master-slave delayed reading :
- The first scheme is data redundancy : In a scenario where you need to read and write data immediately , Do not pass data ID Database search , Instead, it directly transmits the whole latest data .
- The second option is to use caching : When writing to the main library , Write this data into Redis in , When you need to query immediately, first look up the cache to get the latest data .
- The third scheme is to query the main database : When inconsistent data is not allowed , It can be directly routed to the main database query , But this practice will put a lot of pressure on the main database , Current limiting shall be done well , Avoid hanging on to the main database .
边栏推荐
- [Niuke discussion area] Chapter 4: redis
- PostgreSQL设置自增字段
- Deep parsing of kubernetes controller runtime
- 【嵌入式基础】内存(Cache,RAM,ROM,Flash)
- 电商转化率这么抽象,到底是个啥?
- Collection de cas d'effets spéciaux en cliquant sur la souris de la page Web
- 【牛客讨论区】第四章:Redis
- 零基础多图详解图神经网络
- Google Earth engine (GEE) -- an error caused by the imagecollection (error) traversing the image collection
- PV操作原语
猜你喜欢

药物发现综述-01-药物发现概述

Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)

Ten thousand words long article understanding business intelligence (BI) | recommended collection

评价——灰色关联分析

Proe/Creo产品结构设计-钻研不断

【说明】Jmeter乱码的解决方法

Message Oriented Middleware for girlfriends

Download, configuration and installation of MySQL

Neural network of zero basis multi map detailed map

数据库的新选择 Amazon Aurora
随机推荐
【开源】开源系统整理-考试问卷等
MySQL - function
【DNS 解析】将Name.com的域名接入DNSPod解析
Li Kou today's question -522 Longest special sequence
Adobe Premiere基础-常用的视频特效(裁剪,黑白,剪辑速度,镜像,镜头光晕)(十五)
Zhang Fan: the attribution of flying pig after advertising based on causal inference technology
awk注意的几个问题
Xctf attack and defense world misc wage earner advanced zone
Is there any risk in opening an account for flush stock? Is it safe for flush to open an account
模块化开发
What are cookies and the security risks of v-htm
从小到大为何一谈学习就愁眉苦脸
评价——灰色关联分析
Taro--- day1--- construction project
What is a through-hole conductive slip ring?
Drug interaction prediction based on learning size adaptive molecular substructure
什麼是數字化?什麼是數字化轉型?為什麼企業選擇數字化轉型?
SPuG - lightweight automatic operation and maintenance platform
Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)
【牛客讨论区】第四章:Redis