Node.js › HTTP & Web Servers

How would you implement HTTP/2 or HTTP/1.1 connection pooling on the client side in Node.js when making many outgoing requests to the same downstream API?

Advanced

Answer

Node's http.Agent (or the equivalent for https) manages a pool of reusable connections to a given host, controlling the maximum number of concurrent sockets and whether keep-alive is used -- explicitly configuring a custom agent with an appropriate maxSockets value (rather than relying on the global default agent, which has more conservative defaults) can significantly improve throughput for a service making many concurrent outgoing requests to the same downstream dependency.
const https = require('node:https');

const agent = new https.Agent({ keepAlive: true, maxSockets: 100, maxFreeSockets: 10 });

async function callDownstreamApi(path) {
  return fetch(`https://api.example.com${path}`, { agent }); // reuses pooled connections
}
Real-world example A service making hundreds of concurrent calls per second to a single downstream payment API configures a dedicated https.Agent with a higher maxSockets limit specifically for that dependency, since the default global agent's conservative connection limit was becoming a throughput bottleneck under peak load.

Common follow-ups: What's the risk of setting maxSockets too high for a downstream service that has its own connection limits?;How does connection pooling behavior differ between the newer built-in fetch() and the older http/https modules in Node.js?