In Spring WebFlux, Mono and Flux are the return and parameter types that make the framework non-blocking. A handler returns a Mono<T> for zero or one result or a Flux<T> for many, and WebFlux subscribes to it and writes the response as data becomes available. This lets a few event-loop threads serve many concurrent requests without waiting on I/O.
Both types come from Project Reactor, which Spring WebFlux uses as its default reactive library.
WebFlux is built on Reactive Streams from the HTTP layer (Netty by default, or Undertow and Servlet 3.1+ containers) up to your controller. Mono and Flux are the common currency at every layer: controllers, WebClient, ServerRequest and ServerResponse, reactive repositories and message listeners. Because all of them return lazy publishers, nothing blocks while waiting for a database or a remote call.
| Layer | Single result | Many results |
|---|---|---|
| Controller return type | Mono<User> | Flux<User> |
WebClient | bodyToMono(User.class) | bodyToFlux(User.class) |
| Reactive repository | findById returns Mono<User> | findAll returns Flux<User> |
| Request body | @RequestBody Mono<User> | @RequestBody Flux<User> |
all() returns a Flux that is serialised as a JSON array. byId turns an empty Mono into an error that an exception handler maps to HTTP 404. create reads the request body without blocking and saves it with flatMap, because save itself returns a Mono.
WebFlux also offers a functional style that uses the same types.
Here the handler returns a Mono<ServerResponse>, and switchIfEmpty supplies the 404 when the user does not exist.
A Flux can stay open and push items to the client as they happen. Return it with the text/event-stream media type for server-sent events:
The browser receives one event per second. With backpressure, a slow client slows the producer instead of filling server memory.
WebClient is the non-blocking replacement for RestTemplate, which is in maintenance mode.
Mono.zip runs both calls concurrently and combines the results when both finish, so the latency is that of the slower call rather than the sum.
subscribe() in a controller, and do not call block() in code that runs on a Netty event-loop thread.Thread.sleep or blocking file I/O. If you must call blocking code, wrap it in Mono.fromCallable(...) and subscribeOn(Schedulers.boundedElastic()).Mono means it is never subscribed and never runs.StepVerifier from reactor-test.Plain objects force the thread to wait until the value exists. A publisher represents a value that will arrive later, so the thread can serve other requests in the meantime.
Yes, Spring accepts plain objects and wraps them, but you then lose the non-blocking benefit if the value was produced by a blocking call.
Use Mono when there is at most one result, such as a lookup by id. Use Flux for lists and streams.
Not necessarily per request. WebFlux improves how many concurrent, I/O-bound requests a small thread pool can handle. For CPU-bound work or blocking dependencies it offers no advantage.
Mono and Flux are the types that carry asynchronous data through every layer of Spring WebFlux, from WebClient and repositories to controller return values. Return them rather than subscribing, keep the chain non-blocking, and use Mono for single results and Flux for collections and streams.