Introduction to Pub/Sub:

priya s
YavarTechWorks
Published in
3 min readMar 20, 2023

Hi friends, in this blog we will see about Pub/Sub(Publish/Subscribe) mechanism.

Pub/Sub is an asynchronous and scalable messaging service that decouples services producing messages from services processing those messages.

Pub/Sub is used for streaming analytics and data integration pipelines to ingest and distribute data. Basically, the Pub/Sub model involves:

  1. A publisher who sends a message.
  2. A subscriber who receives the message via a message broker.

With the popularity of decoupled and microservice-based applications, proper communication between components and services is crucial for overall application functionality. Pub/Sub messaging helps with this in two crucial ways:

  • Allowing developers to create decoupled applications easily with a reliable communication method
  • Enabling users to create event-driven architectures easily

A pub/sub model allows messages to be broadcasted asynchronously across multiple sections of the applications.

The core component that facilitates this functionality is something called a Topic. The publisher will push messages to a Topic, and the Topic will instantly push the message to all the subscribers. This is what differentiates the Pub/Sub model from traditional message brokers, where a message queue will batch individual messages until a user or service requests these messages and retrieves them.

Whatever the message is in the Pub/Sub model, it will be automatically pushed to all the subscribers. The only exception is user-created policies for subscribers that will filter out messages.

This approach makes it possible to create event-driven services without constantly querying a message queue for messages. It also enables developers to create different isolated functions using the same message (data) that can be executed parallelly with the ability to serve multiple subscribers.

The Pub/Sub pattern isolates publishers from subscribers so that publishers do not need to know where the message is being used while the subscriber does not need to know about the publisher. This helps to improve the overall security of the application organically.

Advantages of publish/subscribe pattern:

  1. Decoupled/loosely couples components.
  2. Greater system-wide visibility
  3. Real-time communication
  4. Ease of development
  5. Increased scalability and reliability
  6. Test ability improvements

Disadvantages of pub/sub pattern:

  1. Pub/Sub should be properly configured and maintained.
  2. Pub/Sub is suitable when dealing with media such as audio or video as they require smooth synchronous streaming between the host and the receiver.

Common use cases:

  1. Ingestion user interaction and server events.
  2. Real-time event distribution
  3. Replicating data among databases.
  4. Parallel processing and workflows
  5. Enterprise event bus.
  6. Data streaming from applications, services or IoT devices.
  7. Refreshing distributed caches
  8. Load balancing for reliability.

Pub/Sub messaging services:

  1. Apache Kafka- Developed by Apache, kafka has robust Pub/Sub messaging features with message logs.
  2. Faye- Simple Pub/Sub service designed to power web applications with servers designed for NodeJS and Ruby.
  3. Redis- This is one of the most popular message broers with support for both traditional message queues as well as pub/sub pattern implementations.
  4. Amazon SNS — The Amazon Simple Notification Service is a fully managed service that offers Pub/Sub messages.
  5. Google Pub/Sub — GCP offering for pub/sub messaging service implementation.
  6. Azure Service Bus — A robust messaging service (Maas) solution that offers Pub/Sub pattern.

Hope you find it useful! Thanks for reading!

--

--