Spring 5 WebFlux Mono and Flux
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring 5, a significant milestone in the evolution of Spring Framework, introduced a reactive programming model called WebFlux. At the heart of this model are two abstractions: `Mono` and `Flux`. These are part of the Reactor project and provide support for asynchronous programming by representing sequences of data and events. This article delves into these constructs, exploring their capabilities, usage scenarios, and significance within the reactive WebFlux architecture.
Reactive Streams
Before diving into the specifics of `Mono` and `Flux`, it’s crucial to understand the Reactive Streams specification. Reactive Streams is a standard for asynchronous stream processing with non-blocking back pressure. It defines four core interfaces:
- Publisher: Produces items for the data stream.
- Subscriber: Receives items from the `Publisher`.
- Subscription: Represents a one-to-one lifecycle and manages back pressure through request counts.
- Processor: A combination of a `Subscriber` and a `Publisher`, useful for transforming data streams.
Mono and Flux
Spring WebFlux leverages the Reactor library which implements the Reactive Streams specification. The two primary types in Reactor are:
Mono
- Definition: Represents a single or empty asynchronous computation. It either completes with a result or fails with an error.
- Use Case: Situations where a call returns at most one item, such as a single database record or a REST API call's response with a single result.
- Definition: Represents a stream of 0 to N asynchronous items with the possibility of completing successfully or failing with an error.
- Use Case: Ideal for handling multiple results, for instance, retrieving all records from a database or streaming data from file.
- Map: Transforms the items emitted by a `Mono` or `Flux`.
- FlatMap: Transforms the items into `Mono`/``\
\Flux$ and then flattens these sequences into a single sequence. - Filter: Filters items based on a predicate.
- Zip: Combines items from multiple `Mono` or `Flux` into a single one.
- OnErrorReturn: Fallback to a default value if an error occurs.
- OnErrorResume: Provides an alternative `Mono` in case of errors.
- Immediate: Current thread execution.
- Single: A single reusable thread.
- Elastic: Threads that can grow and shrink on demand.
- Parallel: Optimized for parallel processing, suitable for CPU-bound tasks.
- Scalability: Reactive systems handle more load with fewer threads and less CPU usage.
- Resilience: Built-in mechanisms for error handling lead to systems that can recover from failures gracefully.
- Real-Time Processing: Enables responsive applications with live streaming capabilities across different domains like finance, IoT, etc.
Related reading
- Spring / RabbitMQ transaction management
- Spring Actuator + Kafka Streams - Add kafka stream status to health check endpoint
- Spring actuator's liveness and readiness are returning 404
- Spring aliasFor for Annotations with TargetPARAMETER
- Spring AMQP - Sender and Receiving Messages
- Spring AMQP + RabbitMQ 3.3.5 ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN
- Spring amqp converter issue using rabbit listener
- Spring AMQP (Rabbit) Listener goes in a loop in case of exception

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.