Learnitweb

Publish-Subscribe Messaging (Pub/Sub)

In this lecture, we will explore Pub/Sub messaging, its behavior, and how it differs from traditional message queues. We will also demonstrate a simple implementation with producers and subscribers.


1. Pub/Sub vs Message Queue

Message Queue:

  • A producer adds messages to a queue.
  • One or more consumers process these messages.
  • Each message is consumed only once.
  • Multiple consumers share the workload, and messages are distributed among them.
  • Example: Task processing queues in backend systems.

Key Point: No duplicate processing occurs. If a message is taken by one consumer, it is not available to others.


Publish-Subscribe (Pub/Sub):

  • A publisher sends messages to a topic.
  • Multiple subscribers can listen to the same topic.
  • All active subscribers receive the message simultaneously.
  • Subscribers only receive messages if they are online and listening when the message is published.
  • Example: Live news broadcast, Slack, chat applications.

Key Difference: Unlike queues, messages are broadcast to all subscribers instead of being consumed by only one.


2. Real-Life Example

  • TV news: Only viewers watching at the time of broadcast receive the message.
  • Chat applications like Slack or Discord: All online users in a channel receive messages in real-time.
  • Caching systems: A pub/sub system notifies multiple instances to update their local caches when data changes.

3. Implementing Pub/Sub

Step 1: Create a Topic

  • A topic is a channel through which publishers send messages and subscribers listen.
String topicName = "newsChannel";

Step 2: Subscriber Setup

  • Subscribers listen to the topic for incoming messages.
  • They process messages as they arrive.
client.getTopic(topicName)
      .addListener(String.class, (channel, message) -> {
          System.out.println("Subscriber received: " + message);
      });
  • Multiple subscribers can subscribe to the same topic.
  • Each subscriber receives all messages while they are active.

Step 3: Producer Setup

  • A producer publishes messages to the topic.
  • All active subscribers automatically receive these messages.
client.getTopic(topicName)
      .publish("Hello, Subscribers!");
  • Messages can be any type (String, JSON, custom objects).
  • Ensure subscribers are listening before publishing if they need to receive messages.

4. Behavior of Pub/Sub

  1. Subscribers start before messages are published:
    • They will receive all messages sent while they are online.
  2. Subscribers start after messages are published:
    • They will miss previous messages.
  3. Multiple subscribers:
    • Each receives the same message independently.
  4. Multiple producers:
    • Messages from different producers are merged in the topic and delivered to all subscribers.

5. Demonstration Flow

  1. Start Subscriber 1 → listening on newsChannel.
  2. Start Subscriber 2 → listening on the same channel.
  3. Publish a message: "Hello, World!".
    • Both Subscriber 1 and Subscriber 2 receive it immediately.
  4. Stop Subscriber 2 and publish "Another message".
    • Only Subscriber 1 receives it.
  5. Start Subscriber 2 again and publish "New message".
    • Both subscribers receive "New message", but Subscriber 2 missed "Another message".

6. Key Notes

  • Subscribers must be active to receive messages.
  • Pub/Sub is ideal for:
    • Real-time notifications
    • Chat applications
    • Event broadcasting
    • Cache invalidation across microservices
  • Unlike queues, Pub/Sub does not guarantee message persistence unless backed by a system like Kafka, Redis Streams, or other durable messaging systems.

7. Summary

  • Message Queue: Single-consumer message delivery, task distribution, prevents duplicates.
  • Pub/Sub: Broadcast to all active subscribers, real-time delivery, missed messages if subscribers are offline.
  • Pub/Sub is widely used in chat systems, notifications, and event broadcasting.