Skip to content
Sign inGet all access
Chapter 7·8 minFree

Connections

Keep-alive, head-of-line blocking, and the socket underneath.

A TCP connection is expensive to open. HTTP/1.1 keeps it.

One connection, many requests

Chapter 2 sent Connection: close every time. Leave it out:

GET / HTTP/1.1
Host: localhost
OutputHTTP/1.1 200 OKServer: nginx/1.27.5Date: <now>Content-Type: text/htmlContent-Length: 615Last-Modified: Wed, 16 Apr 2025 12:55:34 GMTConnection: keep-alive

Content-Length makes this possible — the client counts 615 bytes and knows the response ended without a close.

Two requests on one socket

Write both, read both. The responses come back in order:

terminal
{ printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' ''
  printf '%s\r\n' 'GET /50x.html HTTP/1.1' 'Host: localhost' \
      'Connection: close' ''
} | nc -w 2 localhost 8080 | grep -E '^HTTP|^Content-Length'
OutputHTTP/1.1 200 OKContent-Length: 615HTTP/1.1 200 OKContent-Length: 497

Two files, 615 bytes and 497, one connection. The second request was sent before the first response arrived.

Three works the same, and the statuses need not match:

terminal
{ printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' ''
  printf '%s\r\n' 'GET /50x.html HTTP/1.1' 'Host: localhost' ''
  printf '%s\r\n' 'GET /nope HTTP/1.1' 'Host: localhost' 'Connection: close' ''
} | nc -w 2 localhost 8080 | grep '^HTTP'
OutputHTTP/1.1 200 OKHTTP/1.1 200 OKHTTP/1.1 404 Not Found

Close on the first request and the second is never answered:

terminal
{ printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' \
      'Connection: close' ''
  printf '%s\r\n' 'GET /50x.html HTTP/1.1' 'Host: localhost' \
      'Connection: close' ''
} | nc -w 2 localhost 8080 | grep -c '^HTTP'
Output1

HTTP/1.0 and keep-alive

The default flipped between versions. 1.0 closes unless asked:

terminal
printf '%s\r\n' 'GET / HTTP/1.0' '' \
  | nc localhost 8080 | grep -iE '^(HTTP|connection)'
OutputHTTP/1.1 200 OKConnection: close

Asking changes the answer:

terminal
printf '%s\r\n' 'GET / HTTP/1.0' 'Connection: keep-alive' '' \
  | nc -w 2 localhost 8080 | grep -iE '^(HTTP|connection)'
OutputHTTP/1.1 200 OKConnection: keep-alive

Who closes it

Either side, any time. nginx waits 65 seconds by default:

nginx.conf
keepalive_timeout  65;

curl reuses connections

Give it two URLs and it opens one connection:

terminal
curl -sv localhost:8080/ localhost:8080/50x.html \
  -o /dev/null -o /dev/null 2>&1 | grep -E 'Connected to|Re-using'
Output* Connected to localhost (::1) port 8080* Re-using existing connection with host localhost

One Connected to, one Re-using. curl will also count them:

terminal
curl -s -o /dev/null -o /dev/null -w 'connects=%{num_connects}\n' \
  localhost:8080/ localhost:8080/50x.html
Outputconnects=1connects=0

The second URL reports 0 new connections. A client object is meant to be shared, not created per request.

Head-of-line blocking

Responses come back in request order. Swap the requests and the lengths swap:

terminal
{ printf '%s\r\n' 'GET /50x.html HTTP/1.1' 'Host: localhost' ''
  printf '%s\r\n' 'GET / HTTP/1.1' 'Host: localhost' 'Connection: close' ''
} | nc -w 2 localhost 8080 | grep '^Content-Length'
OutputContent-Length: 497Content-Length: 615

That is head-of-line blocking — the problem HTTP/2 solves, in chapter 12.

Pipelining

Sending request two before response one is what the snippets above did. HTTP/1.1 allows it; nobody deployed it:

Problem Why it killed pipelining
Ordering is still fixed one slow response blocks the rest
Proxies mishandled it responses returned out of order, or dropped
No way to cancel one the whole connection has to go

Browsers shipped it disabled, then removed it. It works above because nothing sits between the two ends.

Parallel connections

The workaround was more sockets — six per host, so six handshakes and six TLS negotiations.

That is why bundling mattered before HTTP/2: the seventh request waits.

Ports

The port decides where to connect, nothing else:

GET / HTTP/1.1
Host: localhost:8080
Connection: close
OutputHTTP/1.1 200 OK