Overview
Distributed Message Queue has two models
- Point to Point
- Pub-Sub
To understand the difference we have to understand a few terms first
- Producer – The producer is the one who is producing the message that needs to be consumed by the consumer in an asynchronous way.
- Message Queue – The message Queue is the place where the producer pushes the messages and the place where messages are kept till they are read by the consumer
- Consumer or Subscriber – The consumer is the one who is consuming the message that has been put. When it comes to consumers then there is one important thing to note. There could be multiple processes or threads of the same consumer running on the same or different machine in order to parallelize the processing of messages from the message queue. We will use the term consumer in this tutorial although both consumer and subscriber can be used analogously.
Let’s see the difference between the two
Point to Point
In the Point to Point model, there is one producer and one consumer, and bookkeeping is done per message level. Basically, there is one destination for the message. So for example let’s say there are two threads that are running for the consumer. There is one message in the message queue which has been put by the producer. Then only one thread of that consumer will be able to consume that message and once done it can delete that message from the queue. Below is the diagram for P2P
If there are two messages in the queue then one message could be picked by one thread and the other message could be picked by the other thread. Or there could be a case where both the messages are being picked or consumed by either of the thread. And so on for more than two messages
The point is that one message could only be consumed by one thread or process of the consumer and then it is deleted.
It is also possible that the second message is processed first and deleted from the queue and the first message is then processed and deleted. So therefore an unordered bookkeeping of which messages have been deleted and which have not been deleted is done by the distributed message queue.
Pub-Sub Model
In the Pub-Sub there is one producer and multiple consumers. Again for each of the consumers, there could be multiple threads or processes running on the same or different machine. Basically in Pub-Sub Model, there could be multiple destinations for the same message. Here is the diagram for Pub-Sub Model
If there is one message in the message queue then that message will be consumed by all the consumers. The message is only deleted from the distributed message queue once it is processed by all the consumers.
In the Pub-Sub model generally, the bookkeeping is not done per message per consumer level. Instead distributed message queue maintains an offset up to which each consumer has processed the message. Based upon that offset distributed message queue will decide to delete that message.
As an example consider there are 4 consumers. Also, assume there are 10 messages in the queue. Initially, none of the consumers processed any of the messages, so the offset for each of the consumers is zero.
Consumer Name | Offset |
C1 | 0 |
C2 | 0 |
C3 | 0 |
C4 | 0 |
After that, all the consumers start consuming the messages. Let’s say C1 is able to process all 10 messages and informs Message Queue that it has consumed and processed all 10 messages. Similarly, C2 is able to process the first 5 messages and inform Message Queue about the same. Similarly, C3 and C4 are able to process the first 8 and the first 4 messages respectively. So below will the offset table for each consumer
Consumer Name | Offset |
C1 | 10 |
C2 | 5 |
C3 | 8 |
C4 | 4 |
The message queue can safely delete the first four messages as it knows that the first four have been consumed by all the consumers. Basically, it looks at the minimum offset and decides to delete based on that.
Another point to note here is that the consumers can only commit offset up to which they have consumed the messages. So if a consumer C4 has let’s say successfully message 1,2,3,4, and 6. But it has not been able to process message 5 then it will only commit offset 4 into the Publisher and not 6.
Also note that in the Pub-Sub case as well only a single thread or process of a particular consumer is consuming a particular message then that message cannot be consumed by other thread/process of the same consumer.
Conclusion
This is all about the general differences between the P2P and Pub-Sub Model of a Distributed Message Queue. Hoped you have liked this article. Please share feedback in the comments.