How to enable keep alive on the server / website

What is the Issue?

Enable keep alive on the website since http connection gets closed very soon once request is done.This means that the default HTTP connection is usually closed after each request has been completed, i.e the server closes the TCP connection after delivering the response. In order to keep the connection open for multiple requests, the keep-alive connection header can be used.


Why has this issue occurred?

By default HostingRaja VPS or cloud based server is not enabled with keep alive headers because If the client does not close the connection when all of the data it needs has been received, the resources needed to keep the connection open on the server will be unavailable for other clients and it will impact on server load due to more RAM consumption.


How can we can enable keep alive?

Keeping the server alive has the highest impact on the reliability of the server. For the VPS and above categorized website hosting services in india the root access is given by which you can change the configuration in order to achieve this.

Follow the following steps to enable the keep alive header:

  1. If you have root access to the HostingRaja VPS or cloud server you can add the below mentioned lines on /etc/sentora/configs/apache/httpd.conf in the bottom as:

KeepAlive: Whether or not to allow persistent connections (more than one request per connection). Set to "Off" to deactivate.

 

KeepAlive On


MaxKeepAliveRequests: The maximum number of requests to allow during a persistent connection. Set to 0 to allow an unlimited amount.

# We recommend you leave this number high, for maximum performance.

#

MaxKeepAliveRequests 100


#

# KeepAliveTimeout: Number of seconds to wait for the next request from the

# same client on the same connection.

#

KeepAliveTimeout 100



Once these lines are added you need to restart apache service using the command:

 service httpd restart


2. If you are not having HostingRaja VPS or Cloud server root access then you need to add the below lines on your .htaccess file as :


<ifModule mod_headers.c> Header set Connection keep-alive </ifModule>


3. To know whether keep alive is active or not you need to visit this URL:

https://varvy.com/pagespeed/


Output will be as follows:

keep alive

Advantages of Keep alive

  1. Improves website speed means it reduces latency associated with HTTP transfers and delivers a better user experience.

  2. Reduces CPU Usage For example, If web page is having dozens of different files such as images, stylesheets, javascript files etc. If KeepAlive is disabled a separate connection must be made for each of those files. Creating and closing connections has an overhead and doing it for every single file wastes the CPU time.


Disadvantages of Keep alive

Enabling Keep Alive  increases memory usage on the server. In server Apache processes have to keep connections open waiting for new requests from established connections. While they are waiting they are occupying RAM that could be used to service other clients. If you turn off KeepAlive apache processes will remain active. This will lower memory usage and allow Apache to serve more users.


When you should enable Keep Alive

RAM is very biggest limiting factor in web server, If you are having very less RAM then its better to turnoff keep alive on your server because having Apache processes hanging around while they wait for more requests from persistent connections is a waste of precious memory.


If you have pages with a lot of images or other files linked into them, KeepAlive will improve the user experience significantly. This is because a single connection will be used to transfer multiple files.


It is very important to make sure that type of traffic you are getting on daily basis.If you are getting more hits to your website in a short period of time then its better to disable the Keep alive because your RAM usage will get exceeded if it is in enable mode.


Note; When you buy any server solution from HostingRaja you get free domain name registration India. And you can choose your desired domain extensions that is .com or .in.