I have a server-client setup, both being Java applications. I am using Spring's RestTemplate with PoolingHttpClientConnectionManager at the client end to call the server. The server (tomcat) has the default keep-alive value of an http-connection as 60 seconds. This means that if the client uses the same connection (lying idle in pool) again after 60 seconds for another request, the server will close the connection. Then, client automatically creates another connection object and sends the request via this connection to the server.
Without going into the code and internal working, I simply want to know that what is the best practice regarding http-client's keep-alive time? Do I
keep the default value (connection is kept-alive indefinitely by the client), OR
set the keep-alive time less than server keep-alive time? In this case http-client will always check, before leasing a connection, that whether that connection has expired or not. If expired, it is discarded from the pool, a new connection is created and leased to the requesting thread.
Related
I've been playing around with an http server I'm creating using boost::asio (hence the c++ tag).
With HTTP/1.1 the default is to keep the clients connection open for more than 1 request/response.
My question is:
How long should I keep a client connection open? Should I use a deadline_timer which closes the connection after some arbitrary amount of time?
Or, should I just wait for the underlying socket receive timeout to expire? At which point my receive handler will be invoked with an EOF error prompting me to remove the client connection from my list of connections.
Furthermore, if this is specified in an RFC document, which one?
When a server sent a keep-alive header to a client
Does it mean that every requests of this client ip will be benefited?
Does it mean that every requests of this client ip plus session will be benefited?
Put it into a situation.
After I browse a website and the server sent keep-alive to me. I open another browser and go to the same website. Will my second request connect without handshake?
I read the documentation but I could not find out the target. Please help me.
In HTTP 1.0, if both the client and server support keep alive then the connection will be persisted and multiple requests can use the same connection without handshaking each time, benefitting the session by slightly reducing request/response time.
In HTTP 1.1, connections are keep alive by default so this is the expected behaviour.
This happens within the session - another browser window would constitute another session, so there would be no connection sharing and therefore no benefit.
Http 1.1 uses persistent connections as it has advantage of sending multiple http request using same connection. My concern is with the below guideline:
You must call the Close method to close the stream and release the connection. Failure to do so may cause your application to run out of connections.
Why should I close the stream or connection after receiving response, if same connection is used for multiple httprequest? I am triggering multiple httprequests for same internet resource so why should I close connection/stream/response each time I receive a response?
What exactly is the difference between long polling and http Keep-Alive??
Doesnt http Keep-Alive solve the issue that long-polling solves??
No. They're almost completely unrelated.
HTTP keepalive allows the client to keep a connection open, but idle, to allow it to make future requests a little bit more efficiently. The server cannot send data to a client over a keepalive connection, as no request is active.
Long polling is a mechanism where the server keeps a request (and thus a connection) active, but not sending data, to allow the server to send data to the client when it becomes available -- for instance, when an event occurs.
I am reading that Keep-Alives is meant for performance - so that no connections need to be recreated but just reuse the existing ones. What if there is a traffic spike, will new connections be created?
Additionally, if I don't turn on Keep-Alive and in a high traffic environment, will it eventually running out of connections/socket port on client side? because a new connection has to be created for each http/web request.
HTTP is a stateless protocol.
In HTTP 1.0 each request meant opening a new TCP connection.
That caused performance issues (e.g. have to re-do the 3-way handshake for each GET or POST) so the Keep-Alive Header was added to maintain the connection across requests and in HTTP1.1 the default is persistent connection.
This means that the connection is reused across requests.
I am not really familiar with IIS but if there is a configuration to close the connection after each HTTP response, it will have impact on the performance.
Concerning the running out of sockets/ports on the client side, that could occur if the client fires a huge amount of requests and a new TCP connection must be opened per HTTP request.
After a while the ports will be depleted