Open In App

What is Pub/Sub Architecture?

Last Updated : 06 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Consider a scenario of synchronous message passing. You have two components in your system that communicate with each other. Let’s call the sender and receiver. The receiver asks for a service from the sender and the sender serves the request and waits for an acknowledgment from the receiver.

  • There is another receiver that requests a service from the sender. The sender is blocked since it hasn’t yet received any acknowledgment from the first receiver.
  • The sender isn’t able to serve the second receiver which can create problems. To solve this drawback, the Pub-Sub model was introduced.

what-is-pub-sub-architecture-banner

What is Pub/Sub Architecture?

The Pub/Sub (Publisher/Subscriber) model is a messaging pattern used in software architecture to facilitate asynchronous communication between different components or systems. In this model, publishers produce messages that are then consumed by subscribers.

Key points of the Pub/Sub model include:

  • Publishers: Entities that generate and send messages.
  • Subscribers: Entities that receive and consume messages.
  • Topics: Channels or categories to which messages are published.
  • Message Brokers: Intermediaries that manage the routing of messages between publishers and subscribers.

What-is-Pub-Sub-Architecture

Components of Pub/Sub Architecture?

In the Pub/Sub (Publish/Subscribe) model, there are several key components that work together to enable communication between publishers and subscribers. These components include:

1. Publisher

The Publisher is responsible for creating and sending messages to the Pub/Sub system. Publishers categorize messages into topics or channels based on their content. They do not need to know the identity of the subscribers.

2. Subscriber

The Subscriber is a recipient of messages in the Pub/Sub system. Subscribers express interest in receiving messages from specific topics. They do not need to know the identity of the publishers. Subscribers receive messages from topics to which they are subscribed.

3. Topic

A Topic is a named channel or category to which messages are published. Publishers send messages to specific topics, and subscribers can subscribe to one or more topics to receive messages of interest. Topics help categorize messages and enable targeted message delivery to interested subscribers.

4. Message Broker

The Message Broker is an intermediary component that manages the routing of messages between publishers and subscribers. It receives messages from publishers and forwards them to subscribers based on their subscriptions. The Message Broker ensures that messages are delivered to the correct subscribers and can provide additional features such as message persistence, scalability, and reliability.

5. Message

A Message is the unit of data exchanged between publishers and subscribers in the Pub/Sub system. Messages can contain any type of data, such as text, JSON, or binary data. Publishers create messages and send them to the Pub/Sub system, and subscribers receive and process these messages.

6. Subscription

A Subscription represents a connection between a subscriber and a topic. Subscriptions define which messages a subscriber will receive based on the topics to which it is subscribed. Subscriptions can have different configurations, such as message delivery guarantees (e.g., at-most-once, at-least-once) and acknowledgment mechanisms.

How does Pub/Sub Architecture work?

Below is how the Pub/Sub architecture works:

  • Step: 1 – Publishers create and send messages to the Pub/Sub system. They categorize messages into topics or channels based on their content.
  • Step: 2 – Subscribers express interest in receiving messages from specific topics. They receive messages from topics to which they are subscribed.
  • Step: 3 – Topics are named channels to which messages are published. Publishers send messages to specific topics, and subscribers can subscribe to one or more topics to receive messages of interest.
  • Step: 4 – Message brokers are intermediaries that manage the routing of messages between publishers and subscribers. They receive messages from publishers and forward them to subscribers based on their subscriptions.
  • Step: 5 – When a publisher sends a message to a topic, the message broker receives the message and forwards it to all subscribers that are subscribed to that topic.
  • Step: 6 – Pub/Sub allows for asynchronous communication between publishers and subscribers. Publishers can send messages without waiting for subscribers to receive them, and subscribers can receive messages without the need for publishers to be active.

Real-World Example of Pub/Sub Architecture

