Basic Introduction to Spring WebFlux Last Updated : 31 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Spring WebFlux is a reactive, non-blocking web framework that uses Project Reactor's reactive streams API to enable highly concurrent and asynchronous processing of web requests in a non-blocking and event-driven way. It is fully asynchronous and non-blocking using reactive streams and callbacks. It uses Project Reactor as the underlying reactive library for composable asynchronous and event-based programming.The core abstraction is around reactive streams - publishers that provide push-based asynchronous streams of data.Controllers return Publisher or Mono types instead of ModelAndView. Responses are streamed back incrementally.It is optimized for non-blocking and event-driven applications on modern servers like Netty and Undertow.In this article, we will explore the advantages and disadvantages of Spring WebFlux – a non-blocking web framework. Flow of Spring WebFluxA client sends an HTTP request to the server which is handled by the Netty/Undertow server.The server dispatches the request to the Spring WebFlux dispatcher handler.The dispatcher handler routes the request to the appropriate controller based on the request path.The controller returns a Mono or Flux type which will emit a stream of responses asynchronously.The dispatcher handler wraps the Mono/Flux in a ServerResponse and returns it.The serverResponse is streamed back to the client incrementally as the Mono/Flux emits values, without blocking threads.Any exceptions are caught and handled gracefully instead of blocking threads.The client receives the response asynchronously without the server blocking on I/O.Core principles of Spring WebFluxAsynchronous operations: All I/O operations like database access, network calls etc are asynchronous and non-blocking. This prevents threads from blocking on I/O.Reactive streams: Data is processed and consumed asynchronously as streams of multiple values over time rather than single blocking responses.Reactive Streams compliance: WebFlux is built on Project Reactor which implements Reactive Streams. This allows asynchronous and non-blocking processing of data streams.Non-blocking requests: The underlying server (Netty by default) is fully asynchronous and non-blocking. It can handle many more requests concurrently without blocking threads.Key Features of Spring WebFluxNon-blocking I/O: Uses asynchronous and non-blocking I/O to avoid blocking threads, enabling high concurrency.Reactive streams support: Fully implements Reactive Streams specification using Project Reactor for reactive programming.Functional programming model: Controllers return Mono/Flux publishers instead of blocking responses, favoring functional style.Server-sent events: Supports server-sent events out of the box for real-time full-duplex communication.WebSockets: Built-in support for WebSockets to enable real-time bidirectional communication.Reactive database access: Seamless integration with reactive databases like MongoDB, Cassandra, Redis etc.Caching support: Supports caching responses using Cache API in a non-blocking manner.Advantages of WebFluxScalability: It can handle more requests concurrently using less resources due to non-blocking I/O. This improves scalability.Responsiveness: Applications remain highly responsive even under heavy load since requests are not blocked waiting for I/O.Resource efficiency: Fewer threads are required to handle the same number of requests than blocking applications.Resiliency: Failure of one request impacts only some. Backpressure prevents overloading.Disadvantages of WebFluxDebugging challenges: Asynchronous code can be more difficult to debug compared to sequential blocking code.Complexity: Reactive code involving streams, and callbacks can become more complex than simple blocking code.Latency sensitivity: Reactive applications require consistently low latency to perform well compared to blocking.Migration effort: Existing blocking code needs to be refactored for non-blocking APIs, which requires time and effort.Use Cases of WebFluxFinancial trading systemsReal-time collaboration toolsVideo/audio streaming PlatformsLive tracking systemsDifference between Spring WebFlux and Spring MVCAs Spring WebFlux is an alternative of Spring MVC, so let us know the core differences between them. Point Spring Web-Flux Spring MVC Architecture Reactive Programming Model (Reactive Streams) Synchronous/Blocking Model Request Handling Asynchronous and non-blocking. Synchronous request-response model. Supported Servers Netty, Undertow etc. Servlet containers like Tomcat. Performance Better for IO-bound apps due to non-blocking. Degrades with concurrency due to blocking. Use Cases Best for data-heavy real-time applications. Works well for request-response applications. Create Quiz Comment P pranay0911 Follow 0 Improve P pranay0911 Follow 0 Improve Article Tags : Geeks Premier League Advance Java Java-Spring Java-Spring-Boot Geeks Premier League 2023 Java-Spring-WebFlux +2 More Explore Java Enterprise EditionIntroduction to Java Servlets4 min readLife Cycle of a Servlet4 min readIntroduction to JSP4 min readJSP Architecture2 min readJSF | Java Server Faces4 min readEnterprise Java Beans (EJB)4 min readMultithreadingJava Multithreading Tutorial3 min readJava Thread Class5 min readLifecycle and States of a Thread in Java5 min readJava Thread Priority in Multithreading3 min readMain thread in Java4 min readConcurrencyjava.util.concurrent Package9 min readJava.util.concurrent.Executor interface with Examples1 min readJava.util.concurrent.ExecutorService Interface with Examples3 min readJava Runnable Interface3 min readCallable vs Future in Java2 min readDifference Between Callable and Runnable in Java3 min readJDBC (Java Database Connectivity)JDBC (Java Database Connectivity)3 min readJDBC Drivers4 min readEstablishing JDBC Connection in Java5 min readTypes of Statements in JDBC4 min readJava FrameworksIntroduction to Spring Framework7 min readSpring - Understanding Inversion of Control with Example6 min readIntroduction to Spring Boot4 min readSpring - MVC Framework3 min readHow to Create a REST API using Java Spring Boot?4 min readWhat is Spring Data JPA?4 min readSpring - JDBC Template7 min readSpring Hibernate Configuration and Create a Table in Database4 min readAspect Oriented Programming (AOP) in Spring Framework3 min readIntroduction to Spring Security and its Features3 min readWhat is Spring Cloud3 min readIntroduction and Working of Struts Web Framework3 min readJUnitIntroduction to JUnit 57 min readJUnit 5 vs JUnit 42 min readHow to Write Test Cases in Java Application using Mockito and Junit?3 min readUnit Testing in Spring Boot Project using Mockito and Junit4 min readJUnit 5 - Test Suites with Example2 min readJUnit 5 â JaCoCo Code Coverage5 min read Like