Reactive Programming: Spring WebFlux

turned on laptop computer
Reading Time: 3 minutes

Reactive Programming

Reactive has specific characteristics that make them ideal for low-latency, high-throughput workloads. Project Reactor and the springWebflux work together to enable developers to build enterprise-grade reactive systems that are responsive, resilient, elastic, and message-driven.

What is reactive processing?

Reactive processing is a paradigm that enables developers to build non-blocking and asynchronous applications which can handle back-pressure (flow control).

Why use reactive processing?

Reactive programming is a design approach that uses asynchronous programming logic to handle real-time adjustments for typically static information. It gives an efficient mechanism — the use of automated data streams — for handling content modifications in response to user queries.

Reactive Manifesto
Principles (applies to system and applications):
● Responsive
○ It needs to respond quickly. The time of the requisition itself is independent of this.
● Resilient
○ It should acknowledge well and support Back-Pressure
○ Messages in control
○ Avoid catastrophic failure
● Elastic
○ Automatic Generation of resources. More threads in our case.
● Message Driven
○ Publisher/Subscriber

What is Spring Webflux?

The Spring Webflux was included in Spring 5 to provide reactive systems support to the Spring-based applications and it is based on Project Reactor and uses non-blocking reactive streams.

To set up Spring Webflux required dependencies :

We just need the dependency, the latest version as of August 2022.

<dependency> 
   
  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-webflux</artifactId>
 
  <version>2.7.0</version> 

</dependency>

Usually, we add spring-boot-starter-web for the traditional MVC framework model but we add spring-boot-starter-web flux for the reactive application using web flux.

This dependency includes the below dependencies

  • springwebflux framework
  • reactor-core that we need for reactive streams
  • reactor-netty (the default server that supports its streams). Any other servlet 3.1+ containers like Tomcat, Jetty, or non-servlet containers like Undertow can be used as well
  • use spring-boot spring-boot-starter for basic Spring Boot application setup.

Reactive Spring Security

Spring Security is used to secure the reactive endpoints / APIs. It works in a similar way as the normal Spring Boot application by using the dependency as shown below-

<dependency> 

  <groupId>org.springframework.boot</groupId> 
        
  <artifactId>spring-boot-starter-security</artifactId>     

</dependency>

We need to use the following annotation and the configuration to set up basic authentication and authorization.

@EnableWebFluxSecurity
public class HelloWebfluxSecurityConfig {

@Bean
 public MapReactiveUserDetailsService userDetailsService() {

  UserDetails user = User.withDefaultPasswordEncoder()

   .username("user")

   .password("user")

   .roles("USER")

   .build();

  return new MapReactiveUserDetailsService(user);

 }

}

SpringWebFlux Basics


Observer Pattern
It Is a practice design pattern that lets you define a subscription mechanism to
notify multiple objects about any incidence that happens to the object they’re
observing.
In other words, we are using declarative programming instead of imperative
programming.

SpringWebFlux Publishers
● Flux
○ Its a publisher for a stream of objects
○ It is used to create lists of objects
○ Processes one stream end to end
○ To handles stream events
● Mono
○ A publisher for a single object
○ Handles object events

Flux. just, Flux. from, Flux. from iterable,
Flux. from the array, Flux. from the stream,
Flux.zip
Mono. just, Mono. from,
Mono.fromCallable, Mono.zip,
Mono. from future, Mono. from direct,
Mono.fromRunnable

SpringWebFlux Parallelism


● Flux and ParallelFlux
○ .parallel(parallelism).runOn(Schedulers.parallel())
● Mono
○ .subscribeOn(Schedulers.parallel())

Reference :

Conclusion :

When we decide whether or not to use Spring WebFlux, we need to take into account that in order to take advantage of it we need to ensure the entire flow uses reactive and asynchronous programming and not any of the operations are blocking. The application will necessary to run on non-blocking servers, just like Netty, Undertow, and Servlet 3.1+ containers, and use a reactive database driver. Also, we need to consider that when processing data we need to manipulate Mono and Flux API operations, which might take some time to get experienced with and can be harder to debug.

Spring WebFlux is well fit for ultra concurrent applications. These applications need to be able to process a large number of requests with as few resources as possible, applications that need scalability, or applications that need to stream request data in a live manner.

Written by 

He is a Software Consultant at Knoldus Inc. He has done B.Tech from Dr. APJ Kalam Technical University Uttar-Pradesh. He is passionate about his work and having the knowledge of various programming languages like Java, C++, Python. But he is passionate about Java development and curious to learn Java Technologies. He is always impatient and enthusiastic to learn new things. He is good skills of Critical thinking and problem solving and always enjoy to help others. He likes to play outdoor games like Football, Volleyball, Hockey and Kabaddi. Apart from the technology he likes to read scriptures originating in ancient India like Veda,Upanishad,Geeta etc.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading