java

Introduction to Spring WebFlux

Spring WebFlux is a parallel version of Spring MVC and supports fully non-blocking reactive streams. It supports the back pressure concept and uses Netty as the inbuilt server to run reactive applications.

If you are familiar with the Spring MVC programming style, you can easily work on webFlux.

Spring WebFlux uses Project reactor. Project Reactor is a fully non-blocking foundation with back-pressure support included. It’s the foundation of the reactive stack in the Spring ecosystem and is featured in projects such as Spring WebFlux, Spring Data, and Spring Cloud Gateway.

Why Spring WebFlux Created?

Part of the answer is the need for a non-blocking web stack to handle concurrency  with minimal hardware infrastructure. Other part is, it

supports Functional Programming. Lambda expressions which are part of Java8 supports functional API’S in java.

Spring WebFlux moves off from the thread-per-request interference model in ancient SpringMVC(tomcat by default) and moves towards

a multi-EventLoop, async, non-blocking(Netty by default) paradigm with back pressure thats additional scalable than ancient interference code.

Spring WebFlux Framework:

Spring WebFlux is built on Project Reactor which implements the Reactive Streams specification. WebFlux stack consists of:

  • Spring Boot
  • Spring Webflux
  • Reactive Programming
  • Java
  • Spring Framework
spring webflux
Spring Webflux

Spring Boot exposes a Spring Boot starter for Spring WebFlux reactive web apps: spring-boot-starter-webflux. Below is webflux maven dependency.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

Examples of Spring Data Reactive Libraries:

  • Spring Data Reactive for Apache Cassandra
  • Spring Data Reactive MongoDB
  • Spring Data Reactive Redis
  • Spring Data Elasticsearch

Points to remember:

  • If you are interested in a lightweight, functional web framework for use with Java 8 lambdas or Kotlin, you can use the Spring WebFlux functional web endpoints.
  • In a microservice architecture, you can have a mix of applications with either Spring MVC or WebFlux controllers or with Spring WebFlux functional endpoints.
  • If you have a Spring MVC application with calls to remote services, try the reactive WebClient. You can return reactive types (Reactor, RxJava, or other) directly from Spring MVC controller methods.
  • If you are already shopping for a non-blocking web stack, Spring WebFlux offers the same execution model benefits as others in this space and also provides a choice of servers (Netty, Tomcat, Jetty, Undertow, and Servlet containers).
  • For Undertow, Spring WebFlux uses Undertow APIs directly without the Servlet API.

Conclusion:

Spring WebFlux or Reactive non-blocking applications usually do not make the applications run faster. The essential benefit it serves is the ability to scale an application with a small, fixed number of threads and lesser memory requirements while at the same time making the best use of the available processing power.

Loading

Translate »