The Publisher-Subscriber Model

Photo by Roman Kraft on Unsplash

The Publisher-Subscriber Model

The publisher-subscriber model is a systems design pattern that enables communication between different components of a distributed system. The publisher creates and sends out messages to a channel (the topic), while the subscribers receive the messages. Before a subscriber can receive a message, they have to express interest in receiving messages by subscribing to a topic.

To ease your understanding consider a website that sends out newsletters, you are prompted to enter your email address to 'subscribe' to their newsletter, so when they create a new newsletter, they 'publish' it so that you and any other person who subscribed to their newsletters gets a copy of the newsletter in their mail. It's the same concept in distributed systems.

The publisher-subscriber model (pub-sub) is a messaging pattern that allows for asynchronous communication between different components in a distributed system. It is a decoupled architecture, meaning that publishers and subscribers do not need to be aware of each other's existence or identity. This makes the system more scalable and resilient to failures. So the publisher sends out a message without being aware of who the subscribers are or if they are even any subscribers, as long as it's published its message, it's done. It's completely decoupled from the subscriber. The message broker is responsible for conveying the message to the subscribers. When the subscriber receives the message, it can then process it as needed.

Real-world applications

The pub-sub model can be used in various real-world applications, for example, in stock market events, when the prices of a stock change, a publisher would publish the new price of the stock, while the subscribers that are interested in updates of the stock prices receive these updates as soon as it's published. They can update their systems with the new price or make some decisions to sell/buy the stock or some other task their system is designed to do.

Another good example of pub-sub in the real world is the notifications you get on your phone, your phone is just one of thousands or even millions of subscribers (other phones) that want to receive updates via notifications.

Benefits

Decoupling: Publishers and subscribers are decoupled from each other, meaning that they do not need to be aware of each other's existence or identity. This makes the system more scalable and resilient to failures.

Scalability: Pub-sub is highly scalable. Publishers and subscribers can be added or removed from the system without affecting the other components.

Asynchronous communication: Pub-sub supports asynchronous communication. This means that publishers do not need to wait for subscribers to receive and process their messages before they can send the next message.

Reliability: Pub-sub is reliable. Messages are typically stored in the message broker until they have been successfully delivered to all subscribers.

Conclusion

The publisher-subscriber model is a powerful messaging pattern that can be used to build a variety of distributed systems. It is a decoupled, scalable, asynchronous, and reliable way to communicate between different components in a system.

Some examples of systems that use pub-sub include Apache Kafka and Rabbit MQ.