Q-1). How to grasp the concepts of Mono, Flux & Backpressure?
A-1). Mono – It represents a single asynchronous value or no value
Flux – It represents a stream of 0 to N asynchronous values
Backpressure – A mechanism where a data producer generates data faster than the consumer can process.
Concept | Purpose | Emits | Use Case |
Mono | Asynchronous single value | 0 or 1 | Fetch a single record, send a single response |
Flux | Asynchronous stream of data | 0 to N | Batch Processing, Streaming data |
Backpressure | Control data flow | Consumer controls | Prevent overwhelming the consumer |
Q-2). What is the confusion regarding imperative and reactive programming styles?
A-2). The transition from imperative to reactive programming styles can be challenging for developers accustomed to traditional, blocking paradigms.
Concepts | Imperative Style | Reactive Style |
Flow Control | Operations occur sequentially | Declarative operations like map, filter, flatMap to process data streams |
Data Processing | Control the flow explicitly with loops, conditions, and return statements. | The framework handles the flow and timing of data emissions |
Blocking v/s Non Blocking Behavior | Operations blocks the current thread until it completes, execution is sequential | Operations are non-blocking, instead of waiting, you define a pipeline for when the data is available. |
Error Handling | Errors are handled using try-catch blocks in a straightforward manner | Errors propagate through the pipeline and are handled using operators like onErrorResume or onErrorMap |
Thread Management | Single-threaded by default unless explicitly creating new threads or using executors | Schedulers control the threading model (e.g. – parallel(), boundedElastic()) |
State and Mutability | State changes are managed explicitly with mutable variables | Avoids shared mutable state, uses transformations and immutability instead |
Debugging & Stack Traces | Errors include a clear stack trace pointing to the exact line where the issue occurred. | Errors in a reactive flow might not show a traditional stack trace because operations are asynchronous. |
Combining multiple async sources | Combine results using blocking calls or threads | Use operators like zip, merge, flatMap to combine asynchronous sources. |
Thinking in Streams | Works with values and collections directly | Focuses on stream of data, even for single items. Everything is treated as a sequence. |