Skip to main content

Publisher-Subscriber Architecture

Description

The Publisher-Subscriber pattern is a software architecture pattern that allows for communication between components without direct dependencies. In this pattern, a component (the publisher) sends messages to a channel, and other components (subscribers) listen for messages on that channel. This pattern is useful for decoupling components and enabling asynchronous communication.

In most implementations a message broker is used to manage the channels and messages. The message broker is responsible for routing messages from publishers to subscribers. This allows publishers and subscribers to be developed independently and to be scaled independently.

Diagram

Client Server Basic Diagram

When to Use

This pattern is a good option if the time or resources required to process messages is significantly different than the time needed to produce them. It is also useful when the number of subscribers is unknown or may change over time and when messages can come from multiple sources.

Advantages

There are many advantages to using the Publisher-Subscriber pattern:

  • Data publishers operate completely asynchronously from the subscribers
  • Little risk of performance degradation due to some consumer experiencing issues
  • Adding or removing subscribers to a topic is a simple configuration change
  • Supports scaling efficiently

Disadvantages

There are some disadvantages to using the Publisher-Subscriber pattern:

  • Testing can be a headache. A message must be sent into the system, and then the test needs to observe the behavior of the component under test to see when and how it handles the message
  • This gets even more complicated if the process needs to consume many messages over time
  • Unexpected surges in message emission can cause bottlenecks in the network or may result in data loss in the message broker