What is a Pub/Sub System?

PB
SystemDesign.us Blog
3 min readNov 28, 2022

Visit systemdesign.us for System Design Interview Questions tagged by companies and their Solutions. Follow us on YouTube, LinkedIn, Twitter, Medium.

Pub/Sub system is a messaging system that allows users to subscribe to specific topics and receive messages only from the topics they are subscribed to. This can be useful for a variety of applications, such as social networking, news feeds, and gaming.

How does pub/sub work?

The publisher (or message sender) sends a message to a specific topic. The message is then received by all subscribers (or message receivers) of that topic. Subscribers can choose to unsubscribe from a topic at any time.

Pub/Sub Components

https://aws.amazon.com/pub-sub-messaging/

There are three main components in a pub/sub system:

topics: messages are organized into topics, and users can subscribe to specific topics to receive the messages.

publishers: publishers are the message sender, and they send messages to specific topics.

subscribers: subscribers are the message receiver, and they receive all messages from the topics they are subscribed to.

Why use pub/sub?

Pub/sub can be useful for applications that require real-time updates from a variety of sources. For example, a social networking app may need to show users updates from their friends, as well as news stories and other content. By using pub/sub, the app can receive all of these messages in real time, without having to constantly polling for new data.

What are the benefits of pub/sub?

There are several benefits of using pub/sub:

  • Real-time updates: Subscribers can receive messages as soon as they are published, without having to wait for a regular polling interval.
  • Flexible subscriptions: Subscribers can choose to subscribe to only the topics that they are interested in, and can unsubscribe at any time.
  • Scalability: The pub/sub system can easily scale to handle a large number of topics and subscribers.
  • It allows for real-time communication between publishers and subscribers.
  • It is a scalable system that can handle a large number of topics and subscribers.
  • It is flexible, as subscribers can choose which topics they want to receive messages from.

What are the drawbacks of pub/sub?

There are some potential drawbacks to using pub/sub:

  • Message delivery is not guaranteed: Since messages are sent asynchronously, there is no guarantee that they will be received by all subscribers. This means that some messages may be missed if the subscriber is offline or the message is delayed for some reason.
  • Higher latency: Asynchronous message delivery can result in higher latency than synchronous message delivery (such as with a traditional messaging system).
  • Limited message history: Most pub/sub systems only keep track of the most recent messages for each topic, meaning that subscribers will only receive new messages after they have subscribed. This can be a problem for applications that require a complete history of messages.
  • The publisher must know the topic of the message in order to send it to the correct subscribers.
  • Subscribers may receive messages they are not interested in if they subscribe to too many topics.
  • There is no guarantee that messages will be delivered in order, or at all.

Despite these drawbacks, pub/sub can be a useful tool for applications that require real-time communication or large-scale messaging.

List of Pub/Sub technologies

  • Apache Kafka
  • Amazon Simple Notification Service (SNS)
  • Google Cloud Pub/Sub
  • Microsoft Azure Event Grid
  • IBM Cloud Pub/Sub
  • NATS Streaming Server
  • CloudEvents
  • MQTT
  • STOMP
  • Webhooks
  • Apache ActiveMQ Artemis
  • RabbitMQ

Visit systemdesign.us for System Design Interview Questions tagged by companies and their Solutions. Follow us on YouTube, LinkedIn, Twitter, Medium.

--

--