当前位置:网站首页>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);}

原网站

版权声明
本文为[Park33448]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091143419684.html