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. HTTP/2 and
HTTP/3 manage connection persistence through
their own mechanisms.
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.
Directives
The HTTP/1.1 specification defines no Keep-Alive
parameters. The timeout and max parameters below
are conventions established by Apache and adopted
across server implementations.
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 sets the maximum number of
requests the connection accepts before closing. The parameter was designed
for HTTP pipelining, where multiple requests are sent
without waiting for each response, and carries little
effect on non-pipelined connections.
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
- RFC 9112: HTTP/1.1, Appendix C.2.2
- Connection
- Http-Connection
- HTTP headers