Mono and Flux are the two publisher types of Project Reactor, the reactive library that Spring WebFlux is built on. Mono<T> represents an asynchronous sequence of zero or one element, and Flux<T> represents a sequence of zero to many elements. Both are lazy and non-blocking: nothing happens until something subscribes, and results are pushed to the subscriber as they become available.
Spring WebFlux uses them as the return and parameter types of controllers, functional handlers, WebClient, and reactive repositories, so a request thread is never held while waiting for I/O.
| Aspect | Mono<T> | Flux<T> |
|---|---|---|
| Elements emitted | 0 or 1 | 0 to N, possibly unbounded |
| Typical use | Fetch one record, save one entity, HTTP call returning one body | Lists, streams, server-sent events, message feeds |
| Blocking counterpart | Optional<T> or CompletableFuture<T> | List<T> or Stream<T> |
| Completion signal | onComplete after the value or empty | onComplete after the last element |
Both signal one of three things to a subscriber: onNext for each element, onComplete when finished, or onError once when something fails. A publisher emits at most one terminal signal.
Flux.interval runs on a background scheduler, which is why the example sleeps before the JVM exits. In a Spring application the framework subscribes for you.
A reactive pipeline is a description of work. Calling map, filter, or flatMap only builds the pipeline. The common beginner bug is to call a method that returns a Mono and ignore the result, so the operation never runs.
In WebFlux controllers you return the Mono or Flux and the framework subscribes when it writes the response. Avoid calling subscribe() inside request handling code, because it detaches the work from the request and loses error propagation.
Returning Mono<User> produces a single JSON object. Returning Flux<User> with the default media type produces a JSON array, while text/event-stream streams each element as a server-sent event as soon as it is emitted.
| Operator | What it does |
|---|---|
map | Transforms each element synchronously |
flatMap | Transforms each element into another publisher and merges the results, for async calls |
filter | Keeps elements matching a predicate |
zip / Mono.zip | Combines results of several publishers into a tuple |
switchIfEmpty / defaultIfEmpty | Supplies a fallback when nothing is emitted |
onErrorResume / onErrorReturn | Recovers from an error |
collectList | Turns a Flux<T> into a Mono<List<T>> |
The two lookups run concurrently, and the result is built when both complete.
Reactive Streams has backpressure: a subscriber requests a number of elements and the publisher must not send more. Reactor handles this for you through operators such as limitRate, onBackpressureBuffer, and onBackpressureDrop.
The biggest runtime mistake is blocking inside a reactive chain. Calling block(), Thread.sleep, or a blocking JDBC call on an event-loop thread stalls many requests at once. Reactor can detect this in tests with the BlockHound agent. If you must call blocking code, wrap it and move it to a bounded elastic scheduler:
Since Java 21, virtual threads let ordinary blocking Spring MVC code scale to many concurrent requests with simple code. WebFlux with Mono and Flux remains the better fit when you need streaming, backpressure, or a fully non-blocking stack end to end, and it is natural when many calls are composed in parallel. For a typical CRUD service, either model works, so choose based on your data access libraries and team experience.
Mono emits at most one element, Flux emits any number. Use Mono for a single result such as findById, and Flux for collections or streams.
Reactive types are lazy. The pipeline runs only when it is subscribed to, which WebFlux does automatically when you return the Mono from a handler. In plain code you need to call subscribe() or block() in tests.
Not on a reactive event-loop thread, where Reactor throws an error for it. It is fine in main methods, tests, and servlet-based applications that use WebClient.
Yes. Use collectList() to get a Mono<List<T>>, next() for the first element, reduce to combine, or then() to discard the values and keep only completion.
Mono is a lazy publisher of zero or one value and Flux a lazy publisher of many. WebFlux accepts and returns them so that I/O never blocks a thread. Remember that subscription triggers execution, prefer operators like flatMap and zip for composition, never block inside a reactive chain, and pick WebFlux when streaming or end-to-end non-blocking behaviour matters.