I will start with the question why reactive programming?
These are some of the challenges as far as an application’s responsivness is concerned.
There are lot of IO intensive operations in modern enterprise applications, like invoking a 3Rd party API, reading/writing a file, querying database
Network latency experienced during the interactions of distributed systems is another issue
Most of the code is written in imperative in fashion. It is only modified when a bottleneck is found.
Scaling applications based on resource utilization is easy on cloud but are we effectively using the available resources?
Java developers by default write blocking code. It's just how the API was set up. One example would be the traditional servlet approach.
A servlet based container like Tomcat spawns thread per request and waits for the whole background process to finish in order to send the response back
The server thread represented in green color is in waiting state until the worker thread represented in yellow color finishes it’s task.
When we compare these two executions, clearly it is visible for us that the amount of time taken by the I/O task is effecting the total execution time of the procedure.
the default thread pool size of Tomact is 200, which means it can handle 200 requests simultaneously. As number of request increase the application will start degrading.
Ask question here – Which one do you think would help us to improve the reponsivness of the system?
I’m going to give you analogy for these computing models.
Example with cake and sing – Concurrent and parallel
Say, ther e is an award night event – there are lot of singers, the committee came up with a challenge, sing Ganam style and eat a cake, whoever completes both tasks in shortest time wins. You can see Elvis Presley with a cake and mic, ready to charge.
Elvis has 2 stratergies in mind;
Sing first 20 lines, then eats 2 pieces of the cake, he can continue like that until he finish it off. Or he can eat half of the cake, sing half of the song, and so on.
He can say that he likes to work as a team, ask someone to eat the cake and he can concentrate on the song. This is parallel programming/parallel computing.
What about async?
Explain async using email example.
If we use the thread based concurrency model, we still nee to wait for the dependent task to complete, so it’s blocking.
We can increase the no of threads to allow more parallel execution - this would increase the resource utilization, we need to increase the compute of the nodes where the application is running – the # of process core and memory should be increased. It is vertical scaling. We know vertical scaling has it’s own disadvantages. so we can horizontally scale it. This is what we were doing so far.
So parallel computing is not a silver bullet. We are not doing anything to effectively use the compute while threads are in blocking state.
IO bound tasks can be performed in an asynchronous and non-blocking fashion without blocking the current thread
If the application is asynchronous, with minimum threads we can complete the tasks.
We understood that asynchronous programming is good. Is that what reactive programming is all about? No, there is more to it.
In Java, you can write code asynchronously using Callbacks and Futures. You can then get and join threads at some later point in time and process the result. Java 8 introduced us with a new class - CompletableFuture, which makes it much easier to coordinate these things.
It works in a simple fashion - when a single process ends, another one starts. After the second one ends, the results are combined into a third process.
This makes it a lot easier to coordinate your application, but it's still ultimately blocking as it creates Threads and waits upon calling a .join() method.
When you moved to Banglore, you called up the packers and movers to move your stuffs. These two people are carrying your stuffs from the truck to the new apartment.
The person at receiving end might collapse if he try to handle more than what he can take.
The person at receiving end can say ..
The Wikipedia definition of backpressure is
Resistance or force opposing the desired flow of fluid through pipes.
In the software terms;
Resistance or force opposing the desired flow of data through software.
When the load on the application is high we get to see
OutOfMemoryException
If the consumer can talk to the producer/ event emitter about the load it can take at a time, we can avoid cascading failures.
Backpressure Strategies TODO
Control the producer (slow down/speed up is decided by consumer)
Buffer (accumulate incoming data spikes temporarily)
Drop (sample a percentage of the incoming data)
Reactor is a reactive library, based on the Reactive Streams
"Reactive Streams" defines an API specification that contains a minimal set of interfaces that expose ways to define operations and entities for asynchronous streams of data with non-blocking backpressure.
The high level of abstraction with Reactive Programming leads to an increased readability of code so developers can focus primarily on the interdependence of events that define the business logic.
When we deal with functions which produce stream of data it can produce no value, one value or many values. There are 2 publishers which are main building block of Reactor.
The image that you are seeing is called marble diagram. You can get marble diagram of the operators from the Java doc.
It can produce stream of event without any restriction, a schedular which produces an event every 1 sec is an example for unbounded publisher.
Use case – On a shopping website, Show top five suggestion for a favorite product on the landing page. The favorites will be based on output of recommendation engine. If we didn’t get response with in 800 seconds get it from the cache.
Tomcat and Jetty are supported on both sides, they’re used through different APIs in each stack. On the servlet stack they’re used through the Servlet API with blocking I/O. On the reactive stack they’re used through Servlet 3.1 non-blocking I/O without exposing the Servlet API
Request mappings can be done in functional programming style.
How requests to server like Netty are treated – There is a stack with events, a even loop thread picks that event and give it to handler, the event loops thread keeps on probing for events from the queue. When handler completes it’s task, puts it backs to the event queue.
To get the best out the WebFlux we need to make sure there are no blocking components in the stack. Database layer is one of the blocking component, there is a reactive driver for MySQL based on R2DBC, however, it’s not widely adapted.
RestTemplate is replaced by WebClient in the WebFlux.