Join our Streaming Lakehouse Tour!
Register Now.->

Publish-Subscribe Pattern

The Publish-Subscribe Pattern (often abbreviated as Pub/Sub) is a messaging pattern where senders of messages, called Publishers (or Producers), do not send messages directly to specific receivers, called Subscribers (or Consumers). Instead, publishers categorize published messages into classes (often called topics or channels) without knowledge of which subscribers, if any, there may be. Similarly, subscribers express interest in one or more topics and only receive messages that are of interest, without knowledge of which publishers, if any, there are.

A central component, often called a Broker or Message Bus, facilitates this communication. The broker filters all messages and distributes them to subscribers based on their topic subscriptions.

Core Components

  • Publisher (Producer): The application or component that creates and sends messages. It classifies messages by topic and sends them to the broker.
  • Subscriber (Consumer): The application or component that registers its interest in one or more topics with the broker. It receives messages only from the topics it has subscribed to.
  • Topic (Channel): A named logical channel to which messages are published. Subscribers subscribe to topics to receive messages sent to those topics.
  • Broker (Message Bus / Event Bus): The intermediary responsible for receiving messages from publishers, filtering them, and distributing them to the appropriate subscribers based on their topic subscriptions. It decouples publishers from subscribers.
  • Message: The data packet that is sent from the publisher to the subscribers via the broker.

How it Works

  1. Subscription: Subscribers register their interest in specific topics with the broker.
  2. Publication: Publishers send messages, tagged with a specific topic, to the broker.
  3. Filtering & Distribution: The broker receives the message, identifies all subscribers who have registered an interest in that topic, and forwards a copy of the message to each of them.

Key Characteristics and Benefits

  • Decoupling: This is the primary benefit. Publishers and subscribers are decoupled in several ways:
    • Space Decoupling: Publishers and subscribers do not need to know each other (e.g., their network locations).
    • Time Decoupling: Publishers and subscribers do not need to be running at the same time. The broker can store messages if subscribers are offline and deliver them when they become available (depending on broker capabilities).
    • Synchronization Decoupling: Operations on both sides (publishing and receiving) can happen asynchronously. Publishers can publish messages without waiting for subscribers to receive them.
  • Scalability: New publishers and subscribers can be added to the system dynamically without affecting existing components. The broker can also be scaled to handle increased message load.
  • Flexibility & Extensibility: Allows for easy addition of new components (subscribers) that react to existing messages without modifying the publishers.
  • Broadcast Communication: A single message published to a topic can be delivered to multiple subscribers interested in that topic.
  • Event-Driven Architectures: The pub/sub pattern is a fundamental building block for event-driven architectures (EDA), where system components react to events (messages).

Use Cases

  • Notifications: Sending out notifications (e.g., email, SMS, push notifications) to users who have subscribed to specific types of updates.
  • Distributed Systems & Microservices: Facilitating asynchronous communication between microservices.
  • Real-time Data Distribution: Distributing real-time data feeds, such as stock market updates, news alerts, or IoT sensor data.
  • Logging and Monitoring: Centralizing log messages from various applications and allowing multiple monitoring tools to subscribe.
  • Fan-out for Parallel Processing: A single event can be fanned out to multiple subscribers that perform different processing tasks in parallel.
  • Cache Updates: Notifying different parts of an application or different services to update their caches when underlying data changes.

Relationship with Message Queues and Event Streaming Platforms

  • Message Queues (Point-to-Point): Traditional message queues often implement point-to-point communication where a message is delivered to a single consumer from a queue. While some MQs can support pub/sub, it might not be their primary design focus.
  • Event Streaming Platforms (e.g., Apache Kafka, Apache Pulsar): These platforms are inherently designed around the pub/sub model using topics. They extend the basic pub/sub concept with features like persistent, ordered logs of messages, replayability, and high throughput, making them suitable for stream processing and handling large volumes of event data.

RisingWave often consumes data from Event Streaming Platforms which utilize the publish-subscribe pattern for data ingestion.

Related Glossary Terms

The Modern Backbone for Your
Event-Driven Infrastructure
GitHubXLinkedInSlackYouTube
Sign up for our to stay updated.