A real-life example of Pub/Sub architecture can be seen in the operation of a social media platform like Twitter.

  • Publishers: Users post tweets, which are published to the Twitter platform.
  • Subscribers: Followers of a user subscribe to their tweets to receive updates.
  • Topics: Each user’s tweets can be considered a topic, and subscribers receive updates for the topics they are interested in.
  • Message Broker: Twitter’s backend infrastructure acts as the message broker, routing tweets from publishers to subscribers.
  • Message: Each tweet is a message that is published by the user and received by their followers.

In this example, the Pub/Sub architecture allows for scalable and efficient distribution of tweets to followers. Publishers (users) can publish tweets without knowing who will receive them, and subscribers (followers) receive updates in real-time without the need for direct communication with the publishers.

Use-cases of Pub/Sub Architecture

The Pub/Sub (Publisher/Subscriber) architecture is widely used in various scenarios where asynchronous and scalable communication between components is required. Some common use cases of Pub/Sub architecture include:

  • Real-time Data Streaming:
    • Pub/Sub is commonly used in real-time data streaming applications, such as IoT (Internet of Things) devices, sensor networks, and telemetry systems.
    • It enables devices to publish data streams that can be consumed by multiple subscribers in real-time.
  • Event-driven Architectures:
    • Pub/Sub is well-suited for event-driven architectures, where components react to events rather than polling for updates.
    • It allows components to subscribe to specific events and receive notifications when those events occur, enabling more responsive and reactive systems.
  • Message Queues:
    • Pub/Sub can be used as a message queuing system, where messages are stored temporarily until they can be processed by subscribers.
    • This helps in managing message delivery and processing in a scalable and efficient manner.
  • Notifications and Alerts:
    • Pub/Sub architecture is used for sending notifications and alerts to users or systems.
    • Publishers can publish notifications, and subscribers can receive them in real-time, enabling timely responses to critical events.
  • Scalable Web Applications:
    • Pub/Sub can be used in web applications to implement features such as real-time updates, chat applications, and collaborative editing.
    • It allows multiple users to receive updates simultaneously without overloading the server.
  • Microservices Communication:
    • Pub/Sub architecture is used in microservices-based applications to enable communication between microservices.
    • It helps in decoupling microservices and allows them to communicate asynchronously, improving overall system scalability and resilience.

When to Use the Pub/Sub Architecture

  • Decoupling: Use Pub/Sub when you want to decouple the components of your system. Publishers and subscribers do not need to know about each other, which allows for more flexible and scalable systems.
  • Scalability: Pub/Sub can be used to build highly scalable systems. You can easily add more publishers or subscribers without affecting the existing components.
  • Asynchronous Communication: If you need asynchronous communication between components, Pub/Sub is a good choice. Publishers can send messages without waiting for subscribers to receive them.
  • Event-Driven Architecture: Pub/Sub is well-suited for event-driven architectures. Publishers can emit events and subscribers can react to these events without tight coupling between them.
  • Dynamic Subscriptions: Pub/Sub allows for dynamic subscriptions. Subscribers can subscribe to different topics or classes of messages at runtime, which adds flexibility to the system.

When Not to Use the Pub/Sub Architecture

  • Low Latency: If you require low-latency communication between components, Pub/Sub might not be the best choice. The overhead of message routing and subscription management can introduce latency.
  • Complexity: Pub/Sub adds complexity to the system, especially in terms of message routing and managing subscriptions. If the system is simple and does not require this level of complexity, simpler communication patterns may be more appropriate.
  • Ordered Delivery: Pub/Sub does not guarantee message delivery in a specific order. If your application requires strict ordering of messages, Pub/Sub may not be suitable.
  • Small Scale: For small-scale applications with a limited number of components that communicate directly with each other, Pub/Sub may introduce unnecessary complexity.

How Scalable and Secure is Pub/Sub Architecture?

