@ -23,7 +23,9 @@ e.g. Spring MVC controllers with the reactive `WebClient`.
@@ -23,7 +23,9 @@ e.g. Spring MVC controllers with the reactive `WebClient`.
[[webflux-new-framework]]
=== Why a new web framework?
=== Motivation
Why was Spring WebFlux created?
Part of the answer is the need for a non-blocking web stack to handle concurrency with a
small number of threads and scale with less hardware resources. Servlet 3.1 did provide
@ -44,9 +46,9 @@ WebFlux to offer functional web endpoints alongside with annotated controllers.
@@ -44,9 +46,9 @@ WebFlux to offer functional web endpoints alongside with annotated controllers.
[[webflux-why-reactive]]
=== Reactive: what and why?
=== Define "reactive"
We touched on non-blocking and functional but why reactive and what do we mean?
We touched on non-blocking and functional but what does reactive mean?
The term "reactive" refers to programming models that are built around reacting to change --
network component reacting to I/O events, UI controller reacting to mouse events, etc.
@ -131,82 +133,86 @@ annotations and being called back.
@@ -131,82 +133,86 @@ annotations and being called back.
[[webflux-framework-choice]]
=== Choosing a web framework
=== Applicability
Spring MVC or WebFlux?
A natural question to ask but one that sets up an unsound dichotomy. It's actually both
working together to expand the range of available options. The two are designed for
continuity and consistency with each other, they are available side by side, and feedback
from each side benefits both sides. The diagram below shows how the two relate, what they
have in common, and what each supports uniquely:
image::images/spring-mvc-and-webflux-venn.png[]
Should you use Spring MVC or WebFlux? Let's cover a few different perspectives.
Below are some specific points to consider:
If you have a Spring MVC application that works fine, there is no need to change.
* If you have a Spring MVC application that works fine, there is no need to change.
Imperative programming is the easiest way to write, understand, and debug code.
You have maximum choice of libraries since historically most are blocking.
If you are already shopping for a non-blocking web stack, Spring WebFlux offers the same
* 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, Servlet 3.1+ containers, a choice of programming models --
annotated controllers and functional web endpoints, and a choice of reactive libraries --
Reactor, RxJava, or other.
If you are interested in a lightweight, functional web framework for use with Java 8 lambdas
* If you are interested in a lightweight, functional web framework for use with Java 8 lambdas
or Kotlin then use the Spring WebFlux functional web endpoints. That can also be a good choice
for smaller applications or microservices with less complex requirements that can benefit
from greater transparency and control.
In a microservice architecture you can have a mix of applications with either Spring MVC
* In a microservice architecture you can have a mix of applications with either Spring MVC
or Spring WebFlux controllers, or with Spring WebFlux functional endpoints. Having support
for the same annotation-based programming model in both frameworks makes it easier to
re-use knowledge while also selecting the right tool for the right job.
A simple way to evaluate an application is to check its dependencies. If you have blocking
* A simple way to evaluate an application is to check its dependencies. If you have blocking
persistence APIs (JPA, JDBC), or networking APIs to use, then Spring MVC is the best choice
for common architectures at least. It is technically feasible with both Reactor and
RxJava to perform blocking calls on a separate thread but you wouldn't be making the
most of a non-blocking web stack.
If you have a Spring MVC application with calls to remote services, try the reactive `WebClient`.
* If you have a Spring MVC application with calls to remote services, try the reactive `WebClient`.
You can return reactive types (Reactor, RxJava, <<webflux-reactive-libraries,or other>>)
directly from Spring MVC controller methods. The greater the latency per call, or the
interdependency among calls, the more dramatic the benefits. Spring MVC controllers
can call other reactive components too.
If you have a large team, keep in mind the steep learning curve in the shift to non-blocking,
* If you have a large team, keep in mind the steep learning curve in the shift to non-blocking,
functional, and declarative programming. A practical way to start without a full switch
is to use the reactive `WebClient`. Beyond that start small and measure the benefits.
We expect that for a wide range of applications the shift is unnecessary.
If you are unsure what benefits to look for, start by learning about how non-blocking I/O
works (e.g. concurrency on single-threaded Node.js is not an oxymoron) and its effects.
The tag line is "scale with less hardware" but that effect is not guaranteed, not without
some network I/O that can be slow or unpredictable. This Netflix