Monday, November 5, 2018

Spring Web Reactive Framework - Spring WebFlux Tutorial

This Spring WebFlux tutorial gives an overview of the new reactive web framework- Spring WebFlux added in Spring version 5.0. It is fully non-blocking, helps you to write event driven, asynchronous logic and supports Reactive Streams back pressure.

Reactive in Spring web reactive

Reactive refers to the even driven programming models that are built around reacting to change. As example, network component reacting to I/O events, UI controller reacting to mouse events etc. In synchronous mode any method call is blocking but the non-blocking is reactive because the operations are not blocking but react to the notifications as operations complete or data becomes available.

Another important feature associated with reactive programming is non-blocking back pressure. In an asynchronous system resource consumption needs to be controlled so that a fast producer does not overwhelm the stream destination.

Reactive stream

Reactive Streams ( is an initiative to provide a standard for asynchronous stream processing with non-blocking back pressure. Reactive stream API is the foundation for reactive libraries.

Reactive stream API

The API consists of the following components that are required to be provided by Reactive Stream implementations:

  • Publisher
  • Subscriber
  • Subscription
  • Processor


A Publisher is a provider of a potentially unbounded number of sequenced elements, publishing them according to the demand received from its Subscriber(s).

public interface Publisher<T> {
    public void subscribe(Subscriber<? super T> s);


Will receive call to onSubscribe(Subscription) once after passing an instance of Subscriber to Publisher.

public interface Subscriber<T> {
    public void onSubscribe(Subscription s);
    public void onNext(T t);
    public void onError(Throwable t);
    public void onComplete();


A Subscription represents a one-to-one lifecycle of a Subscriber subscribing to a Publisher. It can only be used once by a single Subscriber.

public interface Subscription {
    public void request(long n);
    public void cancel();


A Processor represents a processing stage—which is both a Subscriber and a Publisher and obeys the contracts of both.

public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {

Reactor library for Spring WebFlux

Reactor is the reactive library which is an implementation of Reactive Streams API. Spring WebFlux uses the Reactor as its reactive library. It provides the Mono and Flux API types.

  • Mono- Mono is an abstract class in Reactor library that implements Publisher. It is used to work on data sequences of 0..1.
  • Flux- Flux is an abstract class in Reactor library that implements Publisher. It is used to work on data sequences of 0..N.

WebFlux requires Reactor as a core dependency but it is interoperable with other reactive libraries via Reactive Streams. As a general rule WebFlux APIs accept a plain Publisher as input, adapt it to Reactor types internally, use those, and then return either Flux or Mono as output.

Spring WebFlux Programming models

Spring WebFlux provides a choice of two programming models-

  • Annotated Controllers — The annotations used in Spring web reactive spring-webflux module are based on the same annotations from the spring-web module.

    Refer Spring Web Reactive - Spring WebFlux Example Using Annotation-Based Programming to see an example of Spring WebFlux application using annotations.

  • Functional Endpoints — Lambda-based, lightweight, functional programming model that an application can use to route and handle requests. The big difference with annotated controllers is that the application is in charge of request handling from start to finish vs declaring intent through annotations and being called back.

    Refer Spring Web Reactive - Spring WebFlux Example Using Functional Programming to see an example of Spring WebFlux application using functional programming.

As per Spring docs the following diagram shows how the two web modules Spring MVC and WebFlux relate, what they have in common, and what each supports uniquely:

Spring WebFlux tutorial

Servers available for Spring WebFlux applications

Spring WebFlux is supported on Netty, Undertow, Tomcat, Jetty, and Servlet 3.1+ containers. Each server is adapted to a common Reactive Streams API. Spring Boot has a WebFlux starter that uses Netty by default, but it is easy to switch to Tomcat, Jetty, or Undertow by changing your Maven or Gradle dependencies.

Performance with Spring web reactive

A spring web reactive non-blocking application may not run faster than its blocking counter part. In fact it requires more work to do things the non-blocking way and that can slightly increase the required processing time.

The key expected benefit of reactive and non-blocking is the ability to scale with a small, fixed number of threads and less memory as the resources are not blocked. That makes applications more resilient under load because they scale in a more predictable way.


That's all for this topic Spring Web Reactive Framework - Spring WebFlux Tutorial. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page

Related Topics

  1. Spring Integration With Quartz Scheduler
  2. Spring MVC File Download Example
  3. Spring MVC Radiobutton And Radiobuttons Form Tag Example
  4. Insert\Update Using NamedParameterJDBCTemplate in Spring Framework
  5. Autodiscovery of Bean Using componenent-scan in Spring

You may also like-

  1. Transaction Management in Spring
  2. Injecting Inner Bean in Spring
  3. Spring Asynchronous Method Execution Support Using @Async Annotation
  4. Using Spring Profiles to Switch Environment
  5. Optional Class in Java 8
  6. Primitive Type Streams in Java Stream API
  7. Difference Between Encapsulation And Abstraction in Java
  8. How to Convert Date to String in Java