You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Check Peck <co...@gmail.com> on 2015/06/22 23:51:14 UTC

How to decide optimal settings for setMaxTotal and setDefaultMaxPerRoute?

I have a RestService running on 45 different machines in three datacenters
(15 in each datacenter). I have a client which uses RestTemplate to call
these machines depending on where the call is coming from. If the call is
coming from DC1, then it will call my rest service running in DC1 and
similarly for others.

I am using RestTemplate with HttpComponentsClientHttpRequestFactory as
shown below:

    public class DataProcess {

        private RestTemplate restTemplate = new RestTemplate();
        private ExecutorService service = Executors.newFixedThreadPool(15);

        // singleton class so only one instance
        public DataProcess() {
            restTemplate.setRequestFactory(clientHttpRequestFactory());
        }

        public DataResponse getData(DataKey key) {
            // do some stuff here which will internally call our RestService
            // by using DataKey object and using RestTemplate
        }

        private ClientHttpRequestFactory clientHttpRequestFactory() {
            HttpComponentsClientHttpRequestFactory requestFactory = new
HttpComponentsClientHttpRequestFactory();
            RequestConfig requestConfig =
RequestConfig.custom().setConnectionRequestTimeout(1000).setConnectTimeout(1000)

.setSocketTimeout(1000).setStaleConnectionCheckEnabled(false).build();
            PoolingHttpClientConnectionManager
poolingHttpClientConnectionManager = new
PoolingHttpClientConnectionManager();
            poolingHttpClientConnectionManager.setMaxTotal(300);
            poolingHttpClientConnectionManager.setDefaultMaxPerRoute(300);

            CloseableHttpClient httpClientBuilder =
HttpClientBuilder.create()

.setConnectionManager(poolingHttpClientConnectionManager).setDefaultRequestConfig(requestConfig).build();

            requestFactory.setHttpClient(httpClientBuilder);
            return requestFactory;
        }

    }

And this is the way people will call our library by passing dataKey object:

    DataResponse response =
DataClientFactory.getInstance().getData(dataKey);

*Now my question is:*

1) How to decide what should I choose for setMaxTotal and
setDefaultMaxPerRoute in PoolingHttpClientConnectionManager object? As of
now I am going with 300 for both of them? Should I go with 500 or something
else?

2) Also do I need to turn off Nagle's algorithm (TCP_NODELAY) and turn on
TCP keep-alive packets (SO_KEEPALIVE) for better performance here? If yes,
then what's the right way to do this?

I am trying to get best performance out of HttpClient. My client library
will be used under very heavy load in multithreading project. I am using
Apache HttpClient 4.3

Re: How to decide optimal settings for setMaxTotal and setDefaultMaxPerRoute?

Posted by Check Peck <co...@gmail.com>.
Does anyone have any idea on this?

On Mon, Jun 22, 2015 at 2:51 PM, Check Peck <co...@gmail.com> wrote:

> I have a RestService running on 45 different machines in three datacenters
> (15 in each datacenter). I have a client which uses RestTemplate to call
> these machines depending on where the call is coming from. If the call is
> coming from DC1, then it will call my rest service running in DC1 and
> similarly for others.
>
> I am using RestTemplate with HttpComponentsClientHttpRequestFactory as
> shown below:
>
>     public class DataProcess {
>
>         private RestTemplate restTemplate = new RestTemplate();
>         private ExecutorService service = Executors.newFixedThreadPool(15);
>
>         // singleton class so only one instance
>         public DataProcess() {
>             restTemplate.setRequestFactory(clientHttpRequestFactory());
>         }
>
>         public DataResponse getData(DataKey key) {
>             // do some stuff here which will internally call our
> RestService
>             // by using DataKey object and using RestTemplate
>         }
>
>         private ClientHttpRequestFactory clientHttpRequestFactory() {
>             HttpComponentsClientHttpRequestFactory requestFactory = new
> HttpComponentsClientHttpRequestFactory();
>             RequestConfig requestConfig =
> RequestConfig.custom().setConnectionRequestTimeout(1000).setConnectTimeout(1000)
>
> .setSocketTimeout(1000).setStaleConnectionCheckEnabled(false).build();
>             PoolingHttpClientConnectionManager
> poolingHttpClientConnectionManager = new
> PoolingHttpClientConnectionManager();
>             poolingHttpClientConnectionManager.setMaxTotal(300);
>             poolingHttpClientConnectionManager.setDefaultMaxPerRoute(300);
>
>             CloseableHttpClient httpClientBuilder =
> HttpClientBuilder.create()
>
> .setConnectionManager(poolingHttpClientConnectionManager).setDefaultRequestConfig(requestConfig).build();
>
>             requestFactory.setHttpClient(httpClientBuilder);
>             return requestFactory;
>         }
>
>     }
>
> And this is the way people will call our library by passing dataKey object:
>
>     DataResponse response =
> DataClientFactory.getInstance().getData(dataKey);
>
> *Now my question is:*
>
> 1) How to decide what should I choose for setMaxTotal and
> setDefaultMaxPerRoute in PoolingHttpClientConnectionManager object? As of
> now I am going with 300 for both of them? Should I go with 500 or something
> else?
>
> 2) Also do I need to turn off Nagle's algorithm (TCP_NODELAY) and turn on
> TCP keep-alive packets (SO_KEEPALIVE) for better performance here? If yes,
> then what's the right way to do this?
>
> I am trying to get best performance out of HttpClient. My client library
> will be used under very heavy load in multithreading project. I am using
> Apache HttpClient 4.3
>