Publish-Subscribe (Pub-Sub) Design Pattern

Publish-Subscribe (Pub-Sub) Design Pattern

What is topic based Publish-Subscribe (Pub-Sub) design pattern?

Publishers are the entities who create/publish a message on a topic. Subscribers are the entities who subscribe to messages on a topic.

In a topic based Publish-Subscribe pattern, Publishers tag each message with the a topic instead of referencing specific Subscribers. Messaging system then sends the message to all Subscribers who have asked to receive messages on that topic.

Publishers only concern themselves with creating the original message and can leave the task of servicing the Subscribers to the messaging infrastructure (this is where pattern comes into picture).

Design of the Publish-Subscribe pattern:

Publish-Subscribe Design Pattern
Publish-Subscribe Design Pattern

There are three components in Pub-Sub pattern: Publisher, Subscriber and PubSub Service.

  1. Message Publisher sends messages to the PubSub Service without any knowledge of message subscribers
  2. Message Subscribers will only get the messages for which topics they are registered with the PubSubService. E.g. say we have three different topics (message types): a, b, c but only topic a is of interest to Subscriber 1, topics b and c are of interest to Subscriber 2 and Subscriber 3 wants messages for topics a and c. So subscriber 1 will be notified for topic a, Subscriber 2 will be notified for topics b and c, and Subscriber 3 will be notified for topics a and c by the PubSub Service.
  3. Publishers tag each message with a topic, and send it to the PubSubService which acts like a middleman between Publishers and receivers.
  4. Subscriber registers itself with PubSubService (middleman) and tells that it’s interested in messages related to a particular topic.
  5. Publisher-Subscriber pattern is highly loosely coupled architecture where the publishers don’t know who the subscribers are and subscribers don’t know who the publishers of topic are.
  6. While Pub-Sub pattern is very similar to Observer pattern there are 2 main differences:
    1. In Observer pattern, the observers are aware of the observables. In Publisher-Subscriber pattern the publishers and subscribers don’t need to know each other.
    2. Observer pattern is mostly implemented in a synchronous way i.e. the observable calls the appropriate method of all its observers when some event occurs. The Publisher-Subscriber pattern is mostly implemented in an asynchronous way (using message queue).

Implementation details:

  • Project Directory Structure in Eclipse.
Project directory structure
Project directory structure
  • Publisher interface defines the abstract method publish() which sends message to PubSub Service.

  • PublisherImpl class implements Publisher interface and implements publish method, which sends the message to PubSubService.

  • Subscriber is an abstract class which has below:
    • addSubscriber() – Adds/Registers subscriber for a topic with PubSub service.
    • unSubscribe() – Removes/Unsubscribes the subscriber for a topic with PubSub
    • List<Message> subscriberMessages – List of message which stores the messages received by Subscriber.
    • getMessagesForSubscriberOfTopic() – Method which requests for messages for subscriber of topic.

  • SubscriberImpl class extends Subscriber and implements the abstract methods mentioned above.

  • Message class is a simple POJO class to represent the messages. It has topic attribute (for this subscriber is interested) and an attribute for message payload.

  • PubSubService is the main class which has below:
    • Map<String, Set<Subscriber>> subscribersTopicMap – Stores the subscribers interested in a topic.
    • Queue<Message> messageQueue – Stores the messages published by publishers.
    • addMessageToQueue() – Adds message published by publishers to message queue.
    • addSubscriber () – Adds a subscriber for a topic.
    • removeSubscriber() – removes a subscriber for a topic.
    • broadcast() – Broadcast new messages added in queue to all subscribers of the topic. The messagesQueue will be empty after broadcasting is complete.
    • getMessagesForSubscriberOfTopic() – Sends messages about a topic for subscriber at any point.

  • DriverClass is the main class to run and test the Publisher-Subscriber design pattern. This is just for demo purpose and in real system we will have publishers and subscribers doing the same via API calls etc.


Thus we saw the design and implementation details of Publish-Subscribe Design Pattern.

Leave a Reply

Your email address will not be published. Required fields are marked *