I am new to reactive programming. I know reactive programming is asynchronous and non blocking.I am trying to understand how is reactive asynchronous.
From my understanding, in contrast to thread per request programming, thread will become free until we receive a response back from the server(rest call /db call etc).
1)So this is the only advantage of reactive programming(i am sure reactive systems provide more resilient and has high performance when number of requests are high but i am trying to understand how exactly?)
2)And even this is really confusing when debugging as when we are doing some operation on a mono/flux it just continues to the next lines even if the value is not back from the service call.
3)how to understand all methods (operations like flatmap, transferdeferred etc) in mono and flux . I did go though the documentation but looking for a better source of explanation..any suggestions on this please?
I did go through some threads like below, but looking for a better sources for understanding better. Any suggesstions please?
https://www.baeldung.com/java-reactive-systems
CodePudding user response:
The key factor in terms of performance is the fewer number of threads used in reactive systems. Fewer number of threads results in less resource usage (CPU, memory) which in turn results in better scalability. In blocking applications more threads use more memory and require more context switching which requires CPU time.
Regarding your debugging experience, it is important to understand the difference between assembly time and subscription time. A couple of useful learning material for that:
- https://spring.io/blog/2019/03/06/flight-of-the-flux-1-assembly-vs-subscription
- https://stackoverflow.com/a/59555120/6051176 (this is more about what happens in subscription phase)
You don't need to be familiar with all the operators at the start. I recommend checking Which operator do I need? section of the Reactor documentation. It gives a nice overview of the available operators categorized by their purpose.