11

Publish-subscribe and Reactor patterns looking very similar to me. How they are different?

In both patterns a message is getting passed to subscribers indirectly (listeners in reactor pattern).

I feel observer pattern is very similar to two other patterns too.

What are key differences between those patterns?

Mohsen
  • 1,990

3 Answers3

12

As I can see from the link, PubSub pattern is not an OOP pattern, but rather messaging pattern, which is a network architectural pattern.

Reactor pattern is something related to servers' request handling with single-threaded event loop. Again, the link shows some good examples like Node.js, Netty, Twisted, etc.

Finally, Observer is OOP design-pattern which describes a way of interaction between objects in OO-code.

So those three patterns are from different domains.

scriptin
  • 4,442
6

Reactor = single threaded event loop that receives and processes requests. Those requests could be loaded in batches from a file, via SMTP and/or HTTP. Processed concurrently responses are delivered when ready rather than sequentially.

Publishers emit. Subscribers consume.

Publishers + Subscribers = Observer pattern (Heads First Design Patterns). Observers fire sequentially and usually block until they complete.

2

PubSub is more related to Messaging Queues (MQs). It has to do with how nodes receive asynchronous messages in a distributed system.

Reactor pattern has more to do with asynchronous events. For exemple, non-blocking sockets use the reactor pattern to deliver network events: READ, WRITE, CONNECT, ACCEPT

You can check CoralReactor to have a better understanding of the reactor pattern.

Disclaimer: I am one of the developers of CoralReactor.

rdalmeida
  • 250