Keep alive (connection maintenance mechanisms) generally refers to communication connections in a network that are not terminated but are maintained until the connection is interrupted by the client or server. The key feature of keeping them alive is sending a message with no content 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 enabled, it can be used for data exchange.
In addition, keep alive connections are called HTTP keep-alive, persistent HTTP connections, and HTTP connection reuse. The HTTP 1.1 protocol supports keep-alive by default, and also uses the HTTP pipeline to process requests in batches. HTTP 2 extends the persistent connections procedure with additional options (for example, multiplexing).
Conventional network communication operates according to a scheme of request-response. 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. The client then interprets the data and presents it. If it is not complete, additional data is required to complete the visualization. A new connection is used for this and sometimes unnecessary data is caused (overhead surcharge). In the HTTP 1.0 protocol, a new TCP connection is always defined for each request. This means that each request from a client is individually treated by the server and, if feasible, answered individually.
Websites usually consist of different data resources. For example, HTML files, CSS scripts, and scripts that impact user interactions. In addition, images or multimedia files are included. Simply, HTTP 1.0 requires a separate connection for each file, which must also be concluded. This approach is particularly inefficient for larger websites because the network utilization is very high (network congestion). To prevent many individual connections from being established and terminated again, network management was extended through persistent connections and HTTP pipes. For the HTTP 1.1 protocol, it is now feasible to implement multiple requests and responses per connection, with a specific keep-alive message that is sent between the client and the server for each connection and requests can now be stored in the pipeline
How does it work
Most servers can be configured to support "keep-alive". On the client side, the most used browsers are already capable of determining persistent connections. This is, however, a matter of configuration on the server side. Depending on the technology and the programming language used, the necessary changes differ slightly in syntax and semantics. For an Apache server, keep-alive can be allowed in the "httpd.conf" configuration file. Three properties are particularly important:
- KeepAlive: With this property, the on and off values can be entered. For HTTP 1.1, it is the default value.
- MaxKeepAliveRequests: This property sets 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 in a web portal.
- 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 the server must wait for a new request. About 10 seconds is considered ideal, but the wait time can be longer for high traffic websites.
If the corresponding properties and values are entered in the server configuration, the Apache server sends responses to requests that look as follows:.
~ $ 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 the same for all servers. The client and the server exchange very small messages, called "keep-alive-messages." They allow the client to receive various files within a connection. The keep alive message is transmitted using the HTTP header. Tells the client or server that the connection should be maintained. Each request is made through a connection. Only once one of the network participants breaks the connection, it is no longer feasible to perform various requests and responses.
Keep alive by means of .HTACCESS
If the web operator does not have access to the server configuration, the changes can also be made in the htaccess file. The .htaccess file overwrite the Apache server configuration by recording the following source code:
Header set Connection keep-alive
The "Keep Alive" header will be added to each request. It is recommended to run a test to see if the server implements the change as desired. However, more precise definitions of timeouts and peak requests can only be made in the corresponding server configuration file.
Relevance for programming
Persistent connections are provided as standard in the HTTP 1.1 protocol. The HTTP 2 protocol expand this standard by means of a multiplexing method, which further optimizes data transmission. The use of these options and to what extent depends on the technical infrastructure and the customer. This means that the protocols allow different optimizations, but the implementation sometimes has to take place on the server side.
For certain web projects, Keep-alive headers are specifically recommended. This is valid for websites with a lot of files and extensive multimedia content, as well as for online stores that depend on HTTPS connections. HTTPS is generally resource intensive in file transmission because the connection is encrypted using SSL (Secure Socket Layer). By adding a "keep alive" header, the loading speed of the webs can be significantly shortened because latencies between the different connections are completely avoided and there is only one connection that allows several requests and responses that the server can process one at the same time .
At the same time, since resources such as the CPU (central processing unit) and memory are required to determine new connections at the TCP / IP layer (which is below the application layer of the HTTP protocol), the CPU can also be freed by the keep-alive header. The PC must configure significantly fewer TCP connections.