Logo

dev-resources.site

for different kinds of informations.

Spring Webflux - Reactive Java Applications - Part 2

Published at
7/29/2020
Categories
spring
webflux
reactive
java
Author
kamilacode
Categories
4 categories in total
spring
open
webflux
open
reactive
open
java
open
Author
10 person written this
kamilacode
open
Spring Webflux - Reactive Java Applications - Part 2

In part 1 we talk about some concepts used in reactive programming, now, in part 2 we will understand the reason for Webflux was created and how its architecture works :)

What is Spring WebFlux?

Spring WebFlux can be defined as a โ€œparallelโ€ version to the already known and widely used Spring MVC, with the main difference being the support for reactive NIO streams and for supporting the concept of backpressure and with Netty server coming by default embedded in its architecture.

Since version 5.0 of the Spring Framework we have a reactive part in addition to the Servlet structure that already existed, each module of these is optional, you can use the Servlet part to reactive part or even both in your applications.

This can be best exemplified through the image below:

Spring Framework

Webflux is in the "reactive part" of the stack, where:

  • instead of servlet we work with Netty / Undertow as a server;
  • We do not use the Servlet API (which is blocking) and we use Reactive Streams;
  • we started using router functions instead of @Controller

It is important to note that we can use only one or even both architectures, using the best of both.

Spring Webflux was developed due to the need for non-blocking applications that were able to work with a small number of threads simultaneously and that could be run with a few hardware resources.

In Servlet 3.1 an NIO API was provided, but its use does not match the rest of the API and all the concepts behind Servlet, which has blocking contracts like getPart and getParameter for example and their contracts were defined in a way synchronous using Filter and Servlet as an example.

These factors were decisive for the development of a new API that would be used independently of the execution time and in a non-blocking way, which was possible with the servers that consolidated themselves in the asynchronous and non-blocking operation, for example Netty.

Another reason is that WebFlux makes it easier to understand and use functional / reactive programming concepts. With the addition of functional features from Java 8 (such as lambda expressions, streams, Optional ...). In terms of style / programming model, Java 8 allowed Spring WebFlux to have functional endpoints and annotated controllers in the applications.

How does it work?

In the MVC model, requests work as follows:

Spring MVC

The client makes a request, which is received by TomCat and controlled by the thread pool, this is passed on to the Dispatcher Servlet that dispatches that request to the corresponding endpoint in RequestMapping, which will be received at the Controller, which will handle the service and by end will return a response. This whole process occurs in a blocking way, that is, another request will only enter when the previous one is completed.

In Webflux, this would be a little different:

Spring WebFlux

The client makes the request that goes to our non-blocking server (Netty), which inside it has an event loop that manages these requests, then it passes to the reactor-netty (which makes this interface reactive with the application), it passes to the dispatcher handler, which through functionals endpoints will generate this response, and, throughout this process, new requests can be made, as it is a non-blocking architecture.

We can say that Spring WebFlux uses the best of the servlet stack along with its reactive features, as we can see in the image below the documentation of the Spring:

Spring Mvc X Spring Webflux

On the MVC side we have imperative programming, JDBC / JPA and other blocking dependencies / processes.

On the Spring Webflux side, we have funcitonal endpoints, event loop, Netty and some features that already existed in MVC but who in Webflux started to have greater support, such as Reactive Clients.

This article introduced the reason for creating WebFlux and its differences from MVC, in the next article we will talk about Project Reactor!

Thanks,

webflux Article's
24 articles in total
Favicon
Introducing Java Library for Backend Microservice Webflux (Reactor-core)
Favicon
Making reactive applications with a Kitten Care Example
Favicon
Reactive Programming applied to Legacy Services โ€” A WebFlux example
Favicon
Getting Started with Spring WebFlux
Favicon
Implementing Soft Delete in Spring WebFlux with R2DBC
Favicon
Java library for developing backend with reactive programming
Favicon
How to Run an Asynchronous Task in Spring WebFlux Without Blocking the Main Response?
Favicon
How to Run a Method Asynchronously in a Reactive Chain in Spring WebFlux?
Favicon
Spring WebFlux Retry Mechanism
Favicon
Implementing Context Propagation in Reactive Programming Projects ๐Ÿ”„
Favicon
Ability to unlearn in Software: Reactive Programming
Favicon
Create DTO using get results from repository returns duplicate values in Spring Boot Reactive WebFlux
Favicon
Spring R2DBC, MariaDB, and JSON columns
Favicon
SpringBoot WebFlux Annotation-based RestAPIs
Favicon
SpringBoot WebFlux Functional RestAPIs
Favicon
Spring Webflux testing with Mockito
Favicon
A Short Example of Real-Time Event Streaming Using Spring WebFlux
Favicon
Global Error Handling In Webflux (Reactive World)
Favicon
Spring Webflux - Reactive Java Applications - Part 2
Favicon
Building an URL Shortening API with Spring WebFlux (and a lot of supporting cast)
Favicon
Spring Webflux - Reactive Java Applications - Part 1
Favicon
Spring Webflux - Aplicaรงรตes reativas em Java - Parte 1
Favicon
KVision v2.0.0 - with Bootstrap 4, Spring Webflux and lots of other improvements
Favicon
Sending Multipart Form Data Using Spring WebTestClient

Featured ones: