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.
Previous
What is graceful shutdown in Node.js and how do you implement it?
Next
What are common Node.js security vulnerabilities and how do you prevent them?
More Node.js Questions
View all →- Advanced How does Node.js handle concurrency without multiple threads?
- Advanced What is the Node.js memory model and how does garbage collection work?
- Advanced What are memory leaks in Node.js and how do you detect them?
- Advanced What is the difference between process.exit() and throwing an uncaught exception?
- Advanced What is the N+1 query problem and how do you solve it in Node.js?