The BackendDeeper

Kayavak / The Backend / BE·48 Spring Web — MVC and WebFlux / Deeper

Deeper · The Backend

Spring Web — MVC and WebFlux

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

Java web applications have been built on the Servlet API since 1997: a container calls your code on a thread for each request. Spring MVC (from Spring 1.0, annotation-based from 2.5 in 2007) put a front controller, the DispatcherServlet, in front of servlets to route requests to annotated methods and convert bodies to and from objects. It's still the most used Java web framework.

The reactive movement asked a different question: what if threads were never blocked waiting? The Reactive Manifesto (2013–14) and the Reactive Streams specification (2015), from Lightbend, Netflix, Pivotal and others, defined asynchronous streams with backpressure, so a fast producer can't overwhelm a slow consumer. Spring 5 (2017) added WebFlux on Project Reactor and Netty. With virtual threads (Java 21), much of WebFlux's throughput advantage for ordinary request–response services is available to plain blocking MVC code.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

MVC is a pipeline that turns an HTTP request into a method call and a return value into an HTTP response, with hooks at each step. WebFlux is the same pipeline built from non-blocking streams instead of a thread per request.

Question Answer
What is it? Spring's two web stacks: servlet-based MVC and reactive WebFlux
Problem it solves Mapping HTTP to Java code with validation, conversion, errors and filters handled consistently
Parts Filters, handler mapping, interceptors, argument resolvers, message converters, controllers, exception handlers
How it works Each request flows through the chain; converters read the body into objects and write return values as JSON or other types
Assumptions MVC: blocking is fine (one thread per request). WebFlux: nothing in the chain blocks
When they change A blocking JDBC call inside WebFlux stalls an event-loop thread and every request on it
Where it fails Reactive code's debugging and stack traces; mixing blocking libraries into reactive chains
Related ideas Backend fundamentals (ch44); event loops (Foundations, ch08); backpressure in messaging (ch51)

What if?

What if a reactive pipeline had no backpressure?

Reason it through, then open

A fast producer (a database cursor, a message stream) would push items faster than a slow consumer (a client on a weak connection) could take them, and the items would pile up in memory until the server ran out. Backpressure lets the consumer say how many items it can handle (request(n)), so the producer slows to match.

What if exception handling were left to each controller?

Reason it through, then open

Every controller would format errors differently, some would leak stack traces, and validation failures would come back as 500s in one place and 400s in another. A single @RestControllerAdvice producing Problem Details gives every error the same shape and status logic.

Where the boundary is

Aspect Spring MVC Spring WebFlux
Model Thread per request (or virtual thread) Event loop with reactive types (Mono, Flux)
Blocking calls Fine Must be avoided or moved off the loop
Streaming and backpressure Limited First-class
Choose when Most request–response services Streaming, many long-lived connections, reactive end to end

How sure are we?

Claim How it's known Where from
Reactive Streams 1.0 was released in 2015 Historical reactive-streams.org; adopted as java.util.concurrent.Flow in Java 9
Virtual threads close much of the throughput gap for blocking services Measured Published benchmarks; depends on workload and pinning
WebFlux suits streaming better than MVC Model Follows from its design; MVC can stream with effort

Still open

Whether new Spring projects should ever start on WebFlux unless they need streaming is debated now that virtual threads exist.

What would change your mind? If your service streamed large or endless data to many slow clients, WebFlux's backpressure would earn its complexity.

Recall

What does backpressure prevent?

A fast producer overwhelming a slow consumer and exhausting memory.

Why is blocking dangerous in WebFlux?

It stalls an event-loop thread that serves many requests.

What do message converters do in Spring MVC?

Turn request bodies into objects and return values into response bodies (e.g. JSON).

What did virtual threads change for the MVC vs WebFlux choice?

Blocking MVC code now scales to many concurrent requests, removing much of WebFlux's advantage for plain request–response.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.