Reactive Programming Advantages/Disadvantages

人走茶凉 提交于 2019-11-26 19:44:42

问题


I keep studying and trying Reactive Style of coding using Reactor and RxJava. I do understand that reactive coding makes better utilization of CPU compared to single threaded execution.

Is there any concrete comparison between reactive programming vs imperative programming in web based applications?

How much is the performance gain, throughput I achieve by using reactive programming over non-reactive programming?

Also what are the advantages and disadvantages of Reactive Programming?

Is there any statistical benchmark?


回答1:


Well, Reactive Programming means you are doing all your IO bound tasks such as network calls asynchronously. For an instance say your application calls an external REST API or a database, you can do that invocation asynchronously. If you do so your current thread does not block. You can serve lots of requests by merely spawning one or few threads. If you follow blocking approach you need to have one thread to handle each and every request. You may refer my multi part blog post part one, part two and part three for further details.

Other than that you may use callbacks to do the same. You can do asynchronous invocation using callbacks. But if you do so sometimes you may ended up with callback hell. Having one callback inside another leads to very complex codes which are very hard to maintain. On the other hand RxJava lends you write asynchronous code which is much more simple, composable and readable. Also RxJava provides you a lots of powerful operators such as Map, Zip etc which makes your code much more simple while boosting the performance due to parallel executions of different tasks which are not dependent on each other.

RxJava is not another Observer implementation with set of operators rather it gives you good error handling and retry mechanisms which are really handy.

But I have not conducted any bench marking of RxJava with imperative programming approach to commend you statistically. But I am pretty much sure RxJava should yield good performance over blocking mechanisms.

Update

Since I gathered more experience over time, I thought of adding more points to my answer.

Based on the article, ReactiveX is a library for composing asynchronous and event-based programs by using observable sequences. I reckon you to go through this introductory article in the first place.

These are some properties of reactive systems: Event Driven, Scalable, Resilient, Responsive

When it comes to RxJava it offers two main facilities to a programmer. First it offers a nice composable API using a rich set of operators such as zip, concat, map etc. This yields more simple and readable code. When it comes to code, readability and simplicity are the uttermost important properties. Second, it provides excellent abstractions, that enable concurrency to become declarative.

A popular misconception is that Rx is multithreaded by default. To be honest, Rx is single-threaded by default. If you want to do things asynchronously, then you have to tell it explicitly using subscribeOn and observeOn operators by passing relevant schedulers. RxJava gives you thread pools to do asynchronous tasks. There are many schedulers such as IO, Computation and so forth. IO scheduler as the name suggests is best suited for IO intensive tasks such as network calls etc. In contrary Computation scheduler is good for more CPU intensive computation tasks. You can also hook up your own Executor services with RxJava too. The built in schedulers mainly helps you to get rid of maintaining your own Executor services, making your code more simple.

Finally a word on subscribeOn and observeOn

In the Rx world, there are generally two things you want to control the concurrency model for:

  1. The invocation of the subscription
  2. The observing of notifications

SubscribeOn: specify the Scheduler on which an Observable will operate.

ObserveOn: specify the Scheduler on which an observer will observe this Observable




回答2:


Disadvantages

  • More memory intensive to store streams of data most of the times (since it is based on streams over time).
  • Might feel unconventional to learn at start(needs everything to be a stream).
  • Most complexities have to be dealt with at the time of declaration of new services.
  • Lack of good and simple resources to learn.

  • Often confused to be equivalent to Functional Reactive Programming.




回答3:


Apart what they already response regarding no blocking features, another great feature to use Reactive programing, is the important use of backpressure. Normally is used in situations where your publisher emit more information than your consumer can process.

So having this mechanism you can control the flow of traffic between both and avoid the nasty out of memory problems.

You can see some practicle examples of Reactive programing here https://github.com/politrons/reactive

And about back pressure here https://github.com/politrons/Akka/blob/master/src/main/scala/stream/BackPressure.scala

By the way, the only disadvantage about reactive programing, is the curve of learning because you´re changing paradigm of programing. But nowadays all important companies respect and follow the reactive manifesto http://www.reactivemanifesto.org/




回答4:


Reactive Programming is a style of micro-architecture involving intelligent routing and consumption of events.

Reactive is that you can do more with less, specifically you can process higher loads with fewer threads.

Reactive types are not intended to allow you to process your requests or data faster.Their strength lies in their capacity to serve more request concurrently, and to handle operations with latency, such as requesting data from a remote server, more efficiently.

They allow you to provide a better quality of service and a predictable capacity planning by dealing natively with time and latency without consuming more resources.

From
https://blog.redelastic.com/what-is-reactive-programming-bc9fa7f4a7fc https://spring.io/blog/2016/06/07/notes-on-reactive-programming-part-i-the-reactive-landscape https://spring.io/blog/2016/07/28/reactive-programming-with-spring-5-0-m1




回答5:


Advantages

  1. Cleaner code, more concise
  2. Easier to read (once you get the hang of it)
  3. Easier to scale (pipe any operation)
  4. Better error handling
  5. Event-driven inspired -> plays well with streams (Kafka, RabbitMQ,etc)
  6. Backpressure (client can control flow)

Disadvantages

  1. Can become more memory intensive in some cases
  2. Somewhat steep learning curve



回答6:


Reactive programming is a kind of imperative programming. Reactive programming is a kind of parallel programming. You can achieve performance gain over single threaded execution only if you manage to create parallel branches. Will they executed by multiple threads, or by reactive constructs (which in fact are asynchronous procedures), does not matter.

The single advantage of reactive programming over multithreaded programming is lower memory consumption (each thread requires 0.5...1 megabyte). The disadvantage is less easy programming.



来源:https://stackoverflow.com/questions/42062199/reactive-programming-advantages-disadvantages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!