Observer vs. Pub-Sub Pattern: What is the Difference

Observer vs. Pub-Sub Pattern

What is the Observer Pattern?

The observer pattern is software design pattern where an object, known as the subject, keeps a list of its own dependents, which are known as observers, and notifies them of any changes in state. The notifications are usually done by calling the observer’s methods. A software design pattern is simply a reusable solution to commonly occurring problems in software design. This pattern allows the subject to be monitored by a dynamic group of observer objects. This means that whenever some value in the subject is changed all the observers will automatically update themselves. Each observer may be coded with different tasks when the subject changes, the subject itself however does not know what the tasks are. Likewise, the observers have no control over events experienced by the subject. The observer pattern falls under the behavioral pattern category.

Also Read: How do you teach machines to recommend?

What is the Observer Pattern used for?

The main used of the observer pattern is the implantation of event handling systems. The subject in this case would be the stream of events while the observers are sinks of events. The observer pattern is also used when there is a one-to-many relationship between objects such as if one object is modified, its dependent objects are then notified automatically.

A real-world use of the observer pattern can be seen on most social media platforms. Whenever a person updates their status, all their followers get a notification. In this case the subject is the account of the person updating their status. The observers are all the followers of the person updating their status and the task performed by the observers because of a change in the subject is getting a notification.

In the programming world the observer pattern is used in messaging applications. When one of these applications updates its state, all subscribers to this notification will be updated. Some frameworks like HornetQ and JMS work using this pattern. Finally, the entirety of Java UI programming is based off the observer pattern. Any event programmed will have a listener and designated functions. The listener in this case is the subject and the functions to be done are the observers.

What is the Pub-Sub pattern?

The publisher-subscriber pattern, pub-sub for short, is a different version of the observer pattern. Unlike the observer pattern though, the senders of messages, known as publishers, do not send messages directly to the receivers, known as subscribers. Instead, there is a middleman known as a broker, also known as message broker or event bus, which receives information from the publisher and sends it to the subscribers. The broker is used also to filter the incoming messages from the publishers and distributes them based on how the broker is coded.

In this pattern the publisher and the subscriber do not even know that the other exists and are thus not dependent on each other. The two most popular methods of filtering messages are topic based and content based.

In a topic-based system, messages will be filtered to specifically named channels known as topics. Subscribers in this system would receive messages only from the topics they subscribed to.

In a content-based system a message will only be sent to a subscriber if the subscriber matches the attribute constraints of the message. The subscriber is the one responsible for creating the attribute constraints. Sometimes though systems will have a hybrid of content-based and topic-based.

What is the Pub-Sub pattern used for?

The pub-sub pattern is used when services want to communicate asynchronously with latency values around 100 milliseconds. One example of where the pub-sub pattern is used is when a person wants to send a notification to many receivers.

To do this the system has to be modeled in a way that allows for the rapid delivery of events without losing efficiency. There is also the possibility that the receiver may be offline, so a model is needed where the sender doesn’t know or care which clients are online or when new clients connect to the system, which is the pub-sub pattern. Another place the pub-sub pattern is used is in application logs. By utilizing the pub-sub pattern, logs can be sent to a great number of subscribed destinations all at the same time.

Source: YouTube

Differences between Observer and Pub-Sub patterns

  • In the observer pattern the subject will know who all its observers are. There is no middleman between the subject and the observers. On the other hand, with pub-sub patterns the publisher and subscriber have no awareness that the other exists. They have a middleman, called a broker, to communicate with each other.
  • The observer pattern is implemented in a synchronous manner. This means the subject will call a method on all its observers when an event occurs. Meanwhile, the pub-sub pattern is implemented in an asynchronous manner. Asynchronous means that events are not happening at the same time. This is usually done using a message queue such as Apache Kafka.
  • The observer pattern in general is only applied in a single application scope. Meanwhile, the pub-sub pattern is used as a cross application pattern.
Observer vs. Pub-Sub Pattern
Observer vs. Pub-Sub Pattern

Also Watch: Amazon and data collection


Despite these differences, it can be argued that the publisher-subscribe pattern is simply a variation of the observer pattern. This is not entirely wrong because there is a conceptual similarity between the two. Thank you for reading this article.


O’Riordan, Matthew. “Everything You Need to Know about Publish/Subscribe.” Ably Realtime, Ably Realtime, 17 July 2020,

Shosse, Khmelnitske. “Observer.” Refactoring.Guru, Refactoring.Guru,