What is Spring WebFlux and when to use it


Spring WebFlux is the web framework for reactive-stack. Spring
WebFlux is added in Spring 5.0. It is fully non-blocking and supports reactive
stream back pressure. It runs on the non-blocking I/O client-server framework
such as Netty, Undertow, and Servlet 3.1+ containers. In an Spring application,
both modules (spring-webmvc and spring-webflux) can exist side-by-side.
The term "reactive" in programming model refers to
react to change, for example, network components reacting to I/O events, UI
controllers reacting to mouse events. Here non-blocking is reactive because,
instead of being blocked, we are now in the mode of reacting to notifications
as operations complete or data becomes available.
Spring WebFlux is needed for a non-blocking web stack to
handle concurrency with a small number of threads and scale with fewer hardware
resources. Functional programming is good to write concurrent programs. Due to
immutability as a default construct of functional paradigm, concurrent
programming guarantees deterministic execution. After the introduction of
lambda expression, it creates way to write functional programming. Hence to
manage reactive programming for web application, Spring introduces WebFlux
module.
https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux