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.
Note
The Keep-Alive header applies to HTTP/1.1 connections only. HTTP/2 and HTTP/3 manage connection persistence through their own protocol mechanisms.
Directives
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. For
non-pipelined connections, this parameter is ignored
unless the value is 0. The parameter is designed
for HTTP pipelining, where multiple requests are sent
without waiting for each response.
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
Note
HTTP Keep-Alive and TCP keepalive serve different purposes. HTTP Keep-Alive controls connection reuse at the application layer, letting multiple requests share one TCP connection. TCP keepalive is an OS-level mechanism sending probe packets to detect dead connections after long idle periods. The two operate independently.
Note
Persistent connections hold server resources (memory, file descriptors, thread slots) for the duration of the idle timeout. On high-traffic servers, long timeouts exhaust connection pools and block new clients. Balancing timeout length against available resources is essential for stable operation.