Introduction to Pub/Sub:
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:
- A publisher who sends a message.
- 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:
- Decoupled/loosely couples components.
- Greater system-wide visibility
- Real-time communication
- Ease of development
- Increased scalability and reliability
- Test ability improvements
Disadvantages of pub/sub pattern:
- Pub/Sub should be properly configured and maintained.
- 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:
- Ingestion user interaction and server events.
- Real-time event distribution
- Replicating data among databases.
- Parallel processing and workflows
- Enterprise event bus.
- Data streaming from applications, services or IoT devices.
- Refreshing distributed caches
- Load balancing for reliability.
Pub/Sub messaging services:
- Apache Kafka- Developed by Apache, kafka has robust Pub/Sub messaging features with message logs.
- Faye- Simple Pub/Sub service designed to power web applications with servers designed for NodeJS and Ruby.
- Redis- This is one of the most popular message broers with support for both traditional message queues as well as pub/sub pattern implementations.
- Amazon SNS — The Amazon Simple Notification Service is a fully managed service that offers Pub/Sub messages.
- Google Pub/Sub — GCP offering for pub/sub messaging service implementation.
- Azure Service Bus — A robust messaging service (Maas) solution that offers Pub/Sub pattern.
Hope you find it useful! Thanks for reading!