当前位置:网站首页>Reading and writing after separation, performance were up 100%
Reading and writing after separation, performance were up 100%
2022-08-09 12:18:00 【Park33448】
Today is a simple but important question.Recently, I built a project to play with, and it ran smoothly. However, during the stress test, I found that the performance of the interface was bottlenecked, and finally found that slow motion appeared in MySQL.
How can this be done?The index is also optimized, the connection pool is also used, and the Redis cache hit rate is also in line with expectations.However, all requests are concentrated on one main MySQL, and it is still very difficult during stress testing, so let's try reading and writing separation.
One. Master-slave replication logic
The database has one master and multiple slaves, which is a very classic structure, which is beneficial to the disaster recovery, scalability and high availability of the database.One master and multiple slaves, relying on master-slave replication, the following is the logic diagram of master-slave replication, let's take a look:
After master-slave replication, it can be considered that the contents of the slave library and the master library are "consistent", which refers to eventual consistency.In business scenarios that do not require high real-time read performance, there is no problem with read and write separation, and the data will eventually be consistent.
However, it must be said that due to the time required for master-slave replication, after writing data to the master library, if you read it directly from the slave library, you may not be able to read the latest value.Therefore, whether to read the master library or the slave library depends on the business scenario.
After master-slave replication, you can happily separate read and write.Specifically, all write requests are dispatched to the master library, and a large number of read requests are dispatched to the slave library.The logic diagram of read-write separation is as follows, which is very intuitive and easy to understand:
2. The effect of read-write separation
In the actual test, a large number of read requests are dispatched to the slave library, and write requests and a small amount of read requests are left on the main library. It can be seen that after the read and write separation, a small number of read requests on the main library are time-consumingImmediately noticeably decreased and stabilized:
Furthermore, it was actually found that the time-consuming of a large number of read requests was also reduced after being transferred to the slave library.Naturally, the performance of write requests on the main repository has also improved by nearly 100%.The benefits of read-write separation are obvious.So cool!
For application services, read-write separation can also be performed.For example, a service provides read and write interfaces, and the caller can separate read and write, so that different scaling strategies can be implemented for read and write, which improves the flexibility of expansion.
The content of this article is very simple. The key is to understand the process of master-slave replication and the principle of read-write separation.And, for software developers, hands-on practice is an excellent way to learn.After practice, experience the process and see the result, the knowledge and skills are yours.let's work hard together.
Afterword
Say I'm too short to recommend... OK
Find an integer, when stored in memory, the number of binary 1s.
Method 1: An integer number has 32 bits in total. How to judge whether each bit is 1?Just make a bitwise AND of this bit and 1
import java.util.Scanner;public class test {public static void main(String[] args) {Scanner scanner=new Scanner(System.in);int n= scanner.nextInt();int count=0;for (int i = 1; i <=32 ; i++) {if(((n>>i)&1)==1){count++;}}System.out.println("Number of 1s in binary: "+count);}}
The disadvantage of this method is that each number must be digitized with 32 bits. For example, only the first bit of 1 is 1, and there is no need to compare the following 31 0s
Optimization:
import java.util.Scanner;public class Main{public static void main1(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int count = 0;while (n != 0) {//If there is 0 in the process of moving, end the loopif((n & 1) != 0) {count++;}n = n >>> 1;}System.out.println(count);}}
Method 2:
Use two adjacent data to perform bitwise AND operation
The first loop: n=7 n=n&(n-1) = 7 & 6 = 6
Second loop: n=6 n=n&(n-1)= 6 & 5= 4
The third loop: n=4 n=n&(n-1)=4 & 3= 0
In this way, there are several 1s in the binary bits of the data, and the cycle is repeated several times, and bit operations are used in the middle, which is more efficient to process
public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int count = 0;while (n != 0) {n = n & (n-1);count++;}System.out.println(count);}
边栏推荐
猜你喜欢
JS 封装节流(后期优化)
C# async 和 await 理解
"Digital Economy Panorama White Paper" Special Analysis of Banking Industry Intelligent Marketing Application Released
GRPC整体学习
【Data augmentation in NLP】——1
Chinese valentine's day?Programmers don't exist
Redis的下载安装
2022 全球 AI 模型周报
信息系统项目管理师必背核心考点(六十三)项目组合管理的主要过程&DIPP分析
bat文件(批处理文件)运行时一闪而过解决方法
随机推荐
Win10调整磁盘存储空间详解
微信一面:一致性哈希是什么,使用场景,解决了什么问题?
【Basic model】Transformer-实现中英翻译
软件测试——金融测试类面试题,看完直接去面试了
在北京参加UI设计培训到底怎么样?
mysql8.0和navicat premium15下载安装
《数字经济全景白皮书》银行业智能营销应用专题分析 发布
【面试高频题】可逐步优化的链表高频题
Senior told me that the giant MySQL is through SSH connection
proto3-2语法
字节秋招二面把我干懵了,问我SYN报文什么情况下会被丢弃?
F280049库函数API编程、直接寄存器控制编程和混合编程方法
PAT1003
IDEA 关闭/开启引用提示Usages
未来装备探索:数字孪生装备
BeanFacroty和FactoryBean到底是什么?AppliacationContext它又是什么?
信息系统项目管理师必背核心考点(六十三)项目组合管理的主要过程&DIPP分析
The redis library cannot be imported
推荐一个免费50时长的AI算力平台
LeetCode #101. 对称二叉树