Interesting article about message queues

This article is the answer I saw on Zhihu God, and I admire it below:

 

Xiaohong is Xiaoming's older sister.

Xiaohong hopes that Xiaoming will read more, and often looks for good books for Xiaoming to read. The previous way is this: Xiaohong asks Xiaoming when he is free, sends the book to Xiaoming, and supervises Xiaoming after reading the book before leaving. Over time, both of them felt troublesome.

Later, the method was changed to: Xiaohong said to Xiaoming, "You must read the books I put on the bookshelf", and then every time Xiaohong found a good book, he put it on the bookshelf, and Xiaoming saw that there were books on the bookshelf. Take it down and see.

The bookshelf is a message queue, Xiaohong is the producer, and Xiaoming is the consumer.

The benefits of this are:

1. When Xiaohong wants to give Xiaoming a book, she doesn't have to ask Xiaoming when she is free, she handed the book to him, Xiaohong just put the book on the shelf. In this way, Xiaohong Xiaoming's time is more free.

2. Xiaohong believes in Xiaoming's self-awareness and ability to read. It is not necessary to observe Xiaoming's reading process with her own eyes. Xiaohong only needs to do a movement of putting the book, which saves time.

3. When another friend who loves to read, Xiaoqiang, joins tomorrow, Xiaohong still only needs to put the book on the bookshelf, and Xiaoming and Xiaoqiang can take the book from the bookshelf (well, let’s set it as multiple people picking up one book You can take one copy per person, it may be a copy of the e-book or photocopy, regardless of copyright issues).

4. The books on the bookshelf are placed there. Xiaoming reads it early if he reads fast, and reads it later if he reads slowly. It doesn't matter. Compared with the way Xiaohong handed the book to Xiaoming and supervises Xiaoming to finish reading, Xiaoming's pressure will be higher. smaller.

These are the four major benefits of message queues:

1. Decoupling

Each member does not have to be affected by other members, can be more independent and can only be connected through a simple container.

Xiaohong doesn't even know who took the books from the bookshelf, and Xiaoming doesn't even know who put the books on the bookshelf. In their eyes, there are only bookshelves and no other party.

There is no doubt that dealing with a simple container is 10,000 times easier than dealing with complex people. Xiaohong Xiaoming can pursue his own life freely.

2. Speed ​​up

Xiaohong chose to believe in "put the book on the shelf, and I won't ask anything else", saving herself a lot of time.

Xiaohong is very busy and can only spare five minutes, but it is enough time to put the book on the shelf.

3. Broadcast

Xiaohong only needs to work once to allow multiple friends to read books, which greatly saves her time and makes the cost of joining new friends very low.

4. Peak clipping

Assuming that Xiaoming reads very slowly, if Xiaohong supervises Xiaoming to finish reading every book he gives, Xiaoming is under pressure and Xiaohong is impatient.

Anyway, the frequency of Xiaohong's giving of books is not stable. If he gives five books in a row today and tomorrow, and then gives another book every three months, then Xiaoming only needs to take five books from the bookshelf to read within three months. Once it's over, it won't be so stressful.

 

Of course, using message queues also has its costs:

1. Introduce complexity

There is no doubt that the "bookshelf" is extra, and it needs a place to put it, and it also needs to be protected from theft.

2. Temporary inconsistencies

If the mother asked Xiaohong, "What book did Xiaoming read recently?" In the previous method, Xiaohong could tell her mother confidently because she supervised Xiaoming's reading, but in the new method, Xiaohong replied that her mother would later I thought to myself, "Xiao Ming should finish it soon..."

There is a period of time when "mother thinks Xiaoming has read a certain book, but Xiaoming has not read it". Of course, Xiaoming's final reading state will be consistent with his mother's cognition, which is the so-called "eventual consistency" .

So, what conditions need to be met for the use of message queues?

1. Producers do not need feedback from consumers

The return value of the interface of the direct call before the introduction of the message queue should be empty, which makes it possible for the upper layer to continue to move forward as if the action of the lower layer has not been done yet—that is, the so-called asynchronous.

After Xiaohong put the book, Xiaoming did not read it at all, Xiaohong did not ask at all, she acquiesced that he had read it, otherwise she could only use the original method to supervise until it was finished.

2. Tolerate transient inconsistencies

The mother may find that "sometimes it is said that Xiaoming has read a certain book, but in fact he has not read it", as long as the mother is satisfied that "he read it at the end anyway", there is no problem with asynchronous processing.

If her mother couldn't tolerate this situation and was furious with Xiaohong, Xiaohong wouldn't dare to use the bookshelf method.

3. It is indeed effective

That is, the benefits of decoupling, speed-up, broadcasting, and peak shaving exceed the costs of placing and monitoring bookshelves.

Otherwise, if you blindly copy, "I heard that Lao Zhao's family bought a bookshelf, our family also bought one", but it is useless to buy it back, it just makes more steps, it is better to hand the book directly to the other party, that is wrong .

 

 

Some application scenarios of message queues:

  • Asynchronous processing:
    • Asynchronous non-core processes to improve system response performance
  • Application decoupling:
    • The system is not strongly coupled, and message receivers can be added at will without modifying the code of the message sender. The success of the message sender does not depend on the message receiver (for example, some bank interfaces are unstable, but the caller does not need to rely on these interfaces)
    • It is not strongly dependent on the core process of the non-core system. For non-core processes, it can be placed in the message queue for message consumers to consume on demand without affecting the core main process.
  • Eventual consistency: Eventual consistency is not a necessary feature of message queues, but you can indeed rely on message queues to do things with eventual consistency.
    • Write the message first and then operate it, and then modify the message status after the operation is completed. The timing task compensation mechanism realizes the reliable sending and receiving of messages and the reliable execution of business operations. Pay attention to the design of message repetition and idempotency
    • All message queues that do not guarantee 100% message loss cannot theoretically achieve eventual consistency.
  • broadcast:
    • Only need to care whether the message is delivered to the queue, as for who wants to subscribe, it is a downstream matter
  • Flow peak clipping and flow control:
    • When there is a gap between the processing capabilities of the upstream and downstream systems, use the message queue as a general "funnel". Distribute when the downstream is capable of processing.
  • Log processing:
    • Use message queues in log processing, such as Kafka applications, to solve the problem of a large number of log transmissions
  • Newsletter:
    • Message queues generally have built-in efficient communication mechanisms, so they can also be used for simple message communication, such as implementing point-to-point message queues or chat rooms.

 

Guess you like

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