Monday, June 9, 2025

spring-cloud-starter-gateway-server-webflux vs spring-cloud-starter-gateway-server-webmvc

 The difference between spring-cloud-starter-gateway-server-webflux and spring-cloud-starter-gateway-server-webmvc lies in their underlying technology stack, concurrency model, and official support in Spring Cloud Gateway. Here's a breakdown:

1. spring-cloud-starter-gateway-server-webflux (Reactive/WebFlux)

  • Technology Stack:
    Built on Spring WebFlux (reactive stack) and Project Reactor.

  • Concurrency Model:
    Uses non-blocking I/O (event-loop) for high scalability and efficiency with concurrent requests.

  • Dependencies:
    Includes spring-boot-starter-webflux (reactive Netty, Undertow, etc.).

  • Performance:
    Optimized for high-throughput, low-latency scenarios (e.g., microservices, streaming).

  • Official Support:
    This is the standard/officially supported starter for Spring Cloud Gateway.

  • Use Case:
    Reactive applications, cloud-native microservices requiring scalability.

2. spring-cloud-starter-gateway-server-webmvc (Blocking/WebMVC)

  • Technology Stack:
    Built on Spring WebMVC (servlet stack) with blocking I/O.

  • Concurrency Model:
    Uses thread-per-request (blocking) model, limiting scalability under heavy load.

  • Dependencies:
    Includes spring-boot-starter-web (Tomcat, Jetty, etc.).

  • Performance:
    May bottleneck under high concurrency due to thread overhead.

  • Official Support:
    Not officially supported or maintained by Spring Cloud.
    Likely a community experiment or deprecated prototype.

  • Use Case:
    Only for legacy servlet applications (not recommended for new projects).


Key Technical Differences:

Aspectwebflux (Reactive)webmvc (Blocking)
HTTP ServerNetty, Reactor-based serversTomcat, Jetty (Servlet containers)
Request HandlingAsynchronous (event-loop)Synchronous (thread-per-request)
ScalabilityHigh (efficient resource usage)Limited (thread exhaustion risk)
Backpressure SupportYes (via Project Reactor)No
Reactive StreamsFull support (Mono/Flux)Not applicable

Why Spring Officially Recommends WebFlux:

  1. Gateway Design:
    Spring Cloud Gateway relies on reactive APIs (e.g., ServerWebExchangeReactor).

  2. Compatibility:
    Integrates seamlessly with reactive Spring projects (Spring Data R2DBC, RSocket).

  3. Performance:
    Non-blocking I/O handles 10K+ concurrent connections with minimal threads.

  4. Future-Proof:
    Aligns with cloud-native/async trends (gRPC, WebSocket, streaming).


Should You Use webmvc?

  • Avoid it.
    The webmvc variant is unsupported, may have bugs, and negates the core benefits of Spring Cloud Gateway (scalability, non-blocking).

  • If you need a blocking API gateway, consider alternatives:

    • Spring Cloud Netflix Zuul (deprecated but servlet-based).

    • Traditional reverse proxies (Nginx, HAProxy).

Correct Starter for New Projects:

Use the standard (and only officially maintained) dependency:

xml
Copy
Download
Run
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId> <!-- Implicitly uses WebFlux -->
</dependency>

This automatically pulls spring-cloud-starter-gateway-server-webflux under the hood.

Summary:

  • webflux: Production-ready, reactive, high-performance.

  • webmvc: Experimental/legacy, blocking, unsupported.
    Always choose spring-cloud-starter-gateway (WebFlux) for new work.

No comments:

Post a Comment