In this post, I would like to explain three ways of implementing a rest client in Spring boot / Java. Using WebFlux Webclient, native RestTemplate and Apache Client.
The simplest way is the native RestTemplate provided by the Spring Framework. It is blocking, and for each request, the connection is closed afterward. There is no connection pool or connection manager, which makes the requests costly and inefficient.
The settings here are the connection timeout and the read timeout. This means that the connection timeout defines how long the client waits to establish a connection, while the read timeout defines how long the client waits for a response after the connection has been established.
The more common approach is to use the Apache HttpClient dependency, which provides additional configuration options such as max-total, max-per-route, and connection-request-timeout-ms for more robust connection management and better performance. It also supports connection pooling, which reduces the overhead of creating a new connection for every request and improves the efficiency and scalability of the application.
max-total defines: the maximum number of total connections that can exist in the connection pool.
max-per-route: defines the maximum number of connections allowed for a specific target host or route.
connection-request-timeout-ms: defines how long the client waits to obtain a connection from the connection pool before throwing a timeout exception.
The third option is the reactive WebClient, which comes from Spring WebFlux. Unlike RestTemplate, it is non-blocking and asynchronous. This means the application does not wait for the response in a blocking way. Instead, it can continue working on other tasks and gets notified when the response is ready.
WebClient provides better scalability and resource usage, especially in applications with many concurrent requests. It is commonly used in modern microservice architectures and high-performance systems.
Some important properties and concepts are:
- connect-timeout: defines how long the client waits to establish a connection.
- response-timeout: defines how long the client waits for the server response.
max-in-memory-size: defines the maximum size of data stored in memory while processing a response. - Connection pooling is supported through Reactor Netty, which improves performance by reusing existing connections.
- Non-blocking I/O allows threads to handle multiple requests simultaneously instead of waiting for one request to finish.
It gives a quick response to the caller and notifies or calls back the application when the server response is ready. This makes the system more efficient and scalable under high load.

Hinterlasse einen Kommentar