Explore key concepts of reactive programming with WebFlux, focusing on non-blocking operations, reactive streams, and common patterns in Spring Boot applications. Increase your understanding of WebFlux components, functional endpoints, and practical scenarios for building scalable web services.
Which reactive type is commonly used in WebFlux to represent a stream of zero or more elements, such as multiple results from a database query?
Explanation: Flux is used to represent a stream of zero or more elements, making it ideal for cases like returning a list of results. Mono, on the other hand, is limited to zero or one item. Future is not natively reactive and is not used as a core return type in WebFlux. Task is unrelated to reactive streams and may be confused with other frameworks. Flux provides a flexible, non-blocking way to handle multiple items.
If a controller endpoint must return either a single item or nothing, which reactive type should you use in WebFlux?
Explanation: Mono is designed for handling zero or one result, which makes it appropriate for endpoints that may return one item or none. Flux handles streams of multiple items and would be unnecessary here. Thread is not a reactive type nor relevant for endpoint return values. Optional is similar in concept but is not reactive and cannot be used for non-blocking signal emission.
What is the execution model of WebFlux, allowing it to handle many requests efficiently with few resources?
Explanation: WebFlux follows an event-driven, non-blocking model, allowing a small number of threads to manage many concurrent connections. Blocking I/O with threads does not scale well and limits concurrency. Synchronous polling is inefficient and not a WebFlux mechanism. Multi-process forking would be unrelated to WebFlux's efficiency in handling concurrent requests.
When building a reactive stream pipeline in WebFlux, which operator would you use to transform each element in a Flux, such as converting numbers to their squares?
Explanation: The map operator transforms each element in the stream individually, making it perfect for converting numbers to their squares or similar transformations. The filter operator selectively removes items but doesn't transform them. Block forces the pipeline to run synchronously, which is against reactive principles. Collect is used to gather elements into a collection rather than transform them.
Which benefit do functional endpoints provide in a WebFlux application compared to annotated controllers?
Explanation: Functional endpoints enable developers to define routes with functions, providing flexibility in request handling and composition. More threads are not required, and blocking threads is discouraged in reactive systems. Functional endpoints fully support reactive types; in fact, that's a key feature of WebFlux.
What is a recommended way to handle errors reactively in a Flux, such as logging or returning a fallback value if an error occurs?
Explanation: onErrorResume allows for graceful reactive error handling within the stream pipeline, such as logging or supplying fallback values. Wrapping the stream with try-catch is ineffective due to asynchronous execution. Checked exceptions are not directly managed in reactive pipelines. Using block() should be avoided because it negates non-blocking behavior and is not suitable for error management in non-blocking flows.
Which concept allows a WebFlux-based system to handle situations where a consumer cannot keep up with the publisher's data flow?
Explanation: Backpressure is a mechanism where the system signals the producer to slow down if the consumer is overwhelmed, ensuring data is processed efficiently. Caching deals with data storage, not stream flow control. Garbage collection manages memory, not data flow. Thread pooling concerns resource management but does not control publisher-consumer rates.
Why should you prefer non-blocking database drivers, such as ones using reactive APIs, when working with WebFlux?
Explanation: Using non-blocking database drivers ensures the application remains reactive throughout, avoiding thread-blocking and allowing efficient resource use. Using more memory is not a benefit and is incorrect. Thread synchronization is less necessary in properly reactive applications. Supporting only one query at a time is a limitation, not an advantage, as reactive drivers allow multiple queries concurrently.
Which content type in a WebFlux controller allows the server to stream real-time updates to the client, such as a stock price feed?
Explanation: text/event-stream is used for Server-Sent Events (SSE), enabling the server to push real-time updates to connected clients. application/json is standard for static responses. text/html delivers web pages, not continuous data. multipart/form-data is intended for file uploads rather than real-time streaming.
In WebFlux, which operator would you use to merge values from two Flux instances, emitting elements as they arrive from either source?
Explanation: The merge operator combines multiple Flux sources, emitting items as soon as they are available from any source, making it ideal for interleaved streams. Zip pairs corresponding elements by order and waits for all sources to emit. Concat processes one Flux fully before starting the next, not emitting as results are available. delayElements simply introduces a delay between emissions and is unrelated to merging.