Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between Reactive Programming and Message Queue

I'm busy with Reactive concept these days. I have understood two separate concepts as Reactive System and Reactive Programming. In addition, I know Reactive System is a larger concept which contains four properties:

  1. Responsive
  2. Resilient
  3. Scalable
  4. Event-Driven

enter image description here

The Image Reference: medium.com

My problem is about Reactive Programming, I understood that its goal is Asynchronous Programming by Observable/Subscriber model. enter image description here

The Image Reference: https://hub.packtpub.com/introduction-reactive-programming/

Now I'm confused about the difference between Reactive Programming and Message Queue. I have had some experience in Message Oriented Middleware and related standards such as JMS and I think Reactive Programming is the same using messaging queue in listener mode not blocking mode.

I wanted to be clear in Reactive Programming real concept.

like image 232
Sam Avatar asked Aug 16 '18 06:08

Sam


People also ask

How is reactive programming different?

Reactive programming describes a design paradigm that relies on asynchronous programming logic to handle real-time updates to otherwise static content. It provides an efficient means -- the use of automated data streams -- to handle data updates to content whenever a user makes an inquiry.

What is the difference between asynchronous and reactive programming?

Asynchronous programming just means that you're writing code that will not be executed immediately, as in imperative programming, but at "some point in the future". Reactive programming is characterized by the execution of the asynchronous code being triggered by the arrival of data to execute on.

Is reactive programming the same as event-driven?

So what is Reactive programming? It is a declarative, event-driven programming paradigm concerned with data streams and the propagation of change. It is the availability of new information that drives the logic forward rather than having control flow driven by a thread-of-execution.

What is reactive programming used for?

Reactive programming is a design approach that uses asynchronous programming logic to handle real-time adjustments to typically static information. It provides an efficient mechanism — the use of automated data streams — for handling content modifications in response to user inquiries.


2 Answers

Reactive Programming is a new name for old concepts. It means prefer event-driven computing over request-reply computing. Or 'push' versus 'pull'. So instead of writing code that waits for something to arrive, you define callbacks that are executed when something happens. The Observer pattern is a good example of reactive programming and so is the Hollywood Principle ("Don't call us, we'll call you").

In JMS terms, you are doing reactive programming if you defined a MessageListener on your consumer: MessageConsumer.setMessageListener(MessageListener listener)

Then your API or other code can decide what to process in the meantime and if something arrives, from a queue or topic, that callback is invoked.

The alternative, using MessageConsumer.receive(long timeout), is not reactive programming. You are blocking your current thread until the next message arrives.

Thinking in an event-driven or reactive way takes a bit of a leap sometimes, but it is worth the effort.

When looking at the system itself, I would argue that an infrastructure built with topics is a reactive system. An infrastructure built with queues is not.

like image 104
Axel Podehl Avatar answered Oct 19 '22 18:10

Axel Podehl


In synchronous programming, blocking queues are widely used. If consumer does not manage to process messages in timely manner, blocking queue suspends producer thread in order to avoid excessive consumption of memory for messages.

In synchronous world, thread blocking must be avoided, so blocking queues are not acceptable. Instead, reactive streams are used, which inform producer how many messages it is allowed to send.

Technically, a reactive stream is a usual stream of messages plus backward stream of permissions. Permissions are like undistinguishable messages, which need not to be stored, but only counted. In synchronous world, permissions are stored (counted) by java.lang.Semaphore and java.lang.CountdownLatch. Unfortunately, most asynchronous libraries do not provide developer with separate constructs to handle permissions. The only exception I know is my own library df4j (can be found at Github).

Except for (implicit) notion of asynchronous semaphore, I could not find any other sense in all that reactive programming hype.

like image 21
Alexei Kaigorodov Avatar answered Oct 19 '22 19:10

Alexei Kaigorodov