GUIDE

What is Publish-Subscribe (Pub/Sub Model)?

Publish:Subscribe (Pub:Sub) -Pillar Page.jpg

What is Publish-Subscribe (Pub/Sub Model)?

In Pub/Sub, publishers send events & massages to the service, which then delivers these events to subscribers. This asynchronous communication model enhances system flexibility and robustness by not requiring publishers to wait for subscribers to receive data. Using the Pub/Sub pattern, publishers (sources of data) push messages out to any number of subscribers (receivers of data) that have subscribed to their topics. Different pub/sub topics are transmitted via live-feed data streams known as channels. 

Pub/Sub is design to be highly scalable, great for streaming, IoT, telemetry, signaling devices, and other on-demand uses. The inherent decoupling of publishers from their subscribers allows for real-time functionality. Messages can be sent independently and asynchronously to unlimited subscribers with very low latency.

How does Pub/Sub work?

In Pub/Sub messaging, publishers—the party that sends data—push messages to any number of interested subscribers. Devices can subscribe to multiple channels and only receive message delivery from their subscribed topics. These messages are asynchronous and completely separate from each other. Decoupling the data producer and its consumers allows for high-volume messaging and easy scalability.

  • Publisher: Also known as a producer, a publisher is any component that creates messages and sends (publishes) them to a messaging service on a specified topic.

  • Subscriber: Also known as a consumer, a subscriber is any component that receives messages on a specified topic.

  • Message: A message is data that is published or transported through the messaging service. A message includes a header (metadata) and a payload, the intended information.

  • Topic: A topic is a specific category of information sent in publish/subscribe messaging. 

  • Channel: Information on specific topics is sent through data streams called channels. A topic has an active channel if there is at least one subscriber. 

Pub/Sub publishers do not know their subscribers, unlike client-server connections where data is sent directly. Instead, publishers send information to topic-based channels. Publishers can also subscribe to other channels, and data streams can be multiplexed, supporting one-to-one, one-to-many, and many-to-one communication. This setup ensures equal workload distribution and reliability.

Subscribers often use a message broker to manage their subscriptions, ensuring data is organized and delivered correctly. For example, MQTT, an ISO communication protocol, uses the Pub/Sub pattern, and many open-source brokers support its implementation.

Pub/Sub guarantees data is sent to subscribers in the order received by the publisher, ideal for real-time applications like stock trading apps or live scoreboards.

Pub/Sub vs. Traditional messaging

Traditional messaging follows a point-to-point (p2p) model where messages are sent directly from a sender to a specific receiver, requiring the sender to know the receiver's address. This creates tight coupling between the sender and receiver, making it difficult to scale the application dynamically or add new recipients.

In contrast, pub/sub messaging uses a publish/subscribe model where publishers send messages to a topic or channel without knowing the receivers' identities or locations. Subscribers register to receive messages from specific topics or channels, and the pub/sub system handles message delivery to all interested subscribers.

One major advantage of pub/sub over traditional messaging is scalability. Traditional messaging can become a bottleneck as the number of recipients grows because the sender must individually handle message distribution. In pub/sub, the system manages distribution, allowing for easy scaling by adding more subscribers without impacting the sender.

Pub/sub also offers flexibility and decoupling of components. Unlike traditional messaging, where the sender and receiver are tightly coupled, pub/sub allows publishers to send messages to a topic without knowing the specific subscribers. Subscribers register to receive messages independently, making it easier to add or remove recipients dynamically and scale the application.

Additionally, pub/sub ensures that data is sent to subscribers in the exact order it is input and received by the publisher, which is crucial for real-time apps. Traditional messaging may not guarantee message order since messages are sent directly from sender to receiver.

Regarding security, pub/sub messaging can provide added benefits by adding a layer of anonymity and protection, as publishers do not need to know the identities or locations of subscribers. This is particularly important for applications where privacy and security are paramount, such as in healthcare or financial systems.

Part of a larger system

Pub/Sub messaging is usually part of a backend communication infrastructure and is included within a larger message-oriented middleware system, like Java’s JMS. Another common paradigm included in middleware systems is the message queue. 

Message queues also send data asynchronously, but instead of transmitting live feeds, their data is stored until the receiving party chooses to access it. Messages in a queue are intended for only one receiver, and data in the queue will usually be wiped after the targeted receiver reads it. Pub/Sub data payloads go out to all subscribers before deleting.

