Introduction
In this blog, we’ll explore some fundamental notions of reactive programming in an effort to better comprehend what it is.
What is Reactive Programming?
Reactive is a style of programming that encourages an event-driven, asynchronous approach to data processing. “Asynchronous data streams and change propagation are dealt with in reactive programming.“
Now let’s take a moment and understand what the above statement actually means ..
Asynchronous
Imagine a system that stops working because the main thread calls the database and is currently stopped while the user waits. Next, what? Your client completes their task by moving to your rivals. A database call made using the reactive technique immediately returns without blocking the thread that initiated it. It has an asynchronous stream of events for a structure. Simply put, after starting some background work to complete the prior task, you immediately move on to the next one. That is asynchronous behavior.
Data Streams
Data Streams are the “Backbone” of Reactive Programming. Streams are wrappers that allow us to act on a data source while also making mass processing simple and quick. We do not store data in a stream, and Additionally, it never changes the underlying data source. We may compare a data stream to a pipeline in which the data is subjected to various operations as it flows.
Propagation of change
Rethink the preceding illustration. What is the pipeline’s other end attached to? You are the consumer. The propagation of streaming data to a consumer is similar. The term “change” in this context refers to how the data in the stream was altered. Observers who subscribed for this modification ingest the altered or converted data.
Interfaces used in reactive approach
There are 4 interfaces that we use in the Reactive Approach :
Publisher
The publisher is the component where we generate data. It is a generic interface that provides a single implementation of the subscriber method.
Subscriber
A Subscriber subscribes to the “Publisher” and consumes the data that the publisher generates. The producer can quickly outpace the subscriber if it is unreliable. Subscribers or Publishers can use backpressure, a potent method for carefully regulating the publishers, to address this issue.
Subscription
The Publisher after generating the data pushes everything to the Subscription.
Processors
These take information from the publisher, process it, and then give it to the subscriber. As a result, they can be considered to be a hybrid of the publisher and subscriber.
Why do we need Reactive Programming?
With the exponential rise in data processing and the rising number of concurrent users, there are new standards that software must meet to compete. We can sum up these needs by the four Reactive Manifesto principles, which declare that Reactive Systems are:
Message Driven
As application complexity has grown, developers have shifted away from the conventional monolithic method and toward microservices design. These loosely linked services communicate and share data in the form of messages. To delegate both data and failures, reactive systems use asynchronous message transmission between components.
Responsive
CRUD applications with blocked IO operations not only waste system resources but also cause greater latencies and poor performance. As a result, your money and your customers’ time are wasted – the last thing any business wants! The user anticipates outcomes in the blink of an eye. Reactive systems focus on providing rapid and consistent reaction times to meet this demand.
Elastic
The ability of a system to scale under varied workloads is crucial. Elasticity refers to the capacity to scale up or scale down resources assigned to service dependent on load in order to remain responsive.
Resilient
Failure is an unavoidable occurrence. It is inelegant to have the entire application disrupted because one module is down. Even in the face of failure, the system must stay responsive.
When to use Reactive Programming?
In a multi-user scenario or when working with large amounts of data, reactive programming is usually the most efficient option. That’s where asynchronous processing comes in handy to make the application more responsive. There is no use in sticking to this paradigm when it can be smoothly implemented as a component that we can make ‘reactive’ rather than changing the coding approach of the entire program. It is not necessary to add complexity when we can get the same results with a simpler strategy.
Reactive in Java
- RxJava : a library for creating event-based and asynchronous programming utilising observable sequences.
- Project Reactor : reactive library for creating non-blocking apps on the JVM based on the Reactive Streams standard.
- Reactive Streams API : It is a project aimed at developing a standard for asynchronous stream processing with non-blocking backpressure.
Conclusion
To summarise, reactive programming is a programming approach that uses asynchronous data streams. It makes it easier to work in distributed asynchronous non-blocking methods.
For more blogs stay tuned to: http://blog.knoldus.com