Mono and Flux are the reactive types of Project Reactor that Spring WebFlux uses to represent asynchronous, non-blocking data. Mono<T> emits zero or one item and Flux<T> emits zero to many items. Together they let you describe a data flow once and have the runtime execute it without tying up a thread per request.
This article explains the two types, the operators you use most, and how they behave when you subscribe, fail or need to limit the flow.
Both implement the Reactive Streams Publisher interface. A publisher sends three kinds of signals to its subscriber: onNext for each item, then exactly one terminal signal, either onComplete or onError. A Mono sends at most one onNext, which makes it suitable for single results and for completion-only operations such as deleting a record.
A Flux may send many onNext signals and may never complete, for example a stream of events.
Use fromSupplier or defer when the value should be computed on each subscription. Mono.just(expensiveCall()) evaluates expensiveCall() immediately, even if nobody subscribes.
Operators return a new publisher, so you chain them.
The three operators you will use most:
| Operator | Purpose |
|---|---|
map | Transform each item synchronously. |
flatMap | Transform each item into another publisher and merge the results, used for async calls. |
concatMap | Like flatMap but preserves order by running one inner call at a time. |
The second argument limits concurrency, which protects the downstream service.
Backpressure lets the subscriber control the rate. A Flux produced from a database cursor or a message queue will not push faster than the subscriber requests.
limitRate(100) requests items in batches of about 100 from upstream. When a source cannot slow down (for example UI events or Flux.interval), choose a strategy with onBackpressureBuffer, onBackpressureDrop or onBackpressureLatest.
An error terminates the sequence, so recover with operators.
Use onErrorReturn for a fallback value, onErrorResume for a fallback publisher and retryWhen(Retry.backoff(...)) for retries with delay.
StepVerifier subscribes, checks each signal and fails the test if the sequence does not match. Avoid block() in tests unless you only need the final value.
block() inside reactive code, which Reactor rejects on non-blocking threads.map instead of doOnNext.They are containers for values that arrive later. Mono holds at most one value, and Flux holds any number.
Not in Spring WebFlux. The framework subscribes when it writes the HTTP response. In plain Java programs and in tests you subscribe or use StepVerifier.
map applies a plain function to each item. flatMap applies a function that returns a publisher and flattens the results, which you need for asynchronous calls.
A Stream is synchronous and pull-based and can be consumed once. Reactor types are asynchronous, push-based with backpressure and can be subscribed many times.
Mono and Flux are Reactor's publishers for zero-or-one and zero-to-many items. Build pipelines with operators, rely on backpressure to protect consumers, handle errors with the onError operators, and test with StepVerifier. Keep the chain non-blocking so WebFlux can serve many requests with few threads.