Backend APIs usually include Pub/Sub and message queue models as part of their framework. They serve different needs and use cases. Pub/Sub services are more scalable and optimize lower latency, but Pub/Sub channels are less flexible than message queues.

What are the benefits of using Pub/Sub?

Scalability

One of Pub/Sub’s greatest advantages is that it allows systems to scale dynamically. Pub/Sub decouples publishers from their subscribers, which allows them to work independently. You can then develop and scale each component without worrying about ripple effects in other areas. 

Most importantly, this independence means it is possible to directly scale as needed without overloading any program components. Pub/Sub messaging can scale Internet-wide and with global reach by distributing resources.

Low latency

Pub/Sub messaging is programmed so that each component can minimize shared information. In the Publish/Subscribe method, communication is event-based and uses notifications. There is no need to poll servers for information, decreasing latency.

Loose coupling of systems

Since the publishers and subscribers are decoupled, no service dependencies are required to transport and deliver the data. For example, in traditional coupling, like a client-server relationship, a client requires the server to be running to send a message to the server. With Pub/Sub, separate publishers and subscribers are not concerned with the state of the other systems, which increases delivery consistency.

Balanced workflows

Pub/Sub is useful for managing workflows. For example, it is extremely efficient at moving data between databases and in storage. A large queue of tasks can be easily distributed among multiple devices for the most balanced workload.

System alerts and notifications

Pub/Sub simplifies communication and allows devices to push real-time notifications. This allows instant delivery of critical updates and alerts on the backend and to any front-facing clients. 

Pub/Sub Use Cases

The Pub/Sub pattern is often used within automation, networking operations, and data centers, among many other industries that need real-time computing. Pub/Sub messaging is useful for serverless computing and distributed cloud-based systems, like the Google Cloud Platform (GCP) or Amazon Web Services (AWS).  

IoT is a very common use case for pub/sub messaging. Think of all the smart devices you may use daily - your phone, car, watch, maybe even your home appliances- these are all IoT endpoints. IoT technology can publish messages and push notifications to an unlimited number of these connected devices in realtime. IoT updates are a great example of the scalability and relevant routing that a Pub/Sub messaging service can accomplish.

Pub/Sub methodology is also used in microservices architecture. Microservices are a collection of loosely coupled, decentralized, and independently deployed data. An example of a platform built off microservices would be Netflix. (Kubernetes is a platform for complex applications comprised of multiple microservices).

PubSub messages are behind so many interesting and useful real-time technologies. They can power live dashboards during virtual events, move players in a mobile game, and turn home devices (like a thermostat or security system) on and off from your phone. The possibilities when using Pub/Sub are endless.

What are some disadvantages of using Pub/Sub?

Failed message deliveries 

The most prominent issue with Pub/Sub communication stems from one of its biggest advantages, the decoupling of data producers and their receivers. Failed message deliveries can be an issue with Pub/Sub services since they have no connection or communication. Publishers need to find out if their messages reach all subscribers, and subscribers cannot know if they receive all communications from a publisher. 

Subscribers must assume they are receiving all published messages, which can cause issues if they miss certain information. This is a well-known concern that can have disastrous effects, and different APIs aim to fix this issue with additional parameters. Using a message broker also helps to guarantee message delivery.

Instability with quick scaling

Without additional checks and balances, instability can occur when a Pub/Sub program scales quickly. This can run the risk of a load surge when the number of subscriber requests saturate network bandwidth for some time and causes a slowdown.  

Dynamic scaling may be unnecessary

The Pub/Sub paradigm can be unnecessarily intricate for smaller-scale operations. If a program is unlikely to need to scale exponentially, other methods like polling may be better suited for the use case.

How PubNub implements Pub/Sub

The Pub/Sub messaging paradigm is one of the core foundations of PubNub’s realtime technology. Our technology relies on the unique ability to publish and deliver any volume of messages in less than 100ms. This allows products to reliably stream data, update live dashboards, and broadcast chats.

PubNub is a real-time communication platform that helps build applications with APIs and SDKs. We can integrate your existing technology stacks using your preferred languages and third-party APIs. The PubNub Data Stream Network offers global scalability and unmatched reliability.

Implementing Pub/Sub messaging using a PubNub framework is super simple. We have also published multiple documents that assist with programming a messaging application.  

With over 15 points of presence worldwide supporting 800 million monthly active users and 99.999% reliability, you’ll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes. PubNub is perfect for any application that requires real-time data.

Sign up for a free trial and get up to 200 MAUs or 1M total transactions per month included.

Read more on this topic