Sitemap
CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Publisher & Subscriber Model

--

Hello, I hope you all are fine,

Today I am writing about the most important and most-used model in Software Architecture.

Publisher and Subscriber is a messaging pattern mostly used in distributed systems for asynchronous communication between different components or services. In the same way, we call this a pub/sub model. This model enables to exchange of messages between the pub (publisher) and sub (subscriber).

This model helps to enable Event-Driven architecture in the application. This adds more flexibility to the application.

The publisher is the component that creates messages with information and sends them to subscribers.

The subscriber is the component that receives and consumes messages from the publisher.

According to the above image Service 2 acts as the publisher and Service 2 and 3 are subscribers for Service 1.

Imagine that a client sends a request to service 1. Service 1 receives this request and processes the request. Then sends messages for Service 2 and Service 3. Service 2 and 3 send successful responses for Service 1, and Service 1 also sends successful responses to the client.

The main drawback of this architecture is Service 1 is tightly coupled with Service 01 and Service 2. When Service 1 receives a request from a client, it waits for Service 2 and Service 3 to complete.

Get Maneesha Nirman’s stories in your inbox

Join Medium for free to get updates from this writer.

What happens, If Service 2 is offline?

Then Service 1 is waiting for Service 2 to complete, But Service 1 gets time out because Service 2 is offline. If Service 1 is arranged to store data in the database before sending it to Services 2 and 3, Service 2 failure needs to manage another mechanism to manage rollback for Service 1 and the same as Service 3. Then it is an issue for the whole process.

To solve the above issue, we can implement a message broker. Message brokers are responsible to validate, store, route, and deliver messages to the appropriate destinations. Publishers don’t send messages to specific subscribers in a point-to-point manner. Message brokers use topics to group messages into entities. Topics are Logs or Virtual groups that hold the messages and events in a logical order. This is an advanced level of Queue.

Press enter or click to view image in full size

The pub/Sub model helps us to manage dependencies. Now Service 1 does not depend on any other services. If Service 2 is offline, the message broker executes the task when Service 2 is back to normal.

This model support for high scalability. The pub/Sub model allows any number of publishers to communicate with any number of subscribers. As in the below image, Pub/Sub model allows to work with multiple data sources.

The real-world example for Pub/Sub model is Youtube. When a youtube channel publishes a video, All subscribers of that channel get notified. The point is only subscribers get notified. If subscribers are offline, youtube manages to notify them when subscribers are online.

This is the basic idea for PUB/SUB.

See you in the next article. Follow me through LinkedIn.

https://www.linkedin.com/in/maneesha-nirman-7b2a801a5

Thank you! Stay tuned…

--

--

Maneesha Nirman
Maneesha Nirman

Written by Maneesha Nirman

Techie at Virtusa | Python, Java & Azure | Exploring AI & automation | Sharing real-world dev tips & HCM insights | Building smarter systems daily

CodeX
CodeX

Published in CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers