The Wayback Machine - https://web.archive.org/web/20250808215808/https://www.geeksforgeeks.org/system-design/what-is-pub-sub/
Open In App

What is Pub/Sub Architecture?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
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:

  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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 messages and send them to the Pub/Sub system. They sort these messages into topics or channels based on what the messages are about.
  • Step 2: Subscribers let the system know which topics they're interested in. They’ll get messages only from those topics.
  • Step 3: Topics are basically channels for messages. Publishers send their messages to specific topics, and subscribers can choose one or more topics to follow.
  • Step 4: Message brokers act as go-betweens, managing how messages get from publishers to subscribers. They take messages from publishers and send them to the subscribers who are interested.
  • Step 5: When a publisher sends a message to a topic, the message broker grabs it and sends it out to all the subscribers who have signed up for that topic.
  • Step 6: The Pub/Sub system allows for asynchronous communication. This means publishers can send messages without waiting for subscribers to be ready, and subscribers can pick up messages whenever it suits them, without needing the publisher to be around

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: It’s often used in applications that deal with real-time data, like IoT devices and sensor networks, allowing multiple subscribers to get data streams right away.
  • Event-driven Architectures: This setup works well for systems that react to events instead of constantly checking for updates, making applications more responsive.
  • Message Queues: Pub/Sub can act as a message queue, temporarily holding messages until subscribers can process them, which helps manage how messages are delivered.
  • Notifications and Alerts: It’s used for sending notifications and alerts, letting publishers send important updates that subscribers can receive instantly.
  • Scalable Web Applications: In web apps, Pub/Sub supports features like real-time updates and chat, so many users can receive information at the same time without overloading the server.
  • Microservices Communication: Finally, it helps microservices talk to each other, allowing them to communicate without being tightly connected, which improves scalability and reliability.

Types of Pub/Sub Services

Below are the two types of Pub/Sub Services:

1. Pub/Sub Service

This is the main messaging service that most users and applications choose. It provides:

  • High Reliability: Ensures that messages are delivered consistently.
  • Integrations: Supports a wide range of integrations with other services.
  • Automatic Capacity Management: Handles scaling automatically based on demand.
  • Data Replication: Synchronously replicates all data to at least two zones and offers best-effort replication to a third zone for added reliability.

2. Pub/Sub Lite Service

This is a separate messaging service designed to be more cost-effective but comes with some trade-offs:

  • Lower Reliability: Offers less reliability compared to the standard Pub/Sub service.
  • Zonal or Regional Storage: Zonal Lite topics are stored in one zone, while Regional Lite topics replicate data to a second zone asynchronously.
  • Pre-provisioning Required: You need to manage and provision your own storage and throughput capacity.
  • Cost-Effective: Consider this option if keeping costs low is essential, and you can accept the lower reliability and some extra management tasks.

Comparing Pub/Sub to other Messaging Technologies

Below is the comparison of Pub/Sub to other messaging Technologies:

  • Pub/Sub vs. Message Queues
    • Message Queues (e.g., RabbitMQ): Focus on delivering messages to one consumer at a time, ensuring delivery and often preserving order. Best for point-to-point communication.
    • Pub/Sub: Broadcasts messages to multiple subscribers simultaneously, making it ideal for event-driven systems where many services need to react to the same event.
  • Pub/Sub vs. Streaming Platforms
    • Streaming Platforms (e.g., Kafka): Designed for processing continuous streams of data and can retain messages for longer periods. More complex but great for analytics.
    • Pub/Sub: Simpler and geared towards real-time delivery of messages without extensive setup.
  • Pub/Sub vs. WebSockets
    • WebSockets: Enable real-time, bidirectional communication between clients and servers. Best for applications like chat.
    • Pub/Sub: Decouples publishers and subscribers, allowing for multiple subscribers to receive the same message without needing direct connections.
  • Pub/Sub vs. HTTP APIs
    • HTTP APIs: Follow a synchronous request-response model where clients wait for replies. Good for direct interactions.
    • Pub/Sub: Allows asynchronous communication, letting publishers send messages without waiting for subscribers to respond

