🌐 中文 English
This article aims to explain the principles of two basic message queue modes.
What is a Message Queue
A message queue is a technology used to pass messages in distributed systems. It provides an asynchronous, decoupled, and scalable way to handle messages. Message queues are typically used to decouple the dependencies between producers and consumers, allowing producers and consumers to be developed and scaled independently.
Two Basic Modes of Message Queues
1. Queue Mode (Point-to-Point Mode)
It uses a queue as the data structure, and messages are processed in a first-in-first-out (FIFO) order. Producers send messages to the queue, and consumers retrieve messages from the queue for processing. This mode is suitable for scenarios where messages need to be processed in order, such as task queues, workflows, etc.
2. Publish-Subscribe Mode
It uses a one-to-many relationship between publishers and subscribers, where messages are published and subscribed through topics. When a message is published to a topic, all consumers subscribed to that topic will receive the message. This mode is suitable for scenarios where the dependencies between producers and consumers need to be decoupled, and multiple consumers are allowed to process messages simultaneously, such as event-driven architectures, message broadcasting, etc.
Advantages of Publish-Subscribe Mode Compared to Queue Mode
The publish-subscribe mode has the following advantages over the queue mode:
- In the queue mode, once a message is consumed, it is deleted from the queue and cannot be consumed again. In contrast, in the publish-subscribe mode, when a consumer consumes a message, it only moves the message’s position, and the message is retained. This allows multiple consumers to consume the same message simultaneously, avoiding the need to replicate multiple queues for multiple consumers, which would consume significant storage resources.
- Due to its logging approach, the publish-subscribe mode can skip erroneous consumption content directly. If previously consumed content is lost, it can be replenished by re-consuming the content.
- The publish-subscribe mode uses a Topic-Consumer Group-Queue approach, storing messages sequentially in individual queues. This allows each consumer within a consumer group to read from different queues or a single consumer to read from multiple queues simultaneously, speeding up the reading process.
Summary
The above is an explanation of the principles of the two basic modes of RocketMQ.