19

I quickly browsed the source code of Feign, I found that Feign uses JDK's HttpUrlConnection to issue HTTP request and close it when request finished without using a connection pool. I doubt the efficiency of this kind of manner. Then I read the document of Spring's RestTemplate which says RestTemplate can switch to Apache Http Client or OKHttp to send HTTP request:

Note: by default the RestTemplate relies on standard JDK facilities to establish HTTP connections. You can switch to use a different HTTP library such as Apache HttpComponents, Netty, and OkHttp through the HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) property.

Does it mean RestTemplate is better than Feign at the view of performance?

Neo
  • 2,196
  • 5
  • 32
  • 58

2 Answers2

7

Old question, but probably worth mentioning here that as of Spring 5 the RestTemplate is deprecated in favor of the WebClient.

James Gawron
  • 871
  • 10
  • 27
1

In comparison with Feign, RestTemplate takes advantage on the default client performance (although it is mapped that this client has its issues with Java 11 regarding connections resets), but it loses on the ease of integrating logging libs and the more verbose and harder to test programmatic approach.

Another good point in favor of Feign is the easiness to implement fallback strategies combined with Hystrix, implementing custom ErrorDecoder.

If you want to go deeper about how Feign implementation, take a look at this article.

Talking about performance, another attention point of RestTemplate is that it uses the Java Servlet API, which is based on the thread-per-request model. This means that the thread will block until the web client receives the response, which can lead to degraded performance and to waste resources such as memory and CPU cycles, specially when communicating with slow services. On the other hand, this is not an issue for Feign because it can be used with async clients, which are not thread blocking.

Diego Rojas
  • 237
  • 5
  • 11