Difference Between Mono and Flux in Spring WebFlux
Last Updated :
24 May, 2024
Spring WebFlux is a part of the Spring Framework that provides reactive programming support for web applications. It introduces reactive types like Mono and Flux publishers which are fundamental to Its programming model. Mono and Flux play a crucial role in reactive programming.
In this article, we will explore Mono vs Flux.
Mono
- Creation: A Mono can be created by using static methods like Mono.just() or Mono.empty() or Mono.error()
- Subscription: The subscriber subscribes to the Mono triggering the execution of the pipeline.
- Emission: The Mono publisher emits zero or one item followed by an onComplete or onError signals
- Operators: Various operators can transform the emitted item, handle errors, or perform side-effects
Advantages:
- Simplicity: Ideal for operations that return a single result or none.
- Performance: Efficient handling of single-value streams with minimal overhead.
- Error Handling: Comprehensive error handling capabilities with operators like onErrorReturn and onErrorResume.
Flux
- Creation: A Flux can be created by using static methods like Flux.just() or Flux.fromIterable() or Flux.range()
- Subscription: The subscriber subscribes to the Flux triggering the execution of the pipeline.
- Emission: The Mono publisher emits zero to N items followed by an onComplete or onError signals
- Operators: Various operators can transform the emitted items, handle errors, buffer, window, or combine multiple Flux streams
Advantages:
- Flexibility: Capable of handling multiple items, making it suitable for collections, streams, and real-time data.
- Rich Operators: Extensive set of operators to manipulate and transform data streams, including buffer, window, merge, zip, etc.
- Scalability: Efficiently manages large data streams with built-in Backpressure support.
Examples of Mono and Flux
Here we provide two different examples for Mono and Flux publishers. The Mono publisher can emits zero or one and The Flux publisher can emits zero to N events.
Example Program for Mono:
In this example, we created a Mono type publisher and It returns a String type mono object and The Mono publisher can emits only one event.
Java
package com.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import reactor.core.publisher.Mono;
@SpringBootApplication
public class PublishersApplication {
public static void main(String[] args) {
SpringApplication.run(PublishersApplication.class, args);
Mono<String> greeting = Mono.just("Welcome to GeeksForGeeks");
greeting.subscribe(
System.out::println
);
}
}
Output:

Example Program for Flux:
In this example, we created a Flux type publisher and It returns a String type flux object and The Flux publisher can emits only zero to N events.
Java
package com.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import reactor.core.publisher.Flux;
@SpringBootApplication
public class PublishersApplication {
public static void main(String[] args) {
SpringApplication.run(PublishersApplication.class, args);
Flux<String> names = Flux.just("John", "Marry","Micky", "Hani");
names.subscribe(
System.out::println
);
}
}
Output:

Difference Between Mono and Flux
Below is the difference table of Mono and Flux.
Aspect
| Mono
| Flux
|
---|
Definition
| Represents 0 or 1 item.
| Represents 0 to N items.
|
Use Cases
| Single result. Ex: a database lookup for one record.
| Multiple results. Ex: database query returning a list.
|
Common Operations
| map, flatMap, filter, then
| map, flatMap, filter, take, skip, merge, zip
|
Return Types
| Mono<T>
| Flux<T>
|
Example 1 (Creating)
| Mono.just("Hello, Spring WebFlux!")
| Flux.just("Item 1", "Item 2", "Item 3")
|
Example 2 (Handling HTTP Requests)
| Mono<ServerResponse> handling single item response
| Mono<ServerResponse> handling multiple item response
|
Example 3 (REST Controller)
| @GetMapping("/single") public Mono<String> getSingleItem() { return Mono.just("Single Item"); }
| @GetMapping("/multiple") public Flux<String> getMultipleItems() { return Flux.just("Item 1", "Item 2", "Item 3"); }
|
Publisher Type
| Single-emission publisher
| Multi-emission publisher
|
Life cycle Events
| onNext, onError, onComplete
| onNext, onError, onComplete
|
Reactive Stream Support
| Backpressure handling
| Backpressure handling
|
Error Handling
| onErrorReturn, onErrorResume, onErrorMap
| onErrorReturn, onErrorResume, onErrorMap
|
Cold Publisher
| Emits items only when subscribed
| Emits items only when subscribed
|
Similar Reads
Difference Between Spring MVC and Spring WebFlux Spring MVCSpring MVC Framework takes on the Model-View-Controller design pattern, which moves around the Dispatcher Servlet, also called the Front Controller. With the help of annotations like @Controller and @RequestMapping, the by-default handler becomes a robust(strong) tool with a diverse set of
5 min read
Cancel an Ongoing Flux in Spring WebFlux Spring WebFlux is a reactive, non-blocking web framework for creating contemporary, scalable Java online applications. It is a component of the Spring Framework and implements Java reactive programming using the Reactor library. Example of Cancel an Ongoing Flux in Spring WebFluxBelow is the Example
5 min read
Difference Between Struts and Spring MVC When developing Java-based web applications, choosing between Struts and Spring MVC is crucial for ensuring scalability and maintainability. Both frameworks follow the Model-View-Controller (MVC) architecture, but they differ in their approach and flexibility. Struts, an older and once-dominant fram
5 min read
Difference between Flux and MVC In this article, we will see the difference between Flux vs MVC with the working code examples. Table of Content FluxMVCDifference between Flux and MVC1. Flux: Flux was created by facebook and was initially used by Facebook for building client-side web applications. The Flux application has 3 major
5 min read
What are the differences between Redux and Flux in ReactJS ? During the phase of applications or software development, we gather the requirements of customers to create a solution to solve the problem of customers or businesses. To solve problems we rely on different technologies and architecture patterns. for a long time, developers were using MVC (Model-Vie
10 min read
Difference Between Spring boot and Quarkus Spring Boot and Quarkus are two popular frameworks in the Java ecosystem that simplify application development. While they share some similarities, they fulfill to different needs and preferences in the software development landscape. This article explores the key differences between Spring Boot and
4 min read