Basic Introduction to Spring WebFlux
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 WebFlux
- A 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 WebFlux
- Asynchronous 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 WebFlux
- Non-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 WebFlux
- Scalability: 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 WebFlux
- Debugging 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 WebFlux
- Financial trading systems
- Real-time collaboration tools
- Video/audio streaming Platforms
- Live tracking systems
Difference between Spring WebFlux and Spring MVC
As 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. |