← Lessons

quiz vs the machine

Gold1370

Networking

The HTTP Keep Alive Timeout

How persistent connections are reused and when they are closed.

4 min read · core · beat Gold to climb

Reuse the connection

Opening a TCP connection costs a round trip, and with TLS it costs more. HTTP keep alive lets a client send many requests over one connection instead of opening a fresh one each time, saving handshakes.

The timeout

A reused connection cannot stay open forever, so each side enforces a keep alive timeout:

  • After a response, the connection stays idle and available for the next request.
  • If no new request arrives within the timeout, the server closes the connection.
  • A maximum request count may also cap how many requests one connection serves.

The client and server timeouts can differ, and the smaller one effectively wins. A connection closed by the server between requests forces the client to open a new one.

The race condition

A subtle bug appears when the server timeout is shorter than the client expects. The client may send a request just as the server closes the connection, causing a failed request that a retry usually fixes. Tuning the client idle timeout below the server value avoids this.

Key idea

Keep alive reuses a connection across requests, but an idle keep alive timeout closes it, and a mismatch between client and server timeouts can cause races.

Check yourself

Answer to earn rating on the learn ladder.

1. Why use HTTP keep alive?

2. What causes the keep alive race condition?