Spring 5 Webflux Framework | Reactive Programming Introduction | Simple Programming

preview_player
Показать описание
Spring WebFlux framework is part of Spring 5 and provides reactive programming support for web applications.

In simple terms, it
provides full non-blocking applications that are asynchronous
it provides support to go fully event-driven
It helps become elastic i.e to have a small number of threads and to scale seamlessly

It behaves like a completableFuture, but is feature rich when compared to the later.

Why do we need Reactive Programming

Well, there are four things you need to consider if reactive programming is a thing for you

Responsive: means if we send any events on stream it will return response in fraction of time in highly concurrent environments
 
Resilient: means Application should be responsive at the time of failure
 
Elastic: system should be handle N number of request, it should have the capability to load balance at any condition
 
Message Driven: it is a asynchronous flow of execution, where we no need to wait for response after send request to server , once we send request it move to next item it shouldn’t depend on first request , the first response should be handle by callback mechanism
 

It gives you different components to make use of like

Mono, Flux, to work with

Mono - means can hold a single object
Flux - is a container to hold a collection

WebClient - similar to restTemplate, you can use this to call remote rest services, but is more aligned to work with spring reactive components, as it is fully reactive and gives you a functional feel to work with

In case of RxJava
Single - similar to Mono but used in RxJava
Observable - similiar to Flux but used in RxJava

Why would we need spring web flux
let us say that your servlet container can server 1000 threads at a time
we know when a request comes in, it will be taken up by a new thread, and when it goes to a make a db call or write to a file, there is a IO operation involved in this case, which has a wait and the thread will wait for that io operation to complete, before sending back the response and then take up a new request. what if some issue happens and all the 1000 threads working on the multiple requests are waiting for the io operation to complete, performance will take a hit, and in order to make them solve that these threads needs to be freed to take up new requests.

so what asynchronous does is when there is wait, the waiting thread will be freed and starts to serve other new requests and does not wait for this io operation to complete. when the io operation is completed and data is available then the driver/stream will send the data to either the same thread or a new thread to process and return back the response. the entire process works like events, sending to database will be an event and when database processes and sends back the result will be an event. this way u can even control the numbers threads which are alive in ur application at a given time. it makes the application more efficient and gives a complete non-blocking asynchronous process. This way you have a limited set of threads to do the processing with better efficiency

Non blocking was popularized by CompletableFuture and ReactiveX. Spring web flux gives you an annotation-based reactive components which are much easier to work with.

Event loop - when a request comes in, a thread will pick up and will trigger an even to the database, and will get freed there to process other requests. db will process the request and when data is available it will trigger another event saying that data is ready. after which the thread will pick it up and send back the response. there is no wait or blocking stages in this case. this is called event loop

Advantages
scalability, better cpu utilization, better thread management, etc
you can stream data seamlessly

Disadvantages
Spring reactive is not for all…
There are few things u need to consider and take care when u move to this framework
Entire Application will become reactive, and functional
difficult to debug or understand,
it is mostly suitable for very high volume processing applications, or applications with high concurrency and realtime applications

use cases,
1. high transaction processing system like bank application
2. large online shopping application, to send notifications and interact with other services
3. stock/trade applications to post live data through streams
Рекомендации по теме