Reactive programming is a programming paradigm that promotes an asynchronous, non-blocking, event-driven approach to data processing. Reactive programming involves modeling data and events as observable data streams and implementing data processing routines to react to the changes in those streams.

The cost of scaling a modern application development project can vary significantly depending on various factors. Scaling an application involves increasing its capacity to handle a growing number of users, requests, and data while maintaining performance and responsiveness.

Database Costs: As the application’s data grows, the cost of the database can increase. Cloud-based databases often charge based on storage and data transfer, so you need to account for the growing data size and the number of read/write operations.

What’s the Problem here?
Person person = personService.getPerson(id);
PersonAddress adrs= PersonAddressService.getPersonAddress (id);
Here we are taking person & PersonAddress object from id through database query.
This two lines are not depends on each other, But code execute line by line so thread is idol till person object is received.
Here unnecessarily sequential pattern is followed. So it costs performance and wasted hardware and time increase to get response to the user which is not good for any application.
So here reactive programming come into picture
  • Example with reactive programming :

    What’s Different?

  • Here declarative code to build asynchronous processing pipelines
  • Different way of thinking about code flow and data which is integrated with Java Flow interface
  • Simple and reusable functions
Reactive Streams API 
The new Reactive Streams API was created by engineers from Netflix, Pivotal, Lightbend, RedHat, Twitter, and Oracle, among others and are now part of Java 9. It defines four interfaces.
Publisher :

Emits a sequence of events to subscribers according to the demand received from its subscribers. A publisher can serve multiple subscribers.

Subscriber:

Receives and processes events emitted by a Publisher. Please note that no notifications will be received until Subscription#request(long) is called to signal the demand. It has four methods to handle various kinds of responses received.
Subscription:
Defines a one-to-one relationship between a Publisher and a Subscriber. It can only be used once by a single Subscriber. It is used to both signal desire for data and cancels demand (and allow resource cleanup).
Processor:
Represents a processing stage consisting of both a Subscriber and a Publisher and obeys both contracts.
Processor.java
public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {
}Spring 5 includes Spring WebFlux, which provides reactive programming support for web operations.
What is Spring WebFlux?
Spring WebFlux is a parallel version of Spring MVC and supports fully non-blocking reactive streams. It supports the back pressure concept and uses Netty as the inbuilt server to run reactive applications. If you are familiar with the Spring MVC programming style, you can easily work on webflux also.
Spring webflux uses project reactor as the reactive library. Reactor is a Reactive Streams library; therefore, all of its operators support non-blocking back pressure. It is developed in close collaboration with Spring.
Spring WebFlux heavily uses two publishers :
  1.  Mono: Returns 0 or 1 element.
  2. Flux: Returns 0…N elements. A Flux can be endless, meaning that it can keep emitting elements forever. Also it can return a sequence of elements and then send a completion notification when it has returned all of its elements.
To subscribe call systemflux.subscribe():
In Spring WebFlux, we call reactive APIs/functions that return Monos and Fluxes, and your controllers will return monos and fluxes. When you invoke an API that returns a mono or a flux, it will return immediately. The function call results will be delivered to you through the mono or flux when they become available.
What is Reactive Programming?
The term, “reactive,” refers to programming models that are built around reacting to changes. It is built around publisher-subscriber pattern (observer pattern). In the reactive style of programming, we make a request for resources and start performing other things. When the data is available, we get the notification along with data information of the call back function. The callback function handles the response as per application/user needs.
One important thing to remember is back pressure. In non-blocking code, it becomes important to control the rate of events so that a fast producer does not overwhelm its destination.
Reactive web programming is great for applications that have streaming data, and clients that consume it and stream it to their users.
In reactive programming with Java, the choice of server and database can significantly impact the performance and scalability of your application. To take full advantage of reactive principles, you should consider using servers and databases that support non-blocking and asynchronous operations. Here are some options:
Reactive Servers:
  1.  Vert.x: Vert.x is a highly performant and reactive server that runs on the JVM. It excels at handling asynchronous I/O operations and event-driven programming, making it a strong choice for reactive applications.
  2. Spring WebFlux: Spring WebFlux is a part of the Spring Framework and provides reactive programming support for building web applications. It offers an embedded Netty server that can handle non-blocking I/O, making it well-suited for reactive applications.
  3. Akka HTTP: It enables you to build reactive servers for handling concurrent requests efficiently

Reactive Databases:

  1. MongoDB: MongoDB is a popular NoSQL database that can work well with reactive applications. It natively supports asynchronous operations and can be used with reactive drivers or libraries to take full advantage of non-blocking I/O.
  2.  Cassandra: Apache Cassandra is a distributed NoSQL database known for its scalability and fault-tolerance. When used with reactive libraries, it can efficiently handle asynchronous data operations.
  3.  R2DBC: IR2DBC (Reactive Relational Database Connectivity) is an emerging standard that provides reactive drivers for SQL-based databases like PostgreSQL, MySQL, and Microsoft SQL Server.

Click here to know more ..ReactiveProgramming.

Leave a Reply

Your email address will not be published. Required fields are marked *