title: Using Spring REST template, either creating too many connections or slow
body: <p>I have a RESTful service that works very fast. I am testing it on localhost. The client is using Spring REST template. I started by using a naive approach:</p>
  
  <pre><code>RestTemplate restTemplate = new RestTemplate(Collections.singletonList(new GsonHttpMessageConverter()));
  
  Result result = restTemplate.postForObject(url, payload, Result.class);
  </code></pre>
  
  <p>When I make a lot of these requests, I am getting the following exception:</p>
  
  <pre><code>Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8080/myservice":No buffer space available (maximum connections reached?): connect; nested exception is java.net.SocketException: No buffer space available (maximum connections reached?): connect
  </code></pre>
  
  <p>This is caused by connections not being closed and hanging in TIME_WAIT state. The exception starts happening when the ephemeral ports are exhausted. Then the execution waits for the ports to be free again. I am seeing peak performance with long breaks. The rate I am getting is almost what I need, but of course, these TIME_WAIT connections are not good. Tested both on Linux (Ubuntu 14) and Windows (7), similar results at different times due to different ranges of the ports.</p>
  
  <p>To fix this, I tried using an HttpClient with HttpClientBuilder from Apache Http Components library.</p>
  
  <pre><code>RestTemplate restTemplate = new RestTemplate(Collections.singletonList(new GsonHttpMessageConverter()));
  HttpClient httpClient = HttpClientBuilder.create()
          .setMaxConnTotal(TOTAL)
          .setMaxConnPerRoute(PER_ROUTE)
          .build();
  restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
  
  Result result = restTemplate.postForObject(url, payload, Result.class);
  </code></pre>
  
  <p>With this client, I see no exceptions. The client is now using only a very limited number of ephemeral ports. But whatever settings I use (TOTAL and PER_ROUTE), I can't get the performance I need.</p>
  
  <p>Using the <code>netstat</code> command, I see that there are not many connections done to the server. I tried setting the numbers to several thousands, but it seems the client never uses that much.</p>
  
  <p>Is there anything I can do to improve the performance, without opening too many connections? </p>
  
  <hr>
  
  <p>UPDATE: I've tried setting number of total and per route connections to 5000 and 2500 but it still looks like the client is not creating more than a hundred (judging from <code>netstat -n | wc -l</code>). The REST service is implemented using JAX-RS and running on Jetty.</p>
  
  <p>UPDATE2: I have now tuned the server with some memory settings and I am getting really good throughput. The naive approach is still a bit faster, but I think it's just a little overhead of the pooling on client side.</p>