当前位置:网站首页>Want to do read-write separation, give you some small experience
Want to do read-write separation, give you some small experience
2020-11-06 01:15:00 【Yin Jihuan】
Read write separation is the most common technology to improve the performance of data access in applications , When there are more and more users , More and more visits , Single node database will inevitably encounter performance bottleneck . Many scenes are basically read more and write less , Therefore, it is natural to add multiple slave nodes to share the pressure of the master node .
After the application access read-write separation , There will inevitably be some unexpected problems , This article mainly introduces some common problems , If you have any other questions, please leave a message .
The articles before the sub database and sub table can be viewed :http://mp.weixin.qq.com/mp/homepage?__biz=MzIwMDY0Nzk2Mw==&hid=4&sn=1b96093ec951a5f997bdd3225e5f2fdf&scene=18#wechat_redirect
Realization way
For the use of read-write separation , There are mainly two ways , Client mode and proxy mode .
The client mode can be used by itself Spring Self contained AbstractRoutingDataSource To achieve , It can also be implemented in an open source framework , such as Sharding-JDBC.
Proxy mode needs to write proxy service to manage all nodes , The application does not need to pay attention to the information of multiple database nodes . You can do it yourself , You can also use open source frameworks , You can also use commercial cloud services .
Data delay
When it comes to data latency , You have to understand the principle of master-slave architecture . The addition, deletion and modification of data are performed on the main database , The query is executed on the slave library , When the data is just inserted into the main database , And when I go to check immediately , Most likely, the data has not yet been synchronized to the slave library , There will be no query .
Like I published articles on some websites before , After publishing, jump to the list page , No new articles were found , Refresh the next page again , From this point of view, this is the phenomenon caused by data delay after read-write separation .
Forced routing
Should data delay be solved , It generally depends on the business scenario . For business scenarios with less real-time requirements , Allow a certain delay , For real-time scenarios , The only way is to query directly from the main database , In this way, the latest data just inserted or modified can be read in time .
Forced routing is a solution , That is to force the read request to the main database for query . Most middleware supports Hint grammar /FORCE_MASTER/ and /FORCE_SLAVE/.
With Sharding-JDBC give an example , The framework provides HintManager To force routing , Use as follows :
HintManager hintManager = HintManager.getInstance();
hintManager.setMasterRouteOnly();
For ease of use , Suggest encapsulating a comment , Annotate business methods that require real-time queries , Set mandatory routing through facets .
Annotations use :
@MasterRoute
@Override
public UserBO getUser(Long id) {
log.info(" Query the user [{}]", id);
if (id == null) {
throw new BizException(ResponseCode.PARAM_ERROR_CODE, "id Can't be empty ");
}
UserDO userDO = userDao.getById(id);
if (userDO == null) {
throw new BizException(ResponseCode.NOT_FOUND_CODE);
}
return userBoConvert.convert(userDO);
}
Section settings :
@Aspect
public class MasterRouteAspect {
@Around("@annotation(masterRoute)")
public Object aroundGetConnection(final ProceedingJoinPoint pjp, MasterRoute masterRoute) throws Throwable {
HintManager hintManager = HintManager.getInstance();
hintManager.setMasterRouteOnly();
try {
return pjp.proceed();
} finally {
hintManager.close();
}
}
}
The transaction operations
Read requests in transactions , Go to the main library or the slave Library ? For this question , The simplest way is to go to the main database for all transactions , There are often inserts in transactions , And then re query the scene , The transaction is not committed at this time , Even if the synchronization is fast , There is no data from the database , So we can only go to the main library .
But there are also requests , Just query the slave library , If routing is enforced for all operations in a transaction , It's not very good either . stay Sharding-JDBC It's very good , about In the same thread and database connection , If there is a write operation , Later read operations are read from the main library , For data consistency . If we have a query request before the data is written , Or from the library , Reduce the pressure on the main reservoir .
Dynamic forced routing
In the process of function development, it is decided which interfaces should be forced to go to the main library , At this time, we will control the routing in the code , This is the custom annotation mentioned above . If some are not added , However, when running online, it is found that you still need to go to the main database , At this time, we need to change the code and redistribute it .
Dynamic forced routing can be implemented in combination with configuration center , Determine which interfaces are forced to route by configuration , And then in Filter Pass through HintManager To set up , Avoid code changes, restart .
You can also use dynamic routing configuration that is accurate to the business method level .
Traffic distribution
Scene one :
Suppose you have a master node , Two slave nodes , More read requests , The pressure of the two slave nodes is a little bit high . At this time, we can only increase the pressure from the third node . The phenomenon is that the pressure on the main reservoir is not great , Write less , In terms of cost , Can we not add a third slave node ?
Scene two :
Suppose you have a 8 nucleus 64G Main library ,8 nucleus 64G Slave Library ,4 nucleus 32G Slave Library , In terms of configuration ,4 nucleus 32G The processing capacity of the slave database of the system is definitely lower than that of the other two , At this time, if we don't customize the proportion of traffic distribution , There will be a problem caused by the high pressure of low configuration database . Of course, this can also avoid using different rules of the slave Library .
The above scenario needs to be able to manage requests , stay Sharding-JDBC Provides a read-write separation routing algorithm in , We can customize the algorithm for traffic distribution management .
Implementation algorithm class :
public class KittyMasterSlaveLoadBalanceAlgorithm implements MasterSlaveLoadBalanceAlgorithm {
private RoundRobinMasterSlaveLoadBalanceAlgorithm roundRobin = new RoundRobinMasterSlaveLoadBalanceAlgorithm();
@Override
public String getDataSource(String name, String masterDataSourceName, List<String> slaveDataSourceNames) {
String dataSource = roundRobin.getDataSource(name, masterDataSourceName, slaveDataSourceNames);
// Control logic , For example, different slave nodes ( Different configurations ) There can be different proportions
return dataSource;
}
@Override
public String getType() {
return "KITTY_ROUND_ROBIN";
}
@Override
public Properties getProperties() {
return roundRobin.getProperties();
}
@Override
public void setProperties(Properties properties) {
roundRobin.setProperties(properties);
}
}
be based on SPI Mechanism configuration :
org.apache.shardingsphere.core.strategy.masterslave.RoundRobinMasterSlaveLoadBalanceAlgorithm
org.apache.shardingsphere.core.strategy.masterslave.RandomMasterSlaveLoadBalanceAlgorithm
com.cxytiandi.kitty.db.shardingjdbc.algorithm.KittyMasterSlaveLoadBalanceAlgorithm
Read write separation configuration :
spring.shardingsphere.masterslave.load-balance-algorithm-class-name=com.cxytiandi.kitty.db.shardingjdbc.algorithm.KittyMasterSlaveLoadBalanceAlgorithm
spring.shardingsphere.masterslave.load-balance-algorithm-type=KITTY_ROUND_ROBIN
About author : Yin Jihuan , Simple technology enthusiasts ,《Spring Cloud Microservices - Full stack technology and case analysis 》, 《Spring Cloud Microservices introduction Actual combat and advanced 》 author , official account Ape world Originator .
版权声明
本文为[Yin Jihuan]所创,转载请带上原文链接,感谢
边栏推荐
- (1) ASP.NET Introduction to core3.1 Ocelot
- 微信小程序:防止多次点击跳转(函数节流)
- Flink on paasta: yelp's new stream processing platform running on kubernetes
- Menu permission control configuration of hub plug-in for azure Devops extension
- Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things
- 对pandas 数据进行数据打乱并选取训练机与测试机集
- WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
- 微服務 - 如何解決鏈路追蹤問題
- 如果前端不使用SPA又能怎样?- Hacker News
- PLC模拟量输入和数字量输入是什么
猜你喜欢
随机推荐
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
面经手册 · 第12篇《面试官,ThreadLocal 你要这么问,我就挂了!》
通过深层神经网络生成音乐
hadoop 命令总结
網路程式設計NIO:BIO和NIO
文本去重的技术方案讨论(一)
前端模組化簡單總結
Ubuntu18.04上安裝NS-3
Sort the array in ascending order according to the frequency
恕我直言,我也是才知道ElasticSearch条件更新是这么玩的
windows10 tensorflow(二)原理实战之回归分析,深度学习框架(梯度下降法求解回归参数)
Listening to silent words: hand in hand teaching you sign language recognition with modelarts
python 下载模块加速实现记录
如何成为数据科学家? - kdnuggets
选择站群服务器的有哪些标准呢?
DRF JWT authentication module and self customization
车的换道检测
Electron应用使用electron-builder配合electron-updater实现自动更新
连肝三个通宵,JVM77道高频面试题详细分析,就这?
快快使用ModelArts,零基础小白也能玩转AI!