Practical skills of high-performance site Message Queue articles

What is the message queue

Message queue ( the Message Queue ) is an inter-process communication or communication between different threads of the same process. Between processes or threads through messages to communicate, to return immediately after the message is sent by the messaging system to ensure reliable delivery of information, news publishers (producers) just publish messages to the message in the team and who do not control spending, news users (consumers) just gets the message from the message queue for further processing without management who posted the message, so that publishers and users do not know about each other.

Message ( the Message ) refers to the data transfer between applications. Message can be very simple, for example, only contains the text string, may be very complex, such as an embedded object.

Message Queue Characteristics

By providing the messaging and message queuing model, it can be a distributed environment providing the application decoupling , elastically stretchable , redundant storage , flow clipping , asynchronous communication , data synchronization and the like function as a distributed system architecture of a important components, has a pivotal position. Message Queue main features are:

  • Induction of : synchronous operation will take, by way of sending a message performs asynchronous processing. Reducing the waiting time synchronization.
  • Loosely Coupled : Message Queuing reduces coupling between services, different services may communicate through the message queue, without concern for another implementation details, as long as the definition of a good format of a message on the line
  • Distributed : via scale consumer, reduces the risk of blocking message queue, and the possibility of single point of failure of individual consumers.
  • Reliability : the message queue message received will typically stored on a local hard disk (when the message has been processed, the stored information to achieve depending on the message queue, it is possible to delete it), so that even if the application itself is linked to the message queue or hang out, the message can be reloaded.

Message Queuing application scenario

Asynchronous Processing

Sync is measured from the initiation request has been processed into the final period, request the caller has been waiting to be processed in synchronous blocking call is completed.

Asynchronous processing process refers to the process initiated the request, the client code has returned, and it can continue to conduct their own follow-up operation, without the need to wait for the call processing is completed.

Some of the more time-consuming and does not require real time (synchronous) returns the operation result of the operation, the process can be processed by the asynchronous message queue. Doing so can delay the processing time-consuming operation, the time-consuming operation of asynchronous, without blocking the client program, the client program can continue until the results are processed, thereby improving the processing performance of the client program.

The main object of the process is asynchronous reduction request response time , asynchronous processes to achieve non-core, and improve system performance in response.

Application of Decoupling

The use of message queues, there can be multiple producers announced that more consumer spending messages together to complete the entire business process logic, the producers only care about whether the right to write messages to the message queue, consumers only interested in obtaining from the message queue message, then processing logic does not need to call the direct interaction between producers and consumers, without the coupling dependent code.

The lower coupling program code more easily maintained, easily extended.

Traffic clipping

Widely used in general spike in activity.

In the spike activity in general, due to the instantaneous volume of traffic, the server instantly receive lots of requests, traffic surge, this case is likely to lead to related systems can not handle the request even collapse. To solve this problem, the general will join message queue in front-end applications.

  • First write request message queue, rather than directly by the service processing system, a buffer made, greatly reducing the pressure of the business processing system.
  • Can be done to limit queue length, spike activity are typically of quantitative restrictions, the spike can not write to a user queue goods, such a request may be discarded directly, can be directly returned event has ended or commodities sold.

The use of message queues, even if growth is sustained traffic, the system can still continue to receive requests. Although the producers generated message faster than the consumer spending, but through the message queue buffer, the processing capability in a short time not affect each other between producer and consumer, also can ensure the stability of the system.

Message traffic

Message queues are generally built efficient communication mechanism, and therefore can be used for simple message communication, such as a message queue, or point to point chat.

broadcast

If there is no message queue, whenever a new business party intervention, it requires the FBI once interfaces. With the message queue, the message is delivered only relationship the queue, as to who would like to subscribe, downstream of things, no doubt greatly reduce the workload of the development and the FBI.

Log Processing

With the log message queue processing, to solve the problem of a large number of log transfer (e.g., Kafka).

Transmission mode message queue

Ad-Hoc mode (Point to Point)

Ad-hoc mode for the message producers and message consumers between the point to point communication. A message producer sends a message to a particular queue identified by a name ( Queue). Before being passed to the consumer in the message which is stored in the queue. Message queues can be placed in memory can also be persistent in order to ensure still be able to deliver messages as a service failure.

Point to Point mode features:

  • Each message is only a consumer (Consumer), the message is that once the consumer, the message is no longer in the message queue.
  • There is no dependence between producers and consumers, after the producer sends a message, whether or not consumers in the run, will not affect the next time to send a message to the producer.
  • After successfully receiving the message consumers need to reply successfully to the queue, to delete the message queue messages currently received.

Publish / Subscribe mode (Publish / Subscribe)

Publisher / subscriber model to support a specific news topic production news. 0 or more subscribers likely to receive a particular message topic of interest message.

In this model, publishers and subscribers who do not know each other. Consumers can get more information at the publisher and the subscriber exists between the time-dependent . Publishers need to create a subscription ( subscription), to enable consumers to subscribe. Subscribers must maintain continuous activity status and receive messages .

Publish / CTR mode features:

  • Each message can have multiple subscribers.
  • There are dependent on the time between publishers and subscribers, for a theme (Topic) subscribers, it must be created after a subscription to news consumption publisher.
  • In order to consume messages, subscribers need to subscribe in advance the character theme, and keep the line running.

Comparative centralized messaging middleware

Currently in production, the use of message queues have more ActiveMQ , RabbitMQ , ZeroMQ , Kafka used to live , MetaMQ , RocketMQ and so on.


Good text recommended:

Guess you like

Origin juejin.im/post/5d15d5e55188255eea62de7d