The scalability and security of the Pub/Sub model depend on the implementation and the specific requirements of the system. However, in general, the Pub/Sub model can be both scalable and secure if implemented correctly.

  • Scalability:
    • Horizontal Scalability: Pub/Sub systems can be designed to scale horizontally by adding more publishers, subscribers, or message brokers. This allows the system to handle a large number of messages and subscribers without a significant decrease in performance.
    • Load Balancing: Pub/Sub systems can use load balancing techniques to distribute messages evenly across multiple message brokers or nodes, ensuring efficient use of resources and handling of high message volumes.
  • Security:
    • Access Control: Pub/Sub systems can implement access control mechanisms to ensure that only authorized publishers and subscribers can send or receive messages. This helps protect against unauthorized access and data breaches.
    • Encryption: Messages exchanged in a Pub/Sub system can be encrypted to protect them from eavesdropping and ensure data confidentiality. Transport Layer Security (TLS) can be used to secure message transmission over the network.
    • Authentication and Authorization: Pub/Sub systems can use authentication and authorization mechanisms to verify the identity of publishers and subscribers and ensure that they have the necessary permissions to send or receive messages.
  • Challenges:
    • Message Ordering: Ensuring strict message ordering can be challenging in a distributed Pub/Sub system, especially when messages are processed by multiple subscribers concurrently.
    • Message Delivery Guarantees: Pub/Sub systems typically provide at-least-once or at-most-once message delivery guarantees, which may not be suitable for all applications requiring exactly-once semantics.

Benefits of Pub/Sub Architecture

  • Scalability: Pub/Sub systems can easily scale to accommodate a large number of publishers, subscribers, and messages. This scalability is achieved through the decoupling of publishers and subscribers and the use of message brokers to manage message distribution.
  • Decoupling: Pub/Sub decouples the publishers of messages from the subscribers, allowing them to operate independently. This decoupling simplifies the design and maintenance of the system and makes it easier to add or remove components.
  • Asynchronous Communication: Pub/Sub enables asynchronous communication between components, which improves system responsiveness and efficiency. Publishers can send messages without waiting for subscribers to receive them, and subscribers can process messages at their own pace.
  • Reliability: Pub/Sub systems are designed to be reliable, with mechanisms in place to ensure that messages are delivered successfully. This reliability is achieved through features such as message acknowledgments, retries, and fault tolerance.
  • Real-time Data Streaming: Pub/Sub is well-suited for real-time data streaming applications, where data is generated and processed in real-time. Pub/Sub systems can handle high volumes of data and deliver it to subscribers in real-time, making them ideal for use cases such as IoT, gaming, and financial services

Challenges of Pub/Sub Architecture

  • Message Ordering: Pub/Sub systems typically do not guarantee the order in which messages are delivered to subscribers. This can be a challenge for applications that require strict message ordering, as subscribers may receive messages out of order.
  • Exactly-once Message Delivery: Ensuring exactly-once message delivery can be challenging in Pub/Sub systems, especially in the presence of failures or network issues. Implementing mechanisms to guarantee exactly-once delivery without introducing duplicates can be complex.
  • Latency: Pub/Sub systems introduce latency due to the message routing and delivery process. Minimizing latency while maintaining scalability and reliability can be challenging, especially in real-time applications where low latency is critical.
  • Complexity: Implementing a Pub/Sub architecture can introduce complexity, especially in large-scale deployments. Managing subscriptions, message routing, and ensuring consistency across distributed components require careful design and management.
  • Security: Securing Pub/Sub systems against unauthorized access, data breaches, and message tampering requires implementing robust authentication, authorization, and encryption mechanisms.

Pub/Sub Vs. Point to Point Messaging

Below are the differences between Pub/Sub and Point to Point Messaging:

Aspect Pub/Sub Messaging Point-to-Point Messaging
Message Delivery Messages are broadcast to multiple subscribers Messages are delivered to a single receiver
Subscriber Knowledge Publishers do not need to know about subscribers Sender needs to know the receiver
Scalability Scalable, as new subscribers can be added without affecting publishers Less scalable, as each message is sent directly to a specific receiver
Coupling Loosely coupled, as publishers and subscribers are decoupled Tightly coupled, as sender and receiver are directly connected
Use Case Suitable for broadcasting messages to multiple recipients Suitable for one-to-one communication
Example Technology Google Cloud Pub/Sub, Amazon SNS/SQS JMS, AMQP, RabbitMQ, Kafka



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads