← Back to Node.js, page 3

nodejs-0041 · Hard senior

What do keepAliveTimeout, headersTimeout and requestTimeout do on a Node.js HTTP server, and why do they matter?

keepAliveTimeout (default 5 s) closes idle persistent connections. headersTimeout (60 s) limits how long a client may take to send the request headers, and requestTimeout (300 s) limits the whole request. They defend against slow-client attacks and must be tuned relative to the load balancer in front, or you will see random 502 errors.

Full answer

HTTP/1.1 keeps a TCP connection open between requests (keep-alive) so clients avoid a new handshake each time. Without limits, a connection that does nothing, or sends one byte per second, can hold server resources forever. A large number of such connections is the "slowloris" attack.

Main server settings (defaults on current Node):

  • server.keepAliveTimeout, 5000 ms: how long an idle connection waits for the next request before the server closes it.
  • server.headersTimeout, 60000 ms: how long the client has to send the complete headers.
  • server.requestTimeout, 300000 ms: how long the client has to send the whole request, including the body. Slow clients get a 408 response and the connection is closed. The check runs every connectionsCheckingInterval (30 s by default), so the timeout is not exact.
  • server.timeout, 0 (disabled): inactivity timeout on the socket. Use setTimeout on individual requests for finer control.
  • server.maxRequestsPerSocket: closes a connection after N requests.

The load balancer problem: suppose a load balancer keeps idle connections to your server for 60 seconds, but your Node server closes idle connections after 5 seconds. Sometimes the balancer sends a request on a connection at the moment Node closes it. The client sees a 502 or a connection reset. The usual rule: set the Node keepAliveTimeout higher than the balancer's idle timeout (for example 65 s against 60 s), and keep headersTimeout above keepAliveTimeout.

Also set limits on header size (--max-http-header-size) and body size, and put timeouts on outgoing requests, since a hanging call to another service ties up your handler.

javascript
const http = require('node:http');
const net = require('node:net');

const defaults = http.createServer();
console.log('default keepAliveTimeout:', defaults.keepAliveTimeout, 'ms');
console.log('default headersTimeout:  ', defaults.headersTimeout, 'ms');
console.log('default requestTimeout:  ', defaults.requestTimeout, 'ms');
console.log('default timeout:         ', defaults.timeout, '(0 means off)');

// A strict server for the demo: the whole request must arrive within 200 ms.
const server = http.createServer(
  { requestTimeout: 200, connectionsCheckingInterval: 50 },
  (req, res) => {
    req.resume(); // read the body, then answer
    req.on('end', () => res.end('ok'));
  },
);

server.listen(0, '127.0.0.1', () => {
  // A slow client promises 10 body bytes but sends none.
  const socket = net.connect(server.address().port, '127.0.0.1', () => {
    socket.write('POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 10\r\n\r\n');
  });
  let reply = '';
  socket.on('data', (chunk) => (reply += chunk));
  socket.on('close', () => {
    console.log('slow client received:', reply.split('\r\n')[0]);
    server.close();
  });
});

The demo shortens requestTimeout to 200 ms and the check interval to 50 ms. Production values are far larger. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): default keepAliveTimeout: 5000 ms default headersTimeout: 60000 ms default requestTimeout: 300000 ms default timeout: 0 (0 means off) slow client received: HTTP/1.1 408 Request Timeout

Key points

  • keepAliveTimeout closes idle connections, headersTimeout and requestTimeout limit slow requests.
  • Defaults: 5 s, 60 s and 300 s.
  • Slow clients can tie up resources, so these limits are a security feature.
  • Keep Node's keepAliveTimeout above the load balancer idle timeout.

Common mistakes

  • Leaving a Node server with the 5 second keep-alive behind a load balancer with a 60 second idle timeout.
  • Disabling all timeouts to 'fix' a slow request instead of finding the cause.
  • Forgetting timeouts on outgoing HTTP calls.
  • Assuming requestTimeout fires at exactly the configured millisecond.

Follow-ups

  • What is a slowloris attack?
  • How would you debug random 502 errors from a load balancer?
  • What timeout would you set on an outgoing fetch?

Tips

  • Give the defaults from memory and say they are tunable.
  • Explain the load balancer mismatch, because it is a real production bug.
  • Say that timeouts are a security setting.