Back to results for “Architecture”
Node.js › HTTP & HTTPS Modules

What is HTTP keep-alive, and how does Node's http.Agent manage connection reuse for outgoing requests?

Intermediate

Answer

HTTP keep-alive lets a single TCP connection be reused for multiple sequential HTTP requests to the same host, avoiding the overhead of establishing a new TCP (and, for HTTPS, TLS) handshake for every individual request -- Node's http.Agent manages a pool of these persistent connections for outgoing requests, and reusing an existing Agent (rather than letting one be created fresh per request) is important for performance when an application makes many requests to the same downstream service.
const http = require('node:http');

const agent = new http.Agent({ keepAlive: true, maxSockets: 50 });

// Reusing the same agent across many requests to the same host avoids repeated handshakes
fetch('https://api.example.com/data', { agent }); // (with a library supporting a custom agent)
Real-world example A service making thousands of outgoing requests per minute to the same downstream API saw a meaningful latency improvement after ensuring a single shared, keep-alive-enabled http.Agent was reused across all those requests, rather than the default behavior of Node.js creating a fresh connection (and TLS handshake, for HTTPS) for many of them.

Common follow-ups: What's the tradeoff of setting maxSockets very high versus keeping it constrained for a given downstream service?;How does connection reuse interact with a downstream server's own connection timeout settings?