In Project Reactor, Mono<T> represents an asynchronous sequence of zero or one element, and Flux<T> represents a sequence of zero to N elements. Both are lazy Publisher implementations of the Reactive Streams specification, and they are the foundation of Spring WebFlux. Use Mono for single results (a lookup by ID, an HTTP response) and Flux for streams and collections.
Nothing happens until something subscribes. Building a pipeline only describes the work, and subscribing starts it.
| Type | Emits | Typical use |
|---|---|---|
Mono<T> | 0 or 1 item, then complete or error | Find by ID, save, HTTP call, completion signal |
Flux<T> | 0 to N items, then complete or error | Query results, event streams, server-sent events |
Both support backpressure: a subscriber tells the publisher how many items it can handle, so a fast producer cannot overwhelm a slow consumer. Using a Mono instead of Flux<T> for single values also documents intent and allows some optimisations.
To add Reactor to a project:
With Spring Boot, spring-boot-starter-webflux brings it in.
subscribe takes three callbacks: one for each item, one for an error and one for completion. In an application you rarely subscribe yourself, because the framework (WebFlux) subscribes at the edge.
map transforms each element synchronously.flatMap maps each element to a publisher and merges the results, which is how you chain asynchronous calls.filter, take, skip and distinct shape the stream.zip combines several publishers into tuples; merge and concat join streams.collectList() turns a Flux<T> into Mono<List<T>>.reduce folds a stream into a single Mono.switchIfEmpty and defaultIfEmpty handle missing values.onErrorResume, onErrorReturn and retryWhen handle failures.publishOn and subscribeOn control which scheduler runs the work.A combined example:
The controller returns publishers and the framework writes results as they arrive. The /stream endpoint sends server-sent events, one item per second.
block() inside a reactive pipeline. It ties up a thread and can throw on non-blocking threads.subscribeOn(Schedulers.boundedElastic()) or use a reactive driver like R2DBC.map where you need flatMap, which produces a Mono<Mono<T>>.Reactive code pays off for I/O-heavy services with many concurrent connections and for streaming. It is harder to read and debug than imperative code. Since Java 21, virtual threads let ordinary blocking code scale to many concurrent tasks, so for many Spring MVC services the simpler blocking model with virtual threads is enough. Choose WebFlux when you need streaming, backpressure or a fully reactive stack.
Mono holds at most one element, Flux holds any number. Convert with flux.next() or mono.flux() when needed.
It is lazy. Nothing runs until a subscriber subscribes, which WebFlux does for you when you return it from a controller.
Use map for a plain synchronous function. Use flatMap when the function returns a Mono or Flux.
No. A CompletableFuture is a single, eager value. Mono is lazy, can be re-subscribed, supports backpressure and a rich operator set, and Flux covers streams.
Mono models zero or one asynchronous value and Flux models zero to many. Both are lazy, support backpressure and are composed with operators like map, flatMap and zip. Avoid blocking calls inside pipelines and decide between reactive and virtual threads based on your workload.