← Lessons

quiz vs the machine

Silver1120

Networking

Keepalive and Connection Reuse

Why reusing a connection beats opening a new one each time.

3 min read · intro · beat Silver to climb

The cost of new connections

Every new TCP connection pays for a handshake, and with TLS a second cryptographic handshake on top. That round trip cost adds latency to the first byte. Connection reuse amortizes that cost across many requests.

Keepalive

In HTTP, the keepalive mechanism keeps the underlying TCP connection open after a response so the next request can skip the handshake. A header controls how long an idle connection may stay open and how many requests it may serve.

  • The first request pays the full setup cost.
  • Subsequent requests on the same connection reuse it.
  • An idle timeout eventually closes unused connections.

At the TCP layer a separate TCP keepalive probe detects dead peers by sending periodic empty segments. Clients also maintain connection pools so a fixed set of warm connections serves bursts of traffic without per request setup.

Key idea

Keeping connections warm and reusing them removes repeated handshake latency from most requests.

Check yourself

Answer to earn rating on the learn ladder.

1. What does HTTP keepalive avoid on later requests?

2. What is a connection pool?