Architectural Thinking Growth Tutorial Series (4) - How "Message" Decouples Complex Systems

background

For large-scale Internet systems, the business logic is relatively complex, or the complexity of the technical architecture is increased due to massive, high-concurrency scenarios. At this time, it is necessary to decouple the complex system. Here, message middleware is used to decouple the system.

content

Message middleware usage

Let's understand a few concepts first:

Coupling: Also called coupling degree, it is a measure of the degree of association between modules. The strength of the coupling depends on the complexity of the interface between modules, the way to call the module, and how much data is transferred through the interface.

The coupling degree between modules refers to the dependency relationship between modules, including control relationship, call relationship, and data transfer relationship. Generally speaking, the more connections between modules, the stronger their coupling, and the less independent they are.

Coupling and cohesion are usually used in software design as the standard to measure the degree of module independence. One criterion for dividing modules is high cohesion and low coupling .

Message middleware: It uses an efficient and reliable message delivery mechanism for platform-independent data exchange, and integrates distributed systems based on data communication. It scales inter-process communication in a distributed environment by providing a message passing and message queuing model.

The message queue mainly has the following functions: asynchronous processing, traffic peak clipping, and application decoupling .

  • Asynchronous processing. The production end does not need to wait for the response from the consumer end, and returns directly, which improves the response time and throughput.
  • Traffic clipping. To equalize the traffic during the peak period, the consumer can process it according to its own speed, and at the same time, it does not need to add too many resources during the peak period to improve resource utilization.
  • Application decoupling. The producer and the consumer do not need to depend on each other.

Application Scenario

1. Asynchronous processing:

Scenario description: After the user places an order, they need to send emails and text messages for confirmation. There are two traditional approaches, one is serial and the other is parallel.

  • a. Serial mode: After writing the order information into the database, send an email, and then send a text message, and return to the client after all the above three tasks are completed. One problem here is that emails and text messages are not necessary, it is just a notification, and this approach makes the client wait for unnecessary events.
  • b. Parallel mode: After writing the order information into the database, send emails and text messages at the same time, and return to the client after the above three tasks are completed. The parallel mode can improve the processing time.

Assume that the three service nodes use 50ms respectively, the serial use time is 150ms, and the parallel use time is 100ms. Although the processing time has been improved by parallelism, as mentioned earlier, emails and SMS have no effect on the normal use of the website by users. The client does not need to wait for the completion of the sending to display the success of the order. It should return after writing to the database.

Synchronously

If it is processed in a general synchronous manner, the system performance will be very slow.

Thread pool - asynchronous mode
  • c. Message queue
Message middleware - asynchronous mode

After the message queue is introduced, sending emails, text messages, etc. is not necessary business logic, so asynchronous processing can be performed.

It can be seen from this that after the introduction of the message queue, the user's response time is equal to the time of writing to the database + the time of writing to the message queue. After the introduction of the message queue for post-processing, the response time is 3 times that of serial and 2 in parallel. times.

2. Application decoupling:

Scenario description: A certain treasure double 11 shopping carnival, after the user places an order, the order system needs to notify the inventory system to perform the lock inventory operation. The traditional method is that the order system calls the interface of the inventory system.

Asynchronous processing of orders

There is a downside to this approach: when the inventory system fails, orders fail. The order system and the inventory system are highly coupled, and the solution is to introduce message queues.

  • Order system: After the user places an order, the order system completes the persistence process, writes the message into the message queue, and returns the user's order.
  • successfully ordered. The inventory system subscribes to the order message, obtains the order message, and performs warehouse operations.

If the inventory system fails, the message queue can also ensure the reliable delivery of messages without causing message loss.

3. Flow peak clipping:

Traffic peak clipping is generally widely used in seckill systems.

Scenario Description: A shopping site’s seckill activity usually causes the application to hang up due to excessive traffic. To solve this problem, a message queue can be added to the front end of the application.

flow clipping

effect:

  • The number of active people can be controlled, and orders exceeding a certain threshold are discarded directly.
  • It can alleviate short-term high flow crushing system.

The modified processing flow is as follows:

After the server receives the user's request, it first writes it into the message queue. If the length of the added message queue exceeds the maximum value, it will directly discard the user request or jump to the error page.

The seckill business performs follow-up processing according to the request information in the message queue.

at last:

Above, we introduced the decoupling of complex systems through message middleware, and the introduction of message middleware can also help the system improve responsiveness and usability.

Message middleware is a very commonly used tool in today's architecture technology. I hope you can spend time researching and practicing it.

 

Previous Chapter Tutorial

Architectural Thinking Growth Series Tutorials (3) - How "Cache" Copes with Traffic Peaks of Hundreds of Millions

The series of tutorials

Architectural Thinking Growth Series Tutorials

my column

 

 

At this point, all the introductions are over

 

 

-------------------------------

-------------------------------

 

My CSDN homepage

About me (personal domain name, more information about me)

My open source project collection Github

 

I look forward to learning, growing and encouraging together with everyone , O(∩_∩)O Thank you

Welcome to exchange questions, you can add personal QQ 469580884,

Or, add my group number  751925591 to discuss communication issues together

Don't talk about falsehood, just be a doer

Talk is cheap,show me the code

Guess you like

Origin blog.csdn.net/hemin1003/article/details/114928533