Message Queue and Pub-Sub architecture pattern is widely used in distributed systems and microservices. Message Queue or Pub-Sub is the exchange by means data is exchanged from one end to the other end. Each one has it’s own purpose where, Message Queue exchange is used when it is need to delegate particular task to a service and it’s needs be performed only once where as Pub-Sub exchange is used when message needs to be delivered to all subscribers at least one copy.
Following are different software that facilitate the message queue or pub-sub for your infrastrcture
- Amazon SQS
- Google Cloud Pub-Sub
- Apache Kafka
Message Queue is the exchange used by microservices or distributed systems such that they can communicate allowing the architecture to scale horizontally based on the load or number of outstanding messages waiting to be consumed. Message Queue uses the Queue as exchange consists of publishing service and multiple consuming services. When message are placed in Queue in FIFO ( First in First Out). When the message is consumed by any one of the consumer, it will be removed from the Queue or exchange
As it is shown in the above figure, publisher publishing the message mn onto the queue which will be aded to the tail of the queue and on the right side we can see multiple consumer services, listening for the messages where message m0 is received by only one service “Consumer A” thus which is removed from the queue consequently not available to other consumers
As it is shown in the figure, think of consumers as workers getting work delegated by publisher. When Queue is having many outstanding messages to be consumed, which can trigger scaling of consumers in the microservice architecture
Pub-Sub architecture is used when you want something like broadcast. Pub-Sub exchange can consists of multiple publisher services and multiple consumer services. Exchange or message broker ensures that at least one copy each message published by publisher is received by each subscriber. That is, in pub-sub architecture each message sent by publisher is delivered to each subscriber, subscribed to specific Topic. Such that, unlike Message Queue, in Pub-Sub each subscriber will have it’s own queue where as in Message Queue all consumers share the same Queue.
When message is sent by publisher to a topic, topic will broadcast the message to subscribers. Each subscriber is bound to the queue such that each queue will have listening service (or subscriber) awaiting for the message.
There can be multiple publishers, publishing to same topic while multiple consumers subscribing to same topic as illustrated below
As it is shown in the above figure, when it comes to pub-sub architecture, there can be multiple publishers publishing to the same exchange or topic while all subscribers listing on same topic. Each subscriber receives at least single copy as it is shown all subscribers receiving m0 message from each queue bound to them.
Message Queue vs Pub-Sub Exchange Comparison
|It is used when to delegate task to service that needs be executed once
|It works like broadcast, where at least one copy of the message is received by all subscribers
|The service sending messages is known as publisher
And, service or entity receiving the message or listening to receive messages is referred as consumer
|Service or entity sending message is known as publisher
And, service or entity listening to receive messages referred as subscriber
|Out of many consumer only one consumer will receive the message
|All subscribers will receive the message
|Ideal for task delegation
Example: delegate tasks to pool of worker nodes based on load
|Ideal for message broadcast
Example: sending sensor readings to subscribers (IoT) on cloud