Keep-Alive


Keep-alive (also keep alive) generally refers to communication connections in a network which are not terminated but are held until the client or server interrupts the connection. The key feature of keep alive connections is the sending of a content-free message between a server and a client. With this message, one of the network users (client or server) can control whether the connection will be maintained and prevent it from being canceled. If the connection is still available, it can be used for further data exchange.

Keep alive connections are also referred to as HTTP keep-alive, persistent HTTP connections, and HTTP connection reuse. The HTTP 1.1 protocol supports keep-alive by default, and also utilise HTTP pipelining to process requests in batches. HTTP 2 extends the process of persistent connections with additional options (for example, multiplexing).

General information

Conventional network communication operates according to a request-response scheme. A client requests specific data from a server. The server responds by acknowledging the presence of the data or denying it and issuing an error code. Once the data is available, the client requests it by establishing a new connection. Subsequently, the client interprets the data and presents it. If it is not complete, additional data is requested to complete the display. A new connection is used for this and sometimes data is generated which is not needed (overhead). In the HTTP protocol 1.0, a new TCP connection is always established for each request. This means that each request from a client is treated individually by the server and, if possible, answered individually.

Websites usually consist of different data resources. For example, HTML files, CSS scripts, and scripts that affect user interactions.  Images or multimedia files are also included. Basically, HTTP 1.0 requires a separate connection for each file, which must also be terminated. This approach is particularly inefficient for larger sites because the network utilization is very high (network congestion).[1] In order to prevent many individual connections from being established and terminated again, network management was extended by persistent connections and HTTP pipelining. For the HTTP 1.1 protocol, it is now possible to implement multiple requests and responses per connection, with a specific keep-alive message being sent between client and server for each connection and requests can now be stored in a pipeline.

How it works

Most servers can be set up and configured to support keep-alive.[2] On the client side, the most frequently used browsers are already capable of establishing persistent connections. This is, however, a question of configuration on the server side. Depending on the technology and programming language used, the necessary changes differ slightly in syntax and semantics. For an Apache server, keep-alive can be allowed in the configuration file "httpd.conf". Three properties are particularly important:[3]

  • KeepAlive: With this property, the values ​​on and off can be entered. For HTTP 1.1, on is the default value.[4]
  • MaxKeepAliveRequests: This property defines the maximum possible requests per connection. A value between 50 and 100 is usually sufficient. However, this value depends on the size of the web project or the number of files on a website.
  • KeepAliveTimeout: If the server does not receive any requests, it is idle and maintains the connection until it is canceled. The timeout property limits the time that the server must wait for a new request. Approximately 10 seconds are considered ideal, but the timeout can be set higher for websites with a lot of traffic.

If the corresponding properties and values ​​are entered in the server configuration, the Apache server sends answers to requests that look something like this:[5]

~$ curl -I https://www.domain.com/file.html
HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8
Date: Thu, 15 Jan 2015 16:45:29 GMT
Content-Length: 1845
Keep-Alive: timeout=10, max=20
Server: Apache/2.4.9 (Unix) PHP/5.6.2

The principle is similar for all servers. Very small messages, called keep-alive-messages are exchanged between the client and the server. They allow the client to receive multiple files within a connection. The keep-alive message is transmitted using the HTTP header. It tells the client or server that the connection should be maintained. Each request is then made via a connection. Only once one of the network participants breaks the connection, multiple requests and answers are no longer possible.

Keep alive through .htaccess

If the website operator does not have access to the server configuration, the changes can also be made in the htaccess file. The .htaccess overwrites the configuration of the Apache server by recording the following source code:

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

The keep-alive header will now be appended to each request. A test is recommended to check whether the change is implemented by the server as desired. However, more precise definitions of timeouts and maximum requests can only be made in the configuration file of the respective server.

Relevance to programming

Persistent connections are provided as standard in the HTTP 1.1 protocol.  HTTP 2 protocol extends this standard by a multiplexing method, which further optimizes data transmission. Whether and to what extent these options are used depends on the technical infrastructure and the client. This means that the protocols allow different optimization, but the implementation sometimes has to take place on the server side.

For certain web projects, keep-alive headers are particularly recommendable. This is true for websites with many files and extensive multimedia content, as well as for on-line shops that rely on HTTPS connections. HTTPS is generally resource-intensive in the transmission of files because the connection is encrypted using SSL (Secure Socket Layer). By adding a keep-alive header, the loading speed of websites can be significantly shortened because the latencies between different connections are completely avoided and there is only one connection that allows multiple requests and responses which the server can process them one at a time.

Moreover, since resources such as the CPU (central processing unit) and the memory are needed when establishing new connections on the TCP / IP layer (which is under the application layer of the HTTP protocol), the CPU can also be relieved by the keep-alive header. The computer must set up significantly fewer TCP connections.[6]

References

  1. Congestion techopedia.com. Accessed on 06/06/2016
  2. How to enable keep alive varvy.com. Accessed on 06/06/2016
  3. Apache Performance Tuning: Keep alive to remove latency maanasroyy.wordpress.com. Accessed on 06/06/2016
  4. HTTP Keep Alive web.archive.org. Accessed on 06/06/2016
  5. What is Keep-Alive? maxcdn.com. Accessed on 06/06/2016
  6. Apache optimization: Keep Alive On or Off? abdussamad.com. Accessed on 06/06/2016

Web Links