HTTP persistent connection
HTTP persistent connection, also called HTTP keep-alive, or HTTP connection reuse, is the idea of using a single TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair. The newer SPDY protocol uses the same idea and takes it further to allow multiple concurrent requests/responses to be multiplexed over a single connection.
Under HTTP 1.0, there is no official specification for how keepalive operates. It was, in essence, added to an existing protocol. If the client supports keep-alive, it adds an additional header to the request:
Then, when the server receives this request and generates a response, it also adds a header to the response:
Following this, the connection is not dropped, but is instead kept open. When the client sends another request, it uses the same connection. This will continue until either the client or the server decides that the conversation is over, and one of them drops the connection.
In HTTP 1.1, all connections are considered persistent unless declared otherwise. The HTTP persistent connections do not use separate keepalive messages, they just allow multiple requests to use a single connection. However, the default connection timeout of Apache 2.0 httpd is as little as 15 seconds and for Apache 2.2 only 5 seconds. The advantage of a short timeout is the ability to deliver multiple components of a web page quickly while not consuming resources to run multiple server processes or threads for too long.
- Lower CPU and memory usage (because fewer connections are open simultaneously).
- Enables HTTP pipelining of requests and responses.
- Reduced network congestion (fewer TCP connections).
- Reduced latency in subsequent requests (no handshaking).
- Errors can be reported without the penalty of closing the TCP connection.
These advantages are even more important for secure HTTPS connections, because establishing a secure connection needs much more CPU time and network round-trips.
According to RFC 2616 (page 46), a single-user client should not maintain more than 2 connections with any server or proxy. A proxy should use up to 2×N connections to another server or proxy, where N is the number of simultaneously active users. These guidelines are intended to improve HTTP response times and avoid congestion. If HTTP pipelining is correctly implemented, there is no performance benefit to be gained from additional connections, while additional connections may cause issues with congestion.
For services where single documents are regularly requested (for example, image hosting websites), Keep-Alive can be massively detrimental to performance due to keeping unnecessary connections open for many seconds after the document was retrieved.
Due to increased complexity, persistent connections are more likely to expose software bugs in servers, clients and proxies.
Use in web browsers
By default, Internet Explorer versions 6 and 7 use two persistent connections while version 8 uses six. Persistent connections time out after 60 seconds of inactivity which is changeable via the Windows Registry.
In Firefox, the number of simultaneous connections can be customized (per-server, per-proxy, total). Persistent connections time out after 115 seconds (1.92 minutes) of inactivity which is changeable via the configuration.
- Persistent HTTP Connections in RFC 2616 "Hypertext Transfer Protocol -- HTTP/1.1"
- Apache HTTP Server - KeepAlive Directive
- Apache HTTP Server 2.0 - KeepAliveTimeout Directive
- Apache HTTP Server 2.2 - KeepAliveTimeout Directive
- Multiple (wiki). "Httpd/KeepAlive". Docforge. Retrieved 2010-01-30.
- Nielssen, Henryk; Gettys, James; et al. (September 17, 1997), "Network Performance Effects of HTTP/1.1, CSS1, and PNG", Computer Communication Review 27 (4), ISSN 0146-4833
- "Opera 4.0 Upgrades File Exchange: Includes HTTP 1.1". Opera Software. 2000-03-28. Retrieved 2009-07-08.
- "IE8 speeds things up". stevesouders.com. 2008-03-10. Retrieved 2009-07-17.
- "How to change the default keep-alive time-out value in Internet Explorer". Microsoft. 2007-10-27. Retrieved 2009-07-17.
- "Network.http.keep-alive.timeout". mozillazine.org. Retrieved 2009-07-17.