What is connection pooling for HTTP requests in Node.js?

Why Interviewers Ask This

Interviewers ask this to evaluate whether you have the depth of knowledge needed to mentor others and lead technical decisions. The expected answer goes beyond definitions into practical implications and real-world consequences.

Answer

HTTP connection pooling (keep-alive connections) reuses existing TCP connections for multiple HTTP requests to the same server, avoiding the overhead of TCP handshake and TLS negotiation for every request. Node.js's built-in http/https modules support this via http.Agent. By default, Node.js uses a global agent with keep-alive disabled (or limited). For high-throughput outbound HTTP requests: const agent = new https.Agent({ keepAlive: true, maxSockets: 50, maxFreeSockets: 10, timeout: 60000 }); const response = await fetch(url, { agent });. The axios library uses keep-alive by default. undici — the newer, high-performance HTTP client in Node.js core — uses connection pools internally and is much faster than the older http module. Key settings: maxSockets (max concurrent connections per host — higher increases throughput but consumes server resources), keepAlive (reuse TCP connections), and timeout (idle socket timeout). For microservices making many outbound calls, proper pool tuning can significantly reduce latency and CPU usage.

Pro Tip

This topic has Node.js-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.