Reputation: 2888
Does Apache HttpClient batch requests to the same server to reduce round-trip time?
Upvotes: 0
Views: 1763
Reputation: 9717
There is no term called batch requests in terms of HTTP. However if you mean, sending requests by using same TCP connection, so that avoiding additional round trip times for each request, it's called keep-alive
In HTTP1.1, default is to keep-alive the connection, so you don't need to do anything. Server decides to whether keep the connection alive or not.
In HTTP1.0 you have to add keep-alive header to ask for keeping the connection alive.
This is the example for Apache HttpClient to insert Connection: keep-alive header.
HttpClient client = HttpClients.custom().build();
HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL)
.setHeader(HttpHeaders.CONNECTION, "keep-alive").build();
client.execute(request);
It works simply like this: When you send an HTTP request, you need to establish a TCP connection between server and you. It takes one round trip time, after that you can send your request.
After sending your request, there are two options, to close the TCP connection, or not. If you close the TCP connection, server releases the resources assigned to you(good for server) but when you want to send another request you need to open a new connection. If you don't close the connection you avoid additional RTT's, but server's resources are unavailable to other users while the connection is open.
Upvotes: 1