Why do we need a message queue? Message Queuing What are the benefits?

First, the characteristics of the message queue

Independent service, a message queue having universal properties need not be considered an upper assembly business models for distributing messages only do it, but the different modules of the upper layer service specification relies on the message queue as defined by communication.

FIFO, to ensure delivery arrives first is the essential difference and a message queue in a buffer.

Disaster recovery, for the assembly of universal message queue, the message persistence and dynamic additions and deletions of nodes, which are to support disaster recovery capability of the important fundamental characteristics. Of course, this feature is for most of the game server application's message queue is not essential, this is also related with the application scenarios, many times there is no such persistent demand.

Performance, to say this is not necessary, the throughput of the message queue up, intercom overall system efficiency will also improve.

Second, why need the message queue?

When inconsistencies such as "production" and "consumption" appears in the system speed or stability factors, we need a message queue, as an abstraction layer, to bridge the differences between the two sides. "Message" is the unit of data transfer between two computers. Message can be very simple, for example, contain only text strings; may be more complex, and may contain embedded objects. Message is sent to the queue "message queue" message is stored in a container in the message transmission process.

A few examples

Message 1) business system triggers the SMS application, SMS module but could not keep pace, you need to scratch too late to deal with it, buffer pressure. The application can send SMS message queue thrown directly back user successfully message delivery module can take to re-take the message in the message queue for processing.

2) higher costs under orders tune the remote system, and because the network and other factors of instability, save a group sent along.

3) based task processing system, the user first initiates a request received over the task to the message queue in memory, and then open the rear end of the plurality of application tasks taken from the queue for processing.

Third, the use of message queues What are the benefits?

3.1, to improve the response speed of the system

Using a message queue, one of the producers, the message to the queue fling can immediately return a response to the user. Without waiting for the results.

Processing results allows users to pick their own later on, such as the hospital to take a single laboratory. But also allows producers subscribe (such as: leave a phone number or so that producers implement listener interfaces, listens to join the queue), the outcome of the notification. Obtain the agreement will result somewhere, without notice.

3.2, improve system stability

Taking into account the electricity supplier system orders, send data to the case of the production system. Network between the electricity supplier and production systems may have dropped, the production system may be out of service for maintenance and other reasons. If you do not use the message queue, the electricity supplier system data release out, the customer can not be ordered, affecting business development. It should not be so tight coupling between the two systems. It should be decoupled through the message queue. At the same time make the system more robust and stable.

Asynchronized, decoupling from the peak

The above three points can actually use an example to explain - Imagine a MMO game, there is no buffer layer or ORM human flesh written all logical nodes are directly connected to MySQL, within logical node in addition to focus on complex logic scenes, fighting, interactive and other outside , but also a fight SQL statements modules think is simply boring. First consider the drawbacks of this design is located:

  • Db logical nodes interact with have a lot of IO, even if coupled with Db interaction module within the logical node, you realize it is a black box, if the internal synchronization is achieved, then the card directly to your main game logic, just because a save operation, players are dropped, the server can also be turned off.

  • Then we improve it for 1, this module can do a thread hanging on logical node. In fact, this interactive logic node Db with this front-end module will be based on a comparison of the original message queue. But this also has a downside, and that is both computationally intensive task one is (logic players), one is IO intensive (read only responsible for writing MySQL), get a node, expand it It will be very troublesome, and the degree of coupling is too high. For example, now the scene found a bottleneck placed on a single node, according to sub-node scene, then this hung a data module how to interact with other scenes it?

  • The peak of the problem. In a distributed system, a distributed transaction is associated with a plurality of nodes, each node will become the bottleneck problems throughout the transaction process. Without a cushion between logical nodes and database nodes, that is, each operation must access the database for the MMO, the players on the line a few hundred K load data, a serving 100,000 players on the line enough to engage in a collapse of the mysql node. If the direct bring down the still relatively good result, at least in front of the player and can go up really Login normal game, players log back not on. But unfortunately, a decade before the start of the popular C10K argument is talking about: the amount of concurrent up later, will cause chain reaction, a large number of concurrent will not hang up your mysql node directly, but will slow down the speed, lower throughput, a players requests since the processing time is too long, causing the player to retry give up, but for the back end, the process of the players before the consumption of resources on all waste, into a vicious circle.

So, in this scenario, a buffer between the node between logical nodes and node db is a matter of course. This cache node in fact, often can be seen as a more complex message queue node.

Fourth, why the need for distributed?

4.1, a distributed multi-system collaboration needs

Data message queue needs to share data across multiple systems play value. It is necessary to provide a distributed communication mechanism, coordination mechanism.

4.2, a single deployment environment requires a distributed system

Internal ticketing system for better performance, in order to avoid a single point of failure, mostly clustered environment. Cluster environment, a plurality of applications running in the JVM multiple servers; data is also stored in the plurality of nodes of various types of databases or database. In order to meet the multi-node collaboration needs, the need to provide a distributed solution.

V. distributed environment need to address what issues?

5.1, concurrency issues

The need for good concurrency control. Ensure that the "thread-safe." Do not appear to be an order to ship twice. Customers do not appear under the single A, B shipments sent to customers and other circumstances.

5.2, a simple, unified operation mechanism

Need a simple definition, semantic clear, unrelated business, proper and secure unified access.

5.3 Fault Tolerance

Control the single point of failure, to ensure data security.

5.4, ​​may be laterally extended

Easy expansion.

Sixth, how to achieve?

Too many mature message queuing middleware product family too numerous to prepare the carrier. Proven mature product, interface specification, scalability.

Combined with environmental factors cause, the cause organizational processes estate, real Shiyun Wei consideration, considering the technical route, developers and other circumstances into account.

Seven common message queue comparison and selection

Published 399 original articles · won praise 939 · views 490 000 +

Guess you like

Origin blog.csdn.net/A_BlackMoon/article/details/104382606