A common approach for creating applications that manage asynchronous data streams is reactive programming. It provides an effective and succinct method for handling complex data flows with ease. Flux and Mono are two crucial reactive programming ideas that are a part of Pivotal’s Reactor toolkit.
For creating non-blocking applications on the JVM, the Project Reactor is a fourth-generation reactive library that supports Reactive Streams standards. You can learn more about Mono and Flux by checking out the Java online course.
Mono: Provides a return of 0 or 1.
- Only one value may be produced using the Mono API.
Flux: Gives back 0…N elements.
- The flux can yield an infinite number of different values.
A Mono <T> is a Reactive Streams Publisher that has been enhanced with numerous operators for the purpose of creating, transforming, and orchestrating Mono sequences.
A Mono is either valued (complete with element), empty (complete without element) or failed (error). It is a specialisation of Flux that can emit no more than 1 <T> element.
When only the completion signal—the Reactive Streams equivalent of a Runnable task finishing, is of relevance, a Mono<Void> can be utilised.
Similar to Flux, an asynchronous pipeline defined using the operators will be materialised afresh for each Subscription.
It should be noted that some APIs that alter the cardinality of the sequence will return a Flux (and vice versa, APIs that lower the cardinality to 1 in a Flux return a Mono).
A stream of data that emits zero or more items is referred to as flux. It is a publisher of a series of elements and has synchronous and asynchronous data emission capabilities. Any number of sources, including an array, an iterable, or even a database query, can be used to produce a flux.
Features of Flux
- Backpressure: Flux supports backpressure, which enables it to handle circumstances in which a subscriber is creating data more quickly than it can be consumed. Because memory leaks and other performance problems might occur when a data stream is improperly managed, backpressure is crucial for preventing them.
- Cold and Heat Publishers: Heat or cold flux are also possible. A hot publisher emits data independently to each subscriber, while a cold publisher emits an identical sequence of data to all subscribers. This distinction is significant because it has an impact on how the data stream is handled and processed.
- Error handling: Flux has a number of operators, including onErrorResume and onErrorReturn, for handling problems in the data stream. In the event that a data stream error occurs, you can create a fallback strategy using these operators.
A stream of data that emits either zero or one item is represented by the type mono. Although it is comparable to Flux, it is designed to handle single values. Like flux, monos can come from a multitude of places.
Features of Mono Reactive Programming
- Laziness: Because Mono is a lazy publisher, it doesn’t start sending out data until it has at least one subscriber. By postponing the production of resources until they are truly needed, this feature can assist optimise performance.
- Error handling: Mono has numerous operators, such as onErrorResume and onErrorReturn, for managing errors. The same principles that govern these operators in Flux apply here.
- Combining Operators: There are numerous operators available in Mono to combine different Monos into a single Mono. These operators consist of concat, flatMap, and zip.
Mono vs. Flux
The Publisher interface is implemented in both Mono and Flux. In layman’s words, we may say that we should utilise Mono whenever we’re doing a computation, contacting a database, or using an external service, and anticipating no more than one return.
Flux should be used if we expect several outcomes from a computation, database, or call to an external service.
Due to its 0 or 1 value limit, Mono is more comparable to Java’s Optional class, while Flux is more comparable to List due to its N-value limit.
Reactive programming can be done using either Mono or Flux, although they have different use cases and behaviours. As we’ve seen, Flux is used when you want to work with a potentially infinite stream of data, whereas Mono is used when you want to work with a single value.
Flux and Mono are strong and adaptable types that make it simple to work with single values and asynchronous data streams. They offer a comprehensive collection of operators for processing and manipulating data, and they are performance and resource management optimised. Flux and Mono are crucial tools for any reactive programming project, whether you’re working with events, messages, or any other kind of asynchronous data. Check out the Java full course to learn more about Reactive programming.