<> background

At some stage of the system , Concurrent accesses ( flow ) Will expand rapidly , In instantaneous time (1 Seconds or seconds ), Reach a critical point , Many systems often fail to respond at this stage , For example, the promotion activities of the merchants will be carried out in seconds , or 618, double 11 wait . The business pressure of the system at other times is often small . How to solve the critical pressure , Then we need to cut the peak and fill the valley

<> What is peak cutting and valley filling

The analysis of Baidu Encyclopedia is

Peak cutting and valley filling (Peak
cut) It is a measure to adjust the power load . According to the power consumption law of different users , Reasonably , Arrange and organize the power consumption time of all kinds of users in a planned way . To reduce peak load , Fill the low load . Reduce the peak valley difference of power grid load , Generate electricity , Power consumption tends to balance

It means to stagger the user's power consumption period , Of course, it's not suitable for our system to do this directly , I can't say you'll order again tomorrow .

<> Solution

How about letting users buy goods , It does not cause the database to crash when a large number of requests are directly transferred to the data .. The system needs to be modified as follows

Before transformation

After transformation

Obviously , After the transformation, the system architecture is added to the message queue MQ, Here we can try ActiveMQ,RabbitMQ,RcoketMQ,
Kafka Wait for any one , Select technology according to your own business . This gives full play to the fast processing speed of message middleware to solve the concurrent problem .

<> Example

Here's a list RabbmitMQ Producer and consumer code

* Add dependency <dependency> <groupId>org.springframework.boot</groupId> <artifactId>
spring-boot-starter-amqp</artifactId> <version>1.5.2.RELEASE</version> </
dependency>
* producer package com.rabbitMQ.pro; import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection; import com.rabbitmq.client.Channel;
public classProducer { private final static String QUEUE_NAME = "hello2";//
Queue name cannot be duplicate It would have failed before public static void main(String[] argv) throws java.io.
IOException{ /* Create with factory class Connection and Channel, And set parameters */ ConnectionFactory factory =
new ConnectionFactory(); factory.setHost("192.168.10.111");// MQ Of IP factory.
setPort(5672);// MQ port factory.setUsername("asdf");// MQ user name factory.setPassword
("123456");// MQ password Connection connection = factory.newConnection(); Channel
channel= connection.createChannel(); /* Create message queue , And send a message */ channel.queueDeclare(
QUEUE_NAME, false, false, false, null); String message = " news 2"; channel.
basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(
" Produced a '" + message + "'"); /* Close connection */ channel.close(); connection.close(); } }
* consumer package com.rabbitMQ.pro; import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection; import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer; public class Consumer { private
final static String QUEUE_NAME= "hello2"; public static void main(String[] argv)
throws java.io.IOException, java.lang.InterruptedException { /* Establish a connection */
ConnectionFactory factory= new ConnectionFactory(); factory.setHost(
"192.168.10.111");// MQ Of IP factory.setPort(5672);// MQ port factory.setUsername(
"asdf");// MQ user name factory.setPassword("123456");// MQ password Connection connection =
factory.newConnection(); Channel channel = connection.createChannel(); /*
Declare queues to connect */ channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.
out.println(" Wait for message to be generated :"); /* Create consumer object , For reading messages */ QueueingConsumer consumer = new
QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); /*
Read queue , And blocked , That is, blocking here before reading the message , Until the news , After reading the message , Continue blocking cycle */ while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message =
new String(delivery.getBody()); System.out.println(" Message received '" + message + "'"); } }
}
<> summary

In order to solve the system peak problem , We need to cut the peak and fill the valley , Join message middleware , Lengthen the time , Solve the problem of instantaneous pressure . Of course, it's just a list RabbitMQ,
There are also support for larger concurrent business RocketMQ and Kafka, Let's draw one example from another

Technology
©2019-2020 Toolsou All rights reserved,
It's unexpected Python Cherry tree (turtle The gorgeous style of Library ) Some East 14 Pay change 16 salary , Sincerity or routine ? Browser kernel ( understand )java Four functional interfaces ( a key , simple )HashMap Explain in detail html Writing about cherry trees , Writing about cherry trees os Simple use of module