10 questions and answers for Intro To Reactor Core | Baeldung

  1. What is the purpose of the Reactive Streams Specification? Answer: The Reactive Streams Specification provides a standard for building asynchronous, event-driven applications. It defines how data streams should be produced, subscribed to, and operated on, enabling efficient and scalable reactive programming.

  2. How does Reactor Core handle backpressure? Answer: Reactor Core allows subscribers to control the amount of data received from an upstream by applying backpressure. This can be achieved through request() methods, which enable subscribers to request a specific number of elements or use error handling mechanisms to signal when they need to slow down the stream.

  3. What is the difference between Flux and Mono in Reactor? Answer: Flux is used for producing streams of 0..n elements, while Mono is used for streams of 0..1 elements. Both are implementations of the Publisher interface from the Reactive Streams Specification. Flux is more versatile and suitable for most use cases, whereas Mono is useful when a single element is expected or when dealing with error scenarios.

  4. How do you subscribe to a stream in Reactor? Answer: Subscribing to a stream involves calling the subscribe() method on the stream and providing a Subscriber implementation or a Consumer function. The subscribe() method takes a Subscriber interface or a consumer function as an argument, which defines how the subscribed elements will be handled or processed.

  5. What is the role of the log output in understanding Reactor streams? Answer: The log output provides valuable information about the flow of data within a Reactor stream. It helps visualize the sequence of events, the number of elements, and any errors that might occur. By analyzing the logs, you can track the progression of the stream and identify potential issues or unexpected behaviors.

  6. How do you create a hot stream in Reactor? Answer: A hot stream is an infinite and always-running stream of data. In Reactor, you can create a hot stream by converting a cold stream into a connectable one using the publish() method. This allows multiple subscriptions to be added to the same stream, enabling dynamic and reactive behavior.

  7. What is the difference between a parallel and a serial scheduler in Reactor? Answer: A parallel scheduler runs code on multiple threads, allowing concurrent execution of tasks. On the other hand, a serial scheduler runs tasks one after another on a single thread. This can be useful for controlling the order of execution or when resource-intensive operations need to be performed sequentially to avoid contention.

  8. How do you handle errors in Reactor streams? Answer: Error handling in Reactor can be achieved through error-handling operators such as .handleErrors() or .doOnError(). These operators allow you to define custom behavior when an error occurs, such as logging the error, triggering a recovery strategy, or propagating the error further down the stream.

  9. What is the purpose of sample() in Reactor? Answer: The sample() operator introduces throttling by emitting values at regular intervals. It takes an interval parameter, which specifies the time duration between subsequent emissions. This operator is useful for controlling the rate at which data is pushed to subscribers, reducing overload and improving resource management.

  10. How do you compose multiple Reactor streams? Answer: Composition in Reactor involves combining or integrating multiple streams to create complex reactive behaviors. You can use operators such as zip(), concatMap(), flatMap(), or merge() to combine streams, creating a single stream that represents the results of the composition. These operators allow you to transform and process data from multiple sources simultaneously.


2025-11-19