Skip to content
Keep-Alive Connections
step 1/5

Reading — step 1 of 5

Read

~1 min readConcurrency & Persistence

Keep-Alive Connections

In HTTP/1.0, every request opened a new TCP connection — slow and wasteful. HTTP/1.1 made keep-alive the default: the connection stays open after a response so the next request reuses it.

Two signals control this:

  • Connection: close — client/server requests close after this response
  • Connection: keep-alive — explicit (default in 1.1, opt-in in 1.0)

The server loop becomes:

accept(socket) -> conn
while True:
    request = parse(conn)
    if request is None: break          # client closed
    response = handle(request)
    send(conn, response)
    if request.headers.get("connection") == "close": break
    if request.version == "HTTP/1.0" and not explicit_keep_alive: break
close(conn)

Add an idle timeout (15-60s typical) so dead clients release sockets.

Pipelining (multiple requests sent before any response) is rarely used in practice — modern HTTP clients use HTTP/2 multiplexing instead.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…