Keep-Alive
Persistent connections reduce latency by reusing TCP
and TLS sessions across requests. The HTTP
Keep-Alive header sets idle timeout and request
limit parameters for those connections when the
Connection header is set to
keep-alive. The header is an IANA-registered
permanent header, part of the HTTP/1.0 and
HTTP/1.1 connection model. In HTTP/2
and HTTP/3 the header is forbidden, and a
message carrying it is malformed.
Usage
The Keep-Alive header works alongside the Connection header to fine-tune persistent connection behavior in HTTP/1.1. When a connection is marked as persistent, this header communicates how long the connection remains idle and how many requests the connection handles before closing.
Both clients and servers send this header. A server includes Keep-Alive in a response to inform the client of the connection limits enforced on the server side. A client includes the header to express preferences, though the server is not required to honor them.
Persistent connections reduce latency by avoiding repeated TCP handshakes and, when TLS is involved, repeated TLS negotiations.
Keep-Alive is a hop-by-hop header. In HTTP/1.1
the parameters take effect only when the
Connection header names keep-alive,
and an intermediary strips the header before
forwarding the message to the next hop. A proxy that
misses this step hands the parameters of one
connection to a different one.
Keep-Alive in HTTP/2 and HTTP/3
HTTP/2 and HTTP/3 forbid Keep-Alive
along with the other connection-specific fields
(Connection, Proxy-Connection, Transfer-Encoding
and Upgrade). An endpoint receiving a message
carrying any of them treats the message as malformed,
which in HTTP/2 means a stream error and in a browser
surfaces as a protocol error for the whole page.
Both protocols keep connections open as a matter of
course and manage idle time with their own frames, so
the header has nothing left to say.
The practical failure is a backend speaking HTTP/1.1
behind a proxy or CDN speaking HTTP/2 to visitors. A
response carrying Keep-Alive: timeout=5, max=100
is fine on the backend hop, and the proxy has to drop
the header when it re-frames the response. Mainstream
proxies do, and a Keep-Alive or Connection
header reaching an HTTP/2 client points at a proxy
misconfiguration or a hand-rolled gateway, often
paired with an ERR_HTTP2_PROTOCOL_ERROR in the
browser console.
Directives
The HTTP/1.1 specification defines no Keep-Alive
parameters. The timeout and max parameters below
are server-implementation conventions adopted
widely, with Apache the best-known sender.
timeout
The timeout parameter specifies the minimum number
of seconds the host keeps an idle connection open.
An idle connection is one where no data is transmitted
in either direction for the specified duration. The
host is free to keep the connection open longer than
the stated value.
max
The max parameter states how many more requests
the server accepts on the connection. Apache counts
it down with each response, starting from its
MaxKeepAliveRequests setting. The parameter is
informational, since a server enforces its limit by
sending Connection: close and closing the
connection, and the only specification describing
the parameter deprecates it for that reason.
Example
A server responds with keep-alive parameters, setting a 10-second idle timeout and a limit of 100 requests on the connection. After 100 requests or 10 seconds of inactivity, the server closes the connection.
HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/html; charset=utf-8
Keep-Alive: timeout=10, max=100
A shorter timeout is common on high-traffic servers to free resources more aggressively. This response allows a 5-second idle window and permits up to 500 requests.
HTTP/1.1 200 OK
Connection: keep-alive
Keep-Alive: timeout=5, max=500
See also
- RFC 2068: HTTP/1.1, Section 19.7.1.1 (obsoleted)
- RFC 9113: HTTP/2, Section 8.2.2
- RFC 9112: HTTP/1.1, Appendix C.2.2
- Connection
- Http-Connection
- HTTP headers