Hi friends, today we are going to discuss about keep-alive connections and its significance. First of all, let’s discuss a little about how website works and how connections are maintained.
How website works?
In general below is the flow of any web request.
- User types any address in any browser e.g. www.google.com.
- Browser sends request to web server.
- Web server creates a new process or assign a thread to process this request.
- Web server processes the request and generate the response.
- This response is sent back to client.
- Now assigned process or thread is free to receive another requests.
- Browser displays the response to user.
This is very basic and top view of the process.
Overview of TCP connection
Whenever two machines communicates with each other (In current example, this communication is between client and the web server), a connection is created. Using this connection, machines communicate with each other. This connection is called as TCP pipe\connection.
TCP connection maintains source, destination IP address and port information so that connection can be created and resource could be transferred.
You can get more information about TCP connection and layers here.
As we know that any webpage can have multiple resources like css files, js files, images etc. When we open any webpage then a new connection is created for each resource. So if a web page contains 10 images, 4 css files, and 2 js files then for each of these 16 files, new connection will be created and then resource will be loaded. Once resource downloading is completed, connection gets closed.
What is Keep-Alive?
As we just read that for each web resource, a new connection is created and resource is transferred. Once resource is transferred then connection gets closed. However this is pretty simple process but a little inefficient.
If request is for same server then why we can’t keep connection open so that all required resources can be transferred using same connection. And once all resources are done then connection may be closed.
Yes, you got me right. Keep-alive does the same thing. Whenever we provide instruction to keep the TCP connection alive, then TCP connection is not terminated after resource transfer. Other files can be transferred using same connection.
Benefits of using Keep-Alive
Now we know that using keep alive we can transfer multiple resources using one connection. We don’t need to get a new connection for every resource. We can have following benefits using this approach.
We know that TCP connection creation is a process which CPU need to do for every resource. By using keep alive we can reduce CPU load so that it can be utilized more efficiently.
HTTPS Connection utilization:
When site is on HTTPS connection then it is beneficial to use one connection as new connection creation and handshakes is an expensive process.
Webpage load speed:
As more files can be transferred using single connection so transfer speed improves and page loads faster. Using multiple connection creation process can slow up website load speed.
How to enable Keep-Alive header?
Let’s see how we can enable this setting for any website.
Open IIS in your web server and select the site for which you want to change the setting.
Double click on “HTTP Response Headers”
Click on “Set Common Headers”. Dialog box will open. Check the setting “Enable HTTP Keep-Alive”.
If we uncheck this box then “Connection: close” header will be sent to client which indicates that this connection will be closed once response is sent.
Any disadvantages for using keep alive?
Now we know that having single connection for all resource files from a particular source to destination is a good idea. But do you really think that we can use this setting blindly?
No, there are few points to consider here while enabling this setting. From client side we are fine if connection remain open all the time. But this can be painful for server.
We know that in normal configuration webserver close the connection once resource is delivered. Due to this nature, web server is able to handle lots of requests as server resources gets freed up after file is done. But if connection remain open then few server resources are occupied to main that connection.
So consider if 1000s of clients are remain their connection open for long time then web server may be impacted with serious issues as lots of server resources will be occupied and web server performance will be reduced .
So think twice while enabling this setting. If you think that enabling this setting will improve your overall experience then go ahead and enable it. But there are few factors which will impact this setting.
Let’s discuss those factors so that you can use this setting efficiently.
Factors which Impact keep-Alive
Now let’s discuss those factor which will impact this setting.
It sets the maximum number of requests for every connection.
A value of 100 is normally good enough for almost any scenario. This value, however, can be increased depending on the amount of files within a web page that the server is supposed to deliver. If any webpage contains 100 files to deliver then this value can be increased.
This setting tell about how long not active connection should remain in server. Once any connection is in server more than this timeout value without any work then server should destroy this.
A value between 7 to 10 seconds is usually ideal. With higher traffic this value can go extremely higher to make sure there is no frequent TCP connection re-initiated. If this value goes down too much, Keep-Alive loses its purpose.
All modern browsers use persistent connections till the time server have no objection.
With HTTP/1.0 implementation, this setting was disabled by default. We can enable it. But with HTTP/1.1, Keep-Alive is implemented differently and the connections are kept open by default. HTTP/1.1 connections are always active.
To disable this setting in HTTP/1.1 we need to set the response header “Connection: close”. If we are not sending this close header then connection will remain open. But this connection will not remain open forever.