What is HTTP Keep-Alive
HTTP keep-alive, a.k.a., HTTP persistent connection, is an instruction that allows a single TCP connection to remain open for multiple HTTP requests/responses.
Enabling the keep-alive header allows you to serve all web page resources over a single connection. Keep-alive also reduces both CPU and memory usage on your server.
Enabling the Keep-Alive Header
In the event that keep-alive is not enabled on your server, it can be turned on by adding the following code to your .htaccess file:
<IfModule mod_headers.c> Header set Connection keep-alive> <IfModule>
Within the ‘Connection keep-alive’ header, the following two directives can affect its functionality.
MaxKeepAliveRequests– This directive sets the maximum number of requests for every keep-alive connection. When determining this figure, it’s important to take into account the number of files on your website that a user might want to access.
KeepAliveTimeout– This directive sets the time that a server should wait for user requests before a new TCP connection needs to be established. This figure should be set according to how frequently your website is visited, i.e., sites with high traffic volumes will want to have a large timeout value to limit the number of TCP connection requests.
The Benefits of Connection Keep Alive
The HTTP keep-alive header maintains a connection between a client and your server, reducing the time needed to serve files. A persistent connection also reduces the number of TCP and SSL/TLS connection requests, leading to a drop in round trip time (RTT).
Establishing a TCP connection first requires a three-way handshake – a mutual exchange of SYN and ACK packets between a client and server before data can be transmitted. Using the keep-alive header means not having to constantly perform this process. This results in:
- Network resource conservation – It’s less taxing on network resources to use a single connection per client.
- Reduced network congestion – Reducing the number of TCP connections between your servers and clients can lead to a drop in network congestion.
- Decreased latency – Reducing the number of three-way handshakes can lead to improved site latency. This is especially true with SSL/TLS connections, which require additional round-trips to encrypt and verify connections.
CDNs and Keep-Alive Connections
Keep-alive connections allow CDNs to reduce your site’s RTT while still providing SSL/TLS security benefits.
The Imperva CDN uses keep-alive to maintain an open connection with your origin-server in between user sessions, for a few minutes at a time—as long as your site is visited while the connection is open, your CDN doesn’t need to engage in any new SSL/TLS negotiations.
This saves a considerable amount of overhead that would have been used to initiate a new connection request with your origin for every new user request. Instead, each CDN proxy server is able to leverage its open connection to download resources for many users at once.