Optimize Your Website: Enable Keep Alive
Let’s begin by understanding what actually keep alive means.
Whenever there is a request for a file done by the browser from a server, one by one downloading of file takes place, i.e. after every single file download there is a break in connection which again opens to download the next file. So, the number of processes increases, which finally delays the loading of the website.
So, to resolve this issue the basic idea is to establish such a connection which ensures that all downloading of all the required files from same connection without multiple make and break of the connections.
Here HTTP keep alive play a significant role, as it enables loading of all the files in a single request via same open connection, this cuts off the continuous connecting and disconnecting time and thus leads to much faster loading of the content.
How to check Keep Alive status
Most of the online tools read the HTTP Header of your URL and tell whether keep-alive connections are enabled by detecting the keep-alive configurations listed in the header response.
Here are some of the tools that can be used to check the Keep-Alive status:
- Recently we used Giftofspeed tool for checking the Keep-Alive.
- Online tools like GTmetrix notify whether keep alive is active or not
- Other tools like varvy also notify if keep alive is in off mode.
General Terms Related to Keep-Alive to be Kept in Mind
- KeepAlive: Switches KeepAlive on or off. Use “KeepAlive on” to turn it on and “KeepAlive off” to turn it off.
- MaxKeepAliveRequests: The maximum number of requests a single persistent connection will service. A number ranging between 50 and 75 would be enough.
- KeepAliveTimeout: This determines that how long should the server wait for new requests from connected clients. The default is 15 seconds which is way too high. Set it something between 1 to 5 seconds to avoid having processes that are wasting RAM while waiting for requests.
Ways to Enable Keep Alive
Code in .htaccess file
<ifModule mod_headers.c> Header set Connection keep-alive </ifModule>
Add this code in your .htaccess file and it will enable keep alive for the requests.
In Apache server using config file
# KeepAlive On for more than one request per connection. #Set to "Off" to deactivate (for new connection for every request). KeepAlive On # MaxKeepAliveRequests: The maximum number of requests to allow # During a persistent connection, set MaxKeepAliveRequests to 0 to allow an unlimited amount. # You can keep its number high to attain maximum performance. MaxKeepAliveRequests 100 # KeepAliveTimeout: Number of seconds to wait for the next request from the # same client on the same connection. KeepAliveTimeout 100
For Nginx Server
For Keep Alive
The number of connections to an upstream server that remain open for each worker process. There is no default value for this.
Default: keepalive_disable msie6;
Syntax: keepalive_disable none | browser …;
For Keep Alive Timeout
How long an idle keepalive connection remains open.
Default: keepalive_timeout 75s;
Syntax: keepalive_timeout timeout [header_timeout];
For Keep Alive Number Of Request per connection
It is a number of requests a client can make over a single keepalive connection. The default value is 100, but a much higher value can be especially useful for sending a large number of requests from a single client.
Default: keepalive_requests 100;
Syntax: keepalive_requests number;
How It works
The module of Keep-alives was added to HTTP, mainly to significantly reduce the overhead of rapidly creating & closing socket connections for each new request in same session.
Let’s assume that when a page is requested, 5-7 files are needed to get loaded before page actually shows up.
When Keep Alive in not active
After page is requested all the elements that are needed to show the page start loading one after the one. If keep Alive is not active a new request is created for every single element to load. After every element get loaded, there is a break in connection and for further request by the browser server says No. So, there is a delay as new connection is to be established for every element to download.
When Keep Alive is active
If Keep Alive is active all the elements needed gets downloaded in a single, go via single connection.
Mostly by default keep alive is active but if not, you can activate it with the above discussed methods. But even after following the above instructions if keep alive is still not activated then you need to contact your hosting company as they sometimes don’t keep it in the on mode from their side. You can request them to turn it on. Mostly this case appears when the hosting is shared.
Activating keep-alive enables us to use one established TCP/IP connection to fetch and retrieve multiple web files (all the files your web pages load) at once. So, there will be no need of establishing a new connection for every single file. This helps in speeding up the process of retrieving web files since it makes sure that one persistent TCP/IP connection is used instead of establishing multiple connections. This in turn results in faster page load times.
Really helpful graphics is used to clear. I have read on multiple threads but it totally. Great post with descriptive graphics