Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring WebFlux WebClient resilience and performance

I just test by sample PoC project some blocking / non blocking solutions in simple common scenario.

Scenario:

  • There are rest blocking endpoint which is quite slow - each request tooks 200 ms.
  • There are other - client application, which call this slow endpoint.

I have tested current (blocking) Spring boot client (tomcat), Spring Boot 2.0 (netty) with WebFlux - WebClient, Ratpack and Lagom. In each cases I have stressed client application by gatling test simple scenario (100-1000 users / second).

I have tested ratpack and lagom as reference non blocking io servers to compare results to spring boot (blocking and non blocking).

In all cases i have results as expected, except spring boot 2.0 test. Its working only for small load levels but even then with high latency. If load level rises up - all requests are time outed.

WebClient usage :

@RestController
public class NonBlockingClientController {
private WebClient client = WebClient.create("http://localhost:9000");

@GetMapping("/client")
public Mono<String> getData() {
    return client.get()
            .uri("/routing")
            .accept(TEXT_PLAIN)
            .exchange()
            .then(response -> response.bodyToMono(String.class));
}
}

I have no idea what goes wrong or current snapshot version just working that.

All sources published at https://github.com/rutkowskij/blocking-non-blocking-poc

  • blocking-service - slow blocking endpoint
  • non-blocking-client - Spring Boot 2.0 and WebClient based client

I just created a simple Spring Boot application using spring-boot-starter-webflux with version 2.0.0.BUILD-SNAPSHOT which brings spring-webflux version 5.0.0.BUILD-SNAPSHOT and same for Spring Core, Beans, Context, etc.

like image 339
Jakub Rutkowski Avatar asked Mar 30 '17 21:03

Jakub Rutkowski


People also ask

Is WebClient better than RestTemplate?

RestTemplate will still be used. But in some cases, the non-blocking approach uses much fewer system resources compared to the blocking one. So, WebClient is a preferable choice in those cases.

Should I use spring WebFlux?

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

How does spring WebFlux work internally?

Spring WebFlux is a parallel version of Spring MVC and supports fully non-blocking reactive streams. It supports the back pressure concept and uses Netty as the inbuilt server to run reactive applications. If you are familiar with the Spring MVC programming style, you can easily work on webflux also.

Is spring WebClient thread safe?

WebClient is thread-safe because it is immutable. WebClient is meant to be used in a reactive environment, where nothing is tied to a particular thread. Here we see how to create a WebClient instance, and use it to build and execute an HTTP request.


Video Answer


1 Answers

Issue no longer exists after 5.0 RC4 release. The issue was related to connection pooling in reactor-netty and reactor-core.

I've also tested with Spring Boot 2.0.0.M4 - everything looks fine now.

Details: http://jira.spring.io/browse/SPR-15584

like image 128
Jakub Rutkowski Avatar answered Sep 28 '22 09:09

Jakub Rutkowski