Joke Collection Website - Blessing messages - What is the usage scenario of message queuing?

What is the usage scenario of message queuing?

Personally, I think the main feature of message queue is asynchronous processing, and its main purpose is to reduce the response time and decoupling of requests. Therefore, the main usage scenario is to put time-consuming operations that do not need to return results immediately (synchronously) as messages into the message queue. At the same time, because of the use of message queue, as long as the message format remains unchanged, the sender and receiver of the message do not need to contact each other or influence each other, that is, decouple.

For example, if you use a scenario:

Suppose a user registers in your software, and the server will do these operations after receiving the user's registration request:

Check the user name and other information. If there are no problems, the user record will be added to the database.

If it is an email registration, it will send you an email, and mobile phone registration will send a text message.

Analyze the user's personal information in order to recommend some like-minded people to him or recommend him to those people in the future.

Send a system notification containing an operation guide to the user.

etc ......

But for users, the registration function actually only needs the first step. As long as the server keeps his account information in the database, he can log in and do what he wants. As for other things, do we have to finish them all in this requirement? Is it worth the user's time to wait for you to deal with these unimportant things? Therefore, when the first step is completed, the server can put other operations into the corresponding message queue and immediately return the user results, and the message queue can perform these operations asynchronously.

Or in another case, a large number of users register your software at the same time, and some problems begin to appear in the registration request under high concurrency, such as the mailbox interface can't bear it, or the cpu is loaded with a lot of calculations when analyzing information, which will lead to such a situation. Although the user data records are quickly added to the database, they get stuck when sending emails or analyzing information, which leads to a significant increase in the response time of the request, or even overtime, which is a bit uneconomical. Faced with this situation, these operations are generally put into the message queue (producer-consumer model), which can be processed slowly and the registration request can be completed quickly without affecting the user's use of other functions.