“This is the 27th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

What is Spring WebFlux

WebFlux, a new module added to Spring5, is a Web development framework that uses == responsive programming == and == asynchronous non-blocking ==, and features similar to Spring MVC.

Based on Reactor, WebFlux is asynchronous and event-driven, which can improve system throughput and scalability with limited hardware resources.

Spring WebFlux and Spring MVC have their own characteristics

Spring MVC, built on the Servlet API and Servlet container, is a synchronous blocking I/O framework, where a thread processes a request.

Based on the Reactive Streams API and Servlet 3.1+ container (new in 3.1 specification: asynchronous processing support), Spring WebFlux is an asynchronous non-blocking framework that takes full advantage of the hardware resources of multi-core cpus to handle a large number of concurrent requests. However, WebFlux does not shorten the request response time of an interface, but only improves throughput and scalability.

Spring WebFlux can implement routing requests using Java8 functional programming.

Asynchronous, synchronous: the caller sends a request and waits for the caller to respond before performing other tasks. If the caller does not wait for the caller to respond, the caller directly performs other tasks after sending the request.

Blocking, non-blocking: the caller receives a request and waits until the request is completed before reporting back to the caller.

Differences between Spring WebFlux and Spring MVC

Similarities:

  • Can be annotated,
  • Both can run in the Tomcat container.

Difference:

  • Spring MVC adopts synchronous blocking and Spring WebFlux adopts asynchronous non-blocking. Spring MVC is more convenient for developers to write functional codes and Debug tests, etc. If Spring MVC can meet the scenarios, WebFlux should not be used as far as possible
  • WebFlux operates databases using R2DBC or JASynC-SQL;
  • By default, WebFlux uses Netty as the server.

Choose Spring WebFlux or Spring MVC in development

First: WebFlux is not an alternative to Spring MVC.

According to the official recommendations, the following points can be taken as reference:

  • If you are already developing with Spring-WebMVC and the project is running well, there is no need to change; Moreover, most of the three-party libraries are now blocked and do not take advantage of non-blocking.

  • Webflux offers quite a few choices; At the service layer, you can use (Netty, Tomcat, Jetty, Undertow, and Servlet containers above version 3.1) as Web services; At the application level, you can choose to use @Controller or function programming definitions; In terms of programming style, you can choose to use Reactor, RxJava, or others.

  • If you prefer the lightweight, functional programming style of lambda expressions provided by Java8, webflux is recommended. Meanwhile, for some lightweight applications or micro-services with low complexity, it is recommended to use WebFlux for better control.

  • In microservices architecture, you can mix webMVC and WebFlux projects. Both frameworks can use @Controller annotations to make project reuse easier.

  • The easiest way to evaluate whether a project should choose WebFlux is based on whether the project uses a lot of blocking apis, such as JDBC or some blocking apis that are not suitable for a WebFlux project.

  • The learning path for responsive programming is steep, so if you’re working on a large team, consider the cost of investment, the cost of learning for the team members, etc.