System Design Patterns: Publisher Subscriber pattern

dsysd dev
3 min readApr 12, 2023

--

The publisher-subscriber pattern is a messaging pattern used in distributed systems that involves multiple publishers and multiple subscribers.

It is a messaging paradigm in which senders (publishers) send messages to an intermediary (message broker), which then distributes the messages to any interested receivers (subscribers) who have registered an interest in receiving those messages.

The pattern is also known as pub-sub, and it’s often used in scenarios where there are multiple consumers of data or events.

In a pub-sub system, publishers are responsible for creating and sending messages, while subscribers register their interest in receiving messages on a particular topic or channel.

The basic flow of the pattern involves publishers sending messages to the message broker, which then routes the messages to all subscribers who have expressed interest in receiving them.

Subscribers don’t have to know about the publishers, and publishers don’t have to know about the subscribers — the message broker acts as an intermediary that decouples the two.

A Weather Application example

Photo by Jonathan Bowers on Unsplash

Let’s consider a scenario where a weather application is publishing updates about weather conditions to multiple services that have subscribed to receive these updates.

The services include a history service that logs weather conditions, a user service that sends notifications to users about weather updates, and a third-party service that uses the data for research purposes.

Here’s how the publisher-subscriber pattern can be implemented in this scenario

1. The weather application acts as a publisher, generating weather updates and sending them to a message broker, which acts as an intermediary between the publisher and subscribers.

2. The message broker then distributes the updates to all the services that have subscribed to receive them.

3. The history service receives the updates and logs them in a database for future reference.

4. The user service receives the updates and sends notifications to users who have subscribed to receive them, informing them about weather updates in their area.

5. The third-party service receives the updates and uses them for research purposes, such as analyzing trends in weather patterns.

In this way, the publisher-subscriber pattern allows multiple services to receive updates from a single publisher in a distributed system.

It enables decoupling between the publisher and subscribers, allowing them to operate independently and without knowledge of each other.

This makes the system more scalable, flexible, and fault-tolerant.

To summarize, the publisher-subscriber pattern is a useful design pattern in distributed systems for decoupling components and enabling communication between them.

It can be applied to a variety of scenarios, including weather updates, stock prices, news updates, and more.

Subscribe to my Youtube channel

Subscribe to my youtube channel if you are on the lookout for more such awesome content in video format.

https://www.youtube.com/@dsysd-dev

Subscribe to my Newsletter

If you like my content, then consider subscribing to my free newsletter, to get exclusive, educational, technical and interesting content directly delivered to your inbox

https://dsysd.beehiiv.com/subscribe

Important Links

Thanks for reading the post, be sure to follow the links below for even more awesome content in the future.

twitter: https://twitter.com/dsysd_dev
youtube: https://www.youtube.com/@dsysd-dev
github: https://github.com/dsysd-dev
medium: https://medium.com/@dsysd-dev
email: dsysd.mail@gmail.com
linkedin: https://www.linkedin.com/in/dsysd-dev/
newsletter: https://dsysd.beehiiv.com/subscribe

--

--

dsysd dev
dsysd dev

Written by dsysd dev

Helping you become an 11x developer. I write on distributed systems, system design, blockchain, and go. https://twitter.com/dsysd_dev

No responses yet