Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pub/Sub Vs Observer Vs Reactive

When I have used Pub/Sub pattern frameworks like MVVMLight before, I have seen that the subscriber's calls are handled synchronously. From a scalability point of view, does a reactive framework like Rx help scalability where the pub and sub are completely decoupled and scalable? Which pattern helps scalability?

like image 481
wonderful world Avatar asked Jun 11 '15 14:06

wonderful world


People also ask

Is Pub-sub same as observer pattern?

In the observer pattern, the source of data itself (the Subject) knows who all are its observers. So, there is no intermediate broker between Subject and Observers. Whereas in pub-sub, the publishers and subscribers are loosely coupled, they are unaware of even the existence of each other.

What is the difference between pub and sub?

The major difference between the (real) publisher-subscriber pattern and the observer pattern is this: In the publisher-subscriber pattern, senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers.

When should you not use a pub-sub?

Synchronous point-to-point communication between the two endpoints is the best solution for media streaming. Pub/Sub is not suitable for carrying VoIP or video telephony traffic over the Internet. However, you can still use Pub/Sub to ring the phones.

What is subscriber and Observer?

An Observer is an object that can get data from a data source (an Observable ). The data source pushes data to it by calling the observer's onNext() . A Subscriber is an Observer that can also unsubscribe from that data source (through the Subscription interface).


1 Answers

I don't know the specifics of MVVMLight, but in general the Pub/Sub is a pattern, where:

  • Publishers and subscribers don't know about each other. They only know about a broker, where they publish/consume messages.
  • As a result, the publication and consumption of messages is done asynchronously and is completely decoupled. This means that the publication/consumption side can be scaled independently and in case of failures of one part, the other part is able to keep working.

Now, reactive programming is a pattern used to model changes and their propagation across multiple actors. As such, it's not so much concerned with implementation details, but more focused on providing an abstract, declarative interface, which makes it easier to work with streams of events and perform processing on top of them. Straight from ReactiveX's documentation:

ReactiveX is not biased toward some particular source of concurrency or asynchronicity. Observables can be implemented using thread-pools, event loops, non-blocking I/O, actors (such as from Akka), or whatever implementation suits your needs, your style, or your expertise. Client code treats all of its interactions with Observables as asynchronous, whether your underlying implementation is blocking or non-blocking and however you choose to implement it.

So, the decoupling/scalability will be mainly dependent on the implementation used underneath; the main benefit of the framework is mainly the abstract, declarative interface provided.

Regarding the observer pattern (which is mentioned in the question's title): it's a rather low-level primitive that can be used to achieve the same goal, but can probably lead to a much more complex codebase. For more details on the pitfalls of observer pattern when compared with more abstract reactive frameworks, you can read the following paper:

Deprecating the Observer pattern with Scala.React

like image 113
Dimos Avatar answered Oct 19 '22 03:10

Dimos