Messaging Patterns: Pub/Sub
Published: 2020-11-03 07:45:23

# Introduction
Messaging and message patterns are important parts of software development as they enable high-speed program-to-program communication with reliable delivery. Without it, services in the software will not be able to communicate with each other efficiently. This article mainly focuses on the publisher/subscriber message channel pattern. 

## Messaging and messaging patterns
Messaging in software engineering is typically asynchronous and used for inter-service communication. For software engineering, messaging patterns refer how services carry out messaging with each other, talking to one another through exchanging standardised messages over messaging channels. 

## Publisher/subscriber pattern
The pub/sub pattern is a message channel pattern, which connects the collaborating senders and receivers using a message channel that allows them to exchange messages. In the pub/sub pattern, senders are publishers and receivers are subscribers. In particular, the pub/sub mechanism removes the need for publishers to program the messages directly to specific subscribers. Instead, publishers can categorise published messages without any knowledge of subscribers and interested subscribers can subscribe to the categories of interest without any knowledge of the publishers. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/29pei1jwtnmdvdsmg3iw.png)

### Benefits
From the diagram, we can see that the publisher and subscriber do not need to know of each other, only knowledge of the event channel is needed and is sufficient to communicate. The benefit of this is that the components are loosely coupled, each publisher and subscriber can operate normally while independent of each other. This also provides scalability as publishers and subscribers can be scaled independently in the system without sending ripple effects across to the other components. 

### How is it applied in real life?
Pub/sub message pattern can be implemented through software like RabbitMQ, which is an open source message broker software or message-oriented middleware (MOM) that can be used irrespective of the language that the application is running on. In particular, RabbitMQ is able to support different kinds of messaging exchanges, including one-to-one direct messaging, pub/sub and broadcast types of exchanges. This flexibility makes RabbitMQ very popular and utilised by companies like Reddit, Trivago and Accenture. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2yl4xkvogc61n1orfbwg.png)

The role of RabbitMQ is similar to that of the Event Channel in the first diagram, acting as a message broker to determine which messages are sent to which queues. The producer acts as the publisher, supplying messages to the broker (RabbitMQ) without any knowledge of the consumer or subscriber who consumes messages from the message broker. In the first diagram, the event channel simply acts as a message channel for subscribers to receive the message. However, for RabbitMQ, messages are not published directly to a queue. Instead, it provides an additional step, where the "Exchange" receives the message and routes it to a specific queue. This is done with the help of bindings and routing keys that will determine which queue the message will be added to. These messages then stay in their respective queues until the consumer handles the message. 
The pub/sub pattern is clearly applied here as consumers and producers do not have to know of each other's existence, rather relying on RabbitMQ, a message broker, to help route the messages to their respective queues.