Getting started with message queues

Message Queues allow applications to communicate by sending messages to each other. The message queue provides a temporary message storage when the target queue is busy.

 

Below I will introduce the message queue from the following aspects.

1. What is a message queue?

2. What are the benefits of using message queues?

3. Basic classification of message queues

 

1. What is a message queue?

A queue is a linear first-in, first-out collection of things waiting to be processed. A message queue is a queue of messages sent between two applications. It contains a series of work objects waiting to be processed.

 

A message is a number passed between sender and receiver, which is essentially an array of bytes with a specific format. A message may be a message that tells a system to start processing or finish processing a task, or an ordinary message.


 
The architecture of the most basic message queue is very simple: there is a producer client, which is responsible for creating messages and delivering them to the queue; and an application called a consumer, which connects to the queue and gets messages for processing. deal with. Messages in the queue will be stored in the queue until the consumer gets them.

 

 

 

 

 

 

 

 

 

 

 

 

 

Second, the benefits of using message queues

1. Asynchronous processing

 

The message queue provides an asynchronous communication protocol, and the producer needs the handler to return the processing result immediately. Email is perhaps the best example of asynchronous messaging at its best: after an email has been sent, the sender can go on with other things without waiting for an immediate response from the receiver. Therefore, this method decouples the association between producers and consumers, and producers and consumers do not have to interact with the message queue at the same time.

 

2. Decoupling (DECOUPLING)

 

Decoupling is used to describe the dependency of one system on another, and decoupling separates two systems so that their functions are more cohesive.

A decoupled system can communicate between two or more systems without establishing a connection, so the systems can maintain complete autonomy and transparency to other systems. Decoupling is also often the hallmark of a well-architected computer system, which is easier to maintain, extend, and debug.

When there is a problem with the consumer program, the producer can still put the message into the queue, so the producer is not affected, and can continue processing after the consumer returns to normal.

 

3. The structure is clear and simple

 

The architecture design of message queues makes the responsibilities and relationships between producers and consumers clearer, and reduces the complexity of logical processing; in addition, the architecture design for improving concurrency and thread safety is much simpler, and no more Multiple locking mechanisms.

 

4. SCALABILITY

 

Scalability is simpler for upstream and downstream. With the development of business, the magnitude of messages must also gradually increase. If the message queue is not used, it is easy to process when the data volume is small, but when it reaches a certain order of magnitude, the large data volume will have high requirements on the architecture. Some problems can be solved in the form of message queues. If you want to expand this producer-consumer model, the core module is on the message queue, and the structure extension of the producer and consumer is very easy. Therefore, major Internet companies have put a lot of effort into their own message queues.

 

Three, the basic classification of message queue

Whether the message queue is simple or complex, single-machine or distributed, from the relationship between the producer and the consumer, I divide it into two types:

 

1. Point-to-point message queue

There is a one-to-one relationship between producers and consumers, and the production of these messages is dedicated to a consumer.

For example, my current system receives real-time information from the Gateway system that users browse business pages on JD.com, so we implement message queues through Redis Queue. When the gateway writes the data, the consumption on my side is completed, and the data will be removed from the queue and will not continue to be saved.

Its model is as follows:

 

 

 

 2. Publish/Subscribe Mode (Pub/Sub)

 The usage scenario of this mode generally provides the data of the basic business for the producer, and there will be more than one consumer for the upper-level business. Therefore, the publish/subscribe model ensures that the use of messages by each consumer is independent of each other and does not affect each other. For example, Kafka is a typical Pub/Sub mode message queue.

Its model is:

 

 

 

 

 

 Reference documentation: What is message queueing?

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326536571&siteId=291194637