Reactive Programming with Reactor Library

Suyash Upadhyay
4 min readJan 30, 2022

Let us start with a famous saying of Brene Brown at a Ted Talk event which is “Vulnerability is the birthplace of innovation, creativity, and change.”

We, humans, are lazy creatures and we do not want to solve a problem until it starts impacting us. So what was the problem, which started impacting the software and Reactive Programming came into existence?

Take a trivial example of a person who recently moved to a new city and likes to start his day with a cup of coffee and a newspaper. He prepared a cup of coffee and went to a shop to purchase a newspaper. He was following the same routine for quite a few days. One fine day he realized that the idea of going to buy a newspaper is becoming a more time-consuming and complex experience for every single day. And it would have been a great idea if there would be a system, where he could subscribe to the monthly subscriptions of the newspaper and get it every day at his doorstep So that he can perform the other activities in the meanwhile.

This is the core idea behind Reactive Programming.

Reactive Programming is a design paradigm, which works on a Publisher-Subscriber, event-driven approach that enables developers to build non-blocking, responsive, asynchronous applications that can handle back-pressure (flow control). Reactive systems better utilize modern processors and the inclusion of back-pressure in reactive programming ensures better elasticity and resilience (exception handling) between decoupled components.

Reactive systems have certain characteristics that make them ideal for low-latency, high-throughput workloads, which brings a significant impact on user experience.

Reactor Library is an advanced tool that is based on reactive streams for building non-blocking applications on the JVM. The library can be easily integrated with the applications developed on Spring Frameworks. Java 9 also provides support for reactive streams, but the examples discussed in this series will be based on Reactor Library.

As we have seen the example of the newspaper subscription model, Let us try to put the same into the 4 key interfaces of Reactive Streams that are:

  • Publisher -A Newspaper Agency
  • Subscription -The subscription of the newspaper.
  • Subscriber -Person who reads the newspaper
  • Processor -A Person who delivers the newspaper to your doorstep. This person and this interface have both traits of publisher and subscriber because the processor is subscribing the newspaper from the publisher (newspaper agency) and publishing the same newspaper to the subscriber (person who reads the newspaper).

Let us discuss all the key interfaces in detail:

A Publisher is a provider of a potentially unbounded number of sequenced elements (data streams), publishing them according to the demand received from its Subscribers. A Publisher can serve multiple Subscribers subscribed dynamically at various points in time.

The subscribe() method is for requesting the publisher to start the streaming of data.

A Subscription represents a one-to-one lifecycle of a Subscriber subscribing to a Publisher. It can only be used once by a single Subscriber.

request() method used for sending a signal desire for data. No data will be emitted by a Publisher until demand is signaled via this method and cancel() method is to request the Publisher to stop sending data and clean up the resources.

A Subscriber consumes the data stream published by the publisher.

Subscribers subscribe to the subscription using onSubscribe() method to receive the data emitted by the Publisher. Whenever the publisher emits the data all the registered subscribers get the signal with the respective data in onNext() method. If the error is thrown from the publisher, the subscriber will receive it in onError(). After emitting all the data streams the publisher sends a successful terminal signal via onComplete() method.

A Processor represents a processing stage — which is both a Subscriber and a Publisher and obeys the contracts of both.

Conclusion

In the first episode, I tried to cover the basics of reactive programming and reactor library. Next episodes will have

  • Reactor Library: Flux & Mono
  • Back Pressure Strategy
  • Reactive Streams vs Java Streams
  • Reactive Streams vs Completable Future
  • Some Common methods inside Flux & Mono

Happy Coding!!

--

--

Suyash Upadhyay

Computer Programmer I Amateur Content Writer | Book Reader