Backend Development 9 min read

Introduction to Spring WebFlux and Reactive Programming

This article explains the fundamentals of Spring WebFlux, its reactive programming model with Mono and Flux, compares it to Spring MVC, and provides practical code examples for building non‑blocking backend services using Spring Boot.

Top Architect
Top Architect
Top Architect
Introduction to Spring WebFlux and Reactive Programming

Spring WebFlux Overview

Spring WebFlux is a reactive, non‑blocking web framework introduced in Spring 5.0, which can significantly increase request throughput compared to traditional servlet‑based MVC.

What is Reactive?

Reactive programming allows an API call to return immediately without blocking; the system processes data when it arrives, improving CPU utilization and overall throughput.

Spring WebFlux Reactive API

The framework is built on Project Reactor and provides two core types: Mono for single‑value streams and Flux for multi‑value streams.

// Mono for a single object
Mono
person = personDao.getPerson(personId);
// Flux for multiple objects
Flux
people = personDao.listAllPeople();

WebFlux can run on Netty, Undertow, or any servlet container 3.1+, and can coexist with Spring MVC.

Choosing Spring MVC or WebFlux

Guidelines include: keep MVC if existing project works and uses blocking libraries; use WebFlux for non‑blocking APIs, lambda‑friendly code, micro‑services, or when you need high concurrency.

If your project already works with Spring MVC, no need to switch.

WebFlux offers many deployment options (Netty, Tomcat, Jetty, Undertow) and can use either annotation‑based controllers or functional routing.

For Java 8 lambda lovers and lightweight services, WebFlux is recommended.

Both frameworks can be mixed in a micro‑service architecture.

A project with many blocking APIs (e.g., JDBC) is not suitable for WebFlux.

WebClient can be used in MVC to get reactive responses.

Consider learning cost and team size before adopting reactive programming.

Concurrency Model

Spring MVC uses a thread‑per‑request model with a fixed thread pool, leading to limited throughput. WebFlux, built on Netty’s NIO, uses a few event‑loop threads to handle many requests concurrently, improving CPU utilization.

WebFlux Code Example

Add the dependency:

org.springframework.boot
spring-boot-starter-webflux

Define a data class:

public class Person {
    private Integer id;
    private Integer age;
    private String name;
}

Configure functional routing:

@Configuration
public class PersonRouter {
    @Resource
    private PersonHandler personHandler;

    @Bean
    public RouterFunction
personRoutes() {
        return RouterFunctions.route()
            .GET("/person/{id}", RequestPredicates.accept(MediaType.APPLICATION_JSON), personHandler::getPerson)
            .GET("/person", RequestPredicates.accept(MediaType.APPLICATION_JSON), personHandler::listPeople)
            .POST("/person", personHandler::createPerson)
            .build();
    }
}

Handler implementation:

@Component
public class PersonHandler {
    @Resource
    private PersonRepository personDao;

    public Mono
listPeople(ServerRequest request) {
        Flux
people = personDao.listAllPeople();
        return ServerResponse.ok()
            .contentType(MediaType.APPLICATION_JSON)
            .body(people, Person.class);
    }

    public Mono
createPerson(ServerRequest request) {
        Mono
person = request.bodyToMono(Person.class);
        return ServerResponse.ok().build(personDao.savePerson(person));
    }

    public Mono
getPerson(ServerRequest request) {
        int personId = Integer.parseInt(request.pathVariable("id"));
        return personDao.getPerson(personId)
            .flatMap(p -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).bodyValue(p))
            .switchIfEmpty(ServerResponse.notFound().build());
    }
}

Running the application starts an embedded Netty server; accessing http://localhost:8080/person/1 demonstrates the reactive endpoint.

backendJavaReactive ProgrammingSpring BootFluxMonoSpring WebFlux
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.