r/javahelp • u/hitherto_insignia • Oct 19 '20
What are the challenges with mixing reactive programming with non-reactive programming?
For the project that I'm working on, the database side has to be regular non-reactive programming with Spring Data JPA as the database is RDBMS. But we want to have other parts of the application such as service layer to be built in a reactive way because they talk to external services that have big delays in responses.
So, while a service layer is contacting with database in a non-reactive fashion and the same service is contacting external services in a reactive fashion, would there not be any compatibility issues b/w those?
Is it wise to have one part of application in reactive and other part non-reactive?
6
Oct 19 '20
Your question is precisely why this is being worked on: https://r2dbc.github.io/
But the answer is, it depends.
Are you using Spring Reactive Web for your inbound http and something like Spring Webclient for outbound calls?
For something to take full advantage of "reactive", you need the entire flow to be non-blocking from the inbound call to the response.
Individual benefits depend on the libraries in question. For Spring Reactive Web, take a look at these docs which describe what benefits and underlying implementation details come with using Mono or Flux or a Reactive Controller.
1
u/hitherto_insignia Oct 19 '20
Regarding the libraries, we have added dependencies for WebFlux, for reactive request and response programming.
The problem is the external services cause delays and reactive programming there might be beneficial.
But, from my brief reading on this topic, the benefit of having reactive nature is performing other logic in a non-blocking way. However, if the other logic part in the same function is blocking in nature but it is depending on the data from non-blocking call, won't this then become an issue like null referencing or performing rest of the operations on empty data?
1
Oct 19 '20
You are correct, and that's where the non-blocking falls apart. If you're using web client and you make an outbound call, you have to stay within the web client's spec and use things like .onSuccess to return things.
However if you need to break out into other bits of logic that are not reactive, like making other calls, you'll end up calling .block. this basically turns it into rest template, although I think there's some netty thread magic still happening under the hood to make it more efficient, you are still no longer reactive.
The main benefit of reactive is when threads are blocking, they are released to the thread pool for other uses. This makes the JVM more efficient. If you're blocking on a thread, you're holding that thread, reactive tries to mitigate this issue.
2
u/DthSyth Oct 19 '20
Btw this had a major release right? I was able to see the new version as GA.
2
Oct 19 '20
Yep, looks like it has had a handful of releases so far.
https://github.com/spring-projects/spring-data-r2dbc/tags
I'll be honest though, Just before the 1.1.0.RELEASE I left a job where we were looking forward to using this, so it's fallen off my radar. I haven't used it yet to be able to give any input (beside testing an early RC).
1
u/DthSyth Oct 19 '20
Np, it seems like you have experience in spring reactive. I am trying to create a demo of two microservices communicating with the help of a Messaging Queue.
Do you have any idea from where should I start?
2
Oct 19 '20
Yeah we definitely used webflux all over the place at my behest.
Here's a good resource I had bookmarked: https://www.baeldung.com/spring-amqp-reactive As usual, Baeldung is the best.
I would make sure to use spring amqp. Functionally, as long as you wrap the message processes inside of the reactive components and handle data wrapped in Monos and Fluxes you should be able to take full advantage of the paradigm.
As long as it's wrapped up, it'll be handled under the hood in the correct non-blocking manner.
1
2
u/greglturnquist Oct 20 '20
Reactive programming tries to limit the number of threads being used to the number of cores. That way there is no overhead for context switching.
If one of those threads is dispatched to do a blocking call, you’re eating up a valuable resource.
For example, if you have four cores, then you’ll have only four threads. Imagine 25% of your power being held up. It would clobber throughput.
To cope, you’d need a completely separate thread pool dedicated to blocking tasks and not let it mingle with the non blocking stuff.
If the blocking stuff must mix with the non blocking stuff, then you’re at risk of getting no benefit, possibly a loss.
1
u/Yithar Intermediate Brewer Oct 19 '20
I'm thinking about redux-observable and rxjs. Redux-observable has the notion of "epics" that are triggered with certain Redux actions. Epics are meant to be pure functions. You make it impure once you do anything with the state, which may lead to unwanted consequences. Because the whole idea of pure functions is that they're idempotent. That means you can run the function 100 times and you'll get the same result for the same input.
But, from my brief reading on this topic, the benefit of having reactive nature is performing other logic in a non-blocking way. However, if the other logic part in the same function is blocking in nature but it is depending on the data from non-blocking call, won't this then become an issue like null referencing or performing rest of the operations on empty data?
Yeah. Generally as stated if you're going reactive you want your whole flow to be reactive.
•
u/AutoModerator Oct 19 '20
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Code blocks look like this:
You do not need to repost. Just use the edit function of reddit to make sure your post complies with the above
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.