Abstract: This article belongs to the original, welcome to reprint, reprint please reserve source: github.com/jasonGeng88…
case
One day, the product came up and said, “We’re going to do a user registration feature, and we need to send a success email to the user after the user has registered.”
Xiao Ming (siege lion) : “Good, the demand is very clear.” Provide a registration interface, save user information, initiate email call at the same time, after the email is sent successfully, return the user operation success. In a matter of time, the code was written. After the verification function was ok, it was released online.
After a period of normal online operation, the product rushed to say: “The function you do is not good ah, operation feedback registration operation response is too slow, many users have lost.”
Xiao Ming heard a cold sweat, hurried back to change. He found problems with the old single-line blocking approach. This time, he made use of the JAVA multithreading feature, another thread to send mail, the main thread directly back to save the results. Once the test passes, get it online. Xiaoming thought, this is no problem.
It wasn’t long before the product came back, and he said, “Registration is much faster now. But there is a new problem, some users respond, the mail can not receive. Could you please save the result when you send the email and resend it if it fails to be sent?”
Xiao Ming a listen, ah, and have to stay up late to work overtime. The product saw his helplessness and pain, and said, “Other teams have already done the email service, so you don’t need to do it yourself. You can use their service directly.”
Xiao Ming hurried to communicate with the mail team, only to find out that their service was not open to the public. Now Xiao Ming began to worry. He knew there was such a service, but he could not call it.
The mail team says, “Look, I’ve provided you with something like a post office box, where you write your message and the address we agreed on. Then you don’t have to worry about it any more, we can get the message from the agreed address and do the corresponding operation of the email.”
Later, Xiao Ming knew that this was the message queue widely spread outside the world. You don’t have to know where the service is or how to call it. All you have to do is send the message to the address you agreed on, and your job is done. The corresponding service will naturally listen to the message you send and perform subsequent operations. This is the biggest feature of message queues, which decouples services from synchronous operations to asynchronous processing and from multi-service operations to single service operations.
Ha, ha, ha. I can rest easy now. Too young, there is no foolproof technology
One day soon, you’ll find that all businesses use message queues instead of email. In this case, only one mail service module could not bear the continuous messages from the business side, and a large number of messages piled up in the queue. This requires more consumers (mail services) to jointly process messages in the queue, known as distributed message processing.
To be continued…
conclusion
define
With the above foundation, it should be understandable to look at the very official explanation.
Message queue (English: Message queue) is a method of communication between processes or between different threads of the same process. The storage column of software is used to process a series of inputs, usually from the user. The message queue provides an asynchronous communication protocol. The record in each storage column contains detailed data, including the time of occurrence, the type of input device, and specific input parameters. That is, the sender and receiver of the message do not need to interact with the message queue at the same time. The message is stored in the queue until the receiver retrieves it. — Wikipedia
Noun explanation
While the explanation is too official, let’s look at the simplest architectural model:
- Producer: a Producer of messages that produces and sends messages to the Broker.
- Broker: Message processing center. Responsible for message storage, acknowledgement, retry, etc., typically including multiple queues;
- Consumer: message Consumer, responsible for retrieving messages from the Broker and processing them accordingly;
features
asynchrony
Time-consuming synchronous operations are asynchronized by sending messages. The synchronous wait time is reduced.
Loose coupling
Message queues reduce the coupling between services and allow different services to communicate via message queues without concern for each other’s implementation details, as long as the format of the good news is defined.
distributed
Horizontal scaling of consumers reduces the risk of message queue blocking and the possibility of a single point of failure for a single consumer (although message queues themselves can also be distributed clusters).
reliability
Message queues typically store received messages on local hard disk (once the messages are processed, the stored information may be deleted depending on the message queue implementation) so that messages can be reloaded even if the application or message queue itself fails.