Search code examples
javaokhttp

Connection Pool - OkHttp


We are using OkHttp in WAS environment Could you please help us with the below queries -:

  • Question 1: What should be the ideal connection pool size and keep Alive for a container environment , is there any formula to calculate it, we will be using Okhttp client to connect to two different URL's

  • Question 2: We don't want to have any client side failures , how does OkHttp handle stale connections , I don't see any parameter in OkHttp to check for stale connections?

HTTP Java client Java has this parameter to turn on stale connection check:

http.connection.stalecheck

We are using OkHttp client as mentioned below , is there any important configuration am I missing?

new OkHttpClient.Builder()
              .readTimeout(10,TimeUnit.SECONDS)
              .retryOnConnectionFailure(false)
              .connectTimeOut(5,TimeUnit.SECONDS)
              .connectionPool(new ConnectionPool(10,5,TimeUnit.SECONDS)
              .build();

Solution

  • What should be the ideal connection pool size and keep Alive for a container environment

    Take 256 as a starting point. It's big enough that you'll get a good hit rate and small enough that you won't notice the memory used.

    If you hit lots of different hosts frequently you may adjust it up. If you are running on very small containers or have memory sensitivity you may adjust it down.

    We dont want to have any client side failures

    There's a setting on OkHttpClient.Builder, retryOnConnectionFailure. It's true by default and will handle client-side failures for you. If you did want to handle client-side failures you'd set this to false.