Reactive Programming with Spring Boot WebFlux

Understanding Reactive Programming

Reactive programming with Spring Boot WebFlux presents a contemporary approach to handling data and events in applications. It involves the seamless management of information streams, leveraging asynchronous and non-blocking processes. This methodology significantly enhances efficiency and scalability throughout various systems.

In contrast to traditional methods, which often relied on synchronous operations, reactive programming eliminates bottlenecks and constraints. By facilitating a fluid flow of data, applications operate more smoothly, resulting in improved responsiveness and performance.

Synchronous and Blocking

When a client sends a request, it’s assigned to a specific thread (let’s call it Thread1). If processing that request takes, say, 20 minutes, it holds up Thread1. During this time, if another request comes in, it will have to wait until Thread1 finishes processing the first request before it can be served. This behavior, where one request blocks the processing of others until it’s complete, is what we refer to as synchronous and blocking.

Features of Reactive programming

Asynchronous and Non-blocking

In an asynchronous and non-blocking scenario, when a client sends a request, let’s say it’s picked up by Thread1. If processing that request takes, for example, 20 minutes, and another request comes in, it doesn’t wait for Thread1 to finish processing the first request. Instead, it’s handled separately, without blocking or waiting. Once the first request is complete, its response is returned. This approach allows clients to continue sending requests without waiting for each one to complete, thereby avoiding blocking. This style of operation, where requests are managed independently and without blocking, is commonly referred to as “non-blocking” or “asynchronous.”

spring boot webflux

Features of Reactive Programming with Spring Boot WebFlux

Asynchronous and Non-blocking: Reactive programming focuses on handling tasks concurrently without waiting for each to finish before moving on to the next, making applications more responsive.

Functional Style Coding: Reactive programming promotes a coding style that emphasizes functions or transformations of data streams, making code more expressive and modular.

Data Flow as Event-Driven: In reactive programming, data flow is driven by events, meaning that actions or processing are triggered by changes or updates in data.

Backpressure of DataStream: Reactive streams incorporate mechanisms to manage the flow of data between publishers and subscribers, allowing subscribers to control the rate at which they receive data

Reactive Stream Specifications:

Reactive streams follow specific rules, known as specifications, to ensure consistency across implementations and interoperability.

1. Publisher

The Publisher interface represents a data source in reactive streams, allowing subscribers to register and receive data.

@FunctionalInterface 
public static interface Publisher<T> { 
   public void subscribe(Subscriber<? super T> subscriber); 
} 

2. Subscriber

The Subscriber interface acts as a receiver of data from publishers, providing methods to handle incoming data, errors, and completion signals.

public static interface Subscriber<T> {
        public void onSubscribe(Subscription subscription);
        public void onNext(T item);
        public void onError(Throwable throwable);
        public void onComplete();
 }

3. Subscription

The Subscription interface enables subscribers to request data from publishers or cancel their subscription, offering methods for requesting specific items and canceling subscriptions.

public static interface Subscription {
        public void request(long n);
        public void cancel();
}

4. Processor

The Processor interface combines the functionality of publishers and subscribers, allowing for the transformation and processing of data streams.

public static interface Processor<T,R> extends Subscriber<T>, Publisher<R> {
}

Pub Sub Event Flow:

  • Subscriber subscribes by calling the Publisher’s subscribe(Subscriber s) method.
  • After Subscription, the Publisher calls the Subscriber’s onSubscribe(Subscription s) method.
  • The Subscriber now possesses a Subscription object. Utilizing this object, it requests ‘n’ (number of data) from the Publisher.
  • Subsequently, the Publisher invokes the onNext(data) method ‘n’ times, providing the requested data.
  • Upon successful completion, the Publisher calls the onComplete() method.
Reactive Programming with Spring Boot WebFlux

Next Topic: Read- Spring Webflux Mono Example 

Related Articles:

Leave a Comment