Skip to content

HTTP Keep-Alive

Allows reusing the same TCP connection for multiple HTTP requests instead of opening a new one each time.

Without Keep-Alive

Request 1:
Client --- TCP Handshake (SYN, SYN-ACK, ACK)
Client --- HTTP GET /index.html
Client --- Response
Client --- TCP Close (FIN, FIN-ACK)

Request 2:
Client --- TCP Handshake again
Client --- HTTP GET /style.css
...

Every request pays the TCP handshake cost (1 RTT minimum).

With Keep-Alive

Client --- TCP Handshake (once)
Client --- HTTP GET /index.html
Client --- Response
Client --- HTTP GET /style.css
Client --- Response
Client --- HTTP GET /script.js
Client --- Response
Client --- TCP Close (once)

One connection, multiple requests — saves RTT overhead.

How It Works

Request header:
  Connection: keep-alive
  Keep-Alive: timeout=5, max=100

Response header:
  Connection: keep-alive
  • timeout — how long to keep connection idle before closing
  • max — max requests allowed on this connection

HTTP Versions & Keep-Alive

VersionKeep-AliveDefault
HTTP/1.0OptionalOff (close after each request)
HTTP/1.1Built-inOn by default
HTTP/2Always onMultiplexing (multiple requests simultaneously)
HTTP/3Always onQUIC (UDP based)

Keep-Alive vs HTTP/2 Multiplexing

HTTP/1.1 Keep-Alive:
Connection reused but requests still sequential
--- req1 --- res1 --- req2 --- res2  (one at a time)

HTTP/2 Multiplexing:
Multiple requests in flight simultaneously on same connection
--- req1 --- req2 --- req3 --- res2 --- res1 --- res3  (any order)

Keep-Alive solves connection overhead. HTTP/2 solves head-of-line blocking on top of that.

Head of Line Blocking (HOL) with Keep-Alive

req1 (slow) --------------------> res1
req2 (fast) -- waiting for req1 -- res2
req3 (fast) -- waiting ----------- res3

req2 and req3 stuck behind slow req1 even though they're ready. HTTP/2 fixes this with multiplexing.

Key Points

  • Keep-Alive = TCP connection reuse.
  • Default in HTTP/1.1, always on in HTTP/2+.
  • Reduces latency, saves server resources.
  • Server can close idle connections via timeout.
  • Load balancers also support keep-alive between LB and backend.