RamPrakash
RamPrakash

Reputation: 3312

Netty Http Client Connection Pool

I am trying to understand netty http client connection pool. If it is NIO and asynchronous, then what is the significance of this connection pool?

For ex: if service A calls service B and service A has the client connection pool count set as 50, then does it mean we can make only 50 parallel requests at the max?

UPDATE:

// remote server

@GetMapping("echo")
public Mono<String> echo(){
    return Mono.just("echo")
                .delayElement(Duration.ofSeconds(3));
}
// client 1 conneciton
HttpClient httpClient = HttpClient.create(ConnectionProvider.newConnection());

WebClient client = WebClient.builder()
                          .baseUrl("http://localhost:8080/echo")
                          .clientConnector(new ReactorClientHttpConnector(httpClient))
                          .build();

// run
var start = System.currentTimeMillis();
Flux.range(1, 50)
    .flatMap(i -> client.get().retrieve().bodyToMono(String.class))
    .collectList()
    .doOnNext(System.out::println)
    .doFinally(s -> System.out.println("Total Time Taken : " + (System.currentTimeMillis() - start) + " ms"))
    .block();

I get all the calls get completed in 3.5 seconds. Ideally with 1 connection I should have got them completed in 150 seconds.

Upvotes: 2

Views: 13950

Answers (1)

Violeta Georgieva
Violeta Georgieva

Reputation: 2302

A connection pool is a cache of connections maintained so that the connections can be reused when future requests to the remote service (database, micro service etc.) are required. Connection pools are used to enhance the performance ... see Connection pool. It does NOT depend on the transport that you will choose: NIO, epoll, kqueue etc. When you have a connection pool, for every connection you do just once the DNS resolution, connection establishment etc. and then you reuse this connection for many requests.

then what is the significance of this connection pool?

When you do NOT have a connection pool then DNS resolution, connection establishment etc. will be made every time when you want to make a request to the remote service.

The connection pool contains connections ONLY to a given remote service. So when you have service A and service B then you will have a connection pool for service A and a connection pool for service B (if these are different remote addresses, Reactor Netty does NOT provide configuration per URI).

In Reactor Netty you can choose to configure the connection pools to have one and the same configuration OR you can configure every pool with different configurations (depending on your use case).

In Reactor Netty you can configure the max connection which mean that for a given service you can make maximum this number of parallel requests (opened connections). The other requests will be kept in a queue and once a connection is available for reuse a pending request can be executed.

As it is mentioned above, all available configuration for the connection pool can be found in the Reference Documentation

Upvotes: 15

Related Questions