When and When not to Use Pub/Sub Architecture

1. When to Use the Pub/Sub Architecture

Use Pub/Sub Architecture when:

  • Subscribers don’t need to know about each other, making the system more flexible and easier to scale.
  • Pub/Sub helps you build systems that can grow easily. You can add more publishers or subscribers without disrupting the existing setup.
  • If you want parts of your system to communicate without waiting for each other, Pub/Sub is a great option. Publishers can send messages without needing to wait for subscribers.
  • This approach is perfect for event-driven systems. Publishers can send out events, and subscribers can respond to those events without being tightly linked together.
  • With Pub/Sub, subscribers can change their interests at runtime. They can subscribe to different topics or types of messages, adding more flexibility to the system.

2. When Not to Use the Pub/Sub Architecture

Donot use Pub/Sub Architecture when:

  • If you need very quick communication between parts of your system, Pub/Sub might not be the best option. The process of routing messages and managing subscriptions can slow things down.
  • Using Pub/Sub can make your system more complicated, especially when it comes to routing messages and handling subscriptions.
  • Pub/Sub doesn’t guarantee that messages will be delivered in the order they were sent. If your application needs messages to arrive in a specific sequence, Pub/Sub may not work for you.
  • For smaller applications where a few components communicate directly, using Pub/Sub could add unnecessary complexity that isn’t needed.

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 and Challenges of Pub/Sub Architecture

1. Benefits of Pub/Sub Architecture

Below are the benefits of Pub/Sub Architecture:

  • Scalability: Pub/Sub systems can easily expand to accommodate numerous publishers, subscribers, and messages. This flexibility comes from the separation of publishers and subscribers, along with the use of message brokers to manage how messages are sent out.
  • Decoupling: In a Pub/Sub setup, publishers and subscribers are kept separate, allowing them to function independently. This separation simplifies the design and maintenance of the system, making it easy to add or remove components as needed.
  • Asynchronous Communication: Pub/Sub enables asynchronous communication between components, enhancing the responsiveness and efficiency of the system. Publishers can send messages without having to wait for subscribers, who can take their time processing the messages.
  • Reliability: Pub/Sub systems are designed to be dependable, incorporating features that ensure messages are successfully delivered. This includes things like message acknowledgments, retries, and strategies to deal with faults.

2. Challenges of Pub/Sub Architecture

Below are the challenges of Pub/Sub Architecture:

  • Message Ordering: Pub/Sub systems usually don’t guarantee the order in which messages reach subscribers. This can create problems for applications that need messages to arrive in a specific sequence, as subscribers might get them out of order.
  • Exactly-once Message Delivery: Achieving exactly-once message delivery can be tricky in Pub/Sub systems, especially when there are failures or network problems. Creating methods to ensure messages are delivered once without duplicates can be complicated.
  • Latency: Pub/Sub systems can introduce delays because of how messages are routed and delivered. Balancing low latency with scalability and reliability is especially challenging for real-time applications where speed is crucial.
  • Complexity: Setting up a Pub/Sub architecture can add complexity, particularly in large-scale environments. Managing subscriptions, routing messages, and keeping everything consistent across distributed components requires careful planning and oversight.

Pub/Sub Vs. Point to Point Messaging

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

AspectPub/Sub MessagingPoint-to-Point Messaging
Message DeliveryMessages are broadcast to multiple subscribersMessages are delivered to a single receiver
Subscriber KnowledgePublishers do not need to know about subscribersSender needs to know the receiver
ScalabilityScalable, as new subscribers can be added without affecting publishersLess scalable, as each message is sent directly to a specific receiver
CouplingLoosely coupled, as publishers and subscribers are decoupledTightly coupled, as sender and receiver are directly connected
Use CaseSuitable for broadcasting messages to multiple recipientsSuitable for one-to-one communication
Example TechnologyGoogle Cloud Pub/Sub, Amazon SNS/SQS, KafkaJMS, AMQP, RabbitMQ



Similar Reads