当前位置:网站首页>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);}
边栏推荐
- [工程数学]1_特征值与特征向量
- 学长告诉我,大厂MySQL都是通过SSH连接的
- [Essence] Analysis of the special case of C language structure: structure pointer / basic data type pointer, pointing to other structures
- 微信小程序支付及退款整体流程
- web课程设计
- Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
- buck型三相PFC
- goalng-sync/atomic原子操作
- ThreadLocal类
- [现代控制理论]4_PhasePortrait爱情故事动态系统分析
猜你喜欢
随机推荐
获取url地址中问号后参数(即使是iframe也可以)
字符串 | 反转字符串 | 双指针法 | leecode刷题笔记
GRPC整体学习
2022 Niu Ke Duo School (6) M. Z-Game on grid
_main C:/ti/ccs1011/ccs/tools/compiler/ti-cgt-c2000_20.2.1.LTS/lib/rts2800_fpu32.lib<ar在线升级跳转疑问
PAT1002
[工程数学]1_特征值与特征向量
Chinese valentine's day?Programmers don't exist
LeetCode #101. 对称二叉树
PAT1004
redis的线程模型
[现代控制理论]5_系统的可控性_controllability
Visual Studio 2017 ASP.NET Framework MVC 项目 MySQL 配置连接
ClickHouse之MaterializeMySQL引擎(十)
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
PAT1007
二重指针-char **、int **的作用
TIC2000调用API函数Flash擦除片上FLASH失败
PAT1005
JD.com architects tidy up: what are the core technical knowledge points of jvm and performance tuning