Question presented to candidate: "Your service makes 5 outbound HTTP requests to the same downstream API back to back. By default, does each one open a brand-new TCP connection, or are they reused — and how would you actually check?"
What a strong answer should cover:
http.Agentmanages connection pooling and reuse for outbound HTTP requests — it decides whether a new TCP connection is opened per request or an existing one is kept alive and reused for a subsequent request to the same host.- 📌 Verified, not assumed: 5 sequential requests through an agent with
keepAlive: falsecreated 5 separate underlying sockets — confirmed by directly counting realcreateConnectioninvocations. The identical 5 requests through an agent withkeepAlive: truecreated only 1 socket, reused for all 5 — a dramatic, directly measured difference, not a theoretical claim about "keep-alive being more efficient." - This connects directly to the prompt's own question: by default, Node's global
http/httpsagent historically has not enabledkeepAlive— each request can open a fresh connection unless an agent withkeepAlive: trueis explicitly configured and used, exactly the distinction verified above. - The real, concrete benefit of connection reuse: avoiding the repeated TCP handshake (and TLS negotiation, for HTTPS) cost per request — the same underlying cost the dedicated connection-pooling question measures for database connections, applied here to outbound HTTP calls specifically.
Agentalso controlsmaxSockets— the maximum number of concurrent connections to a single host — a real, tunable limit preventing a service from unintentionally opening an unbounded number of simultaneous connections to one downstream dependency under heavy concurrent load.- A precise answer names that modern Node's built-in
fetch(built onundici, covered in its own dedicated question) has its own separate connection-pooling mechanism, genuinely different from the classichttp.Agent— a precise answer does not conflate the two APIs' connection-management internals as identical just because both eventually make an HTTP request.
Clarifying questions expected:
- "Is this about the classic
http/httpsmodule specifically, or the newer built-infetch?" — their connection-pooling mechanisms genuinely differ. - "Is the downstream service a single host called repeatedly, where connection reuse would actually matter, or many different one-off hosts?"
Code / implementation expected: Yes — the real, measured socket-count difference (5 sockets without keep-alive vs. 1 socket with it, for the identical 5 requests) is the concrete, dramatic proof of the Agent's actual effect, not a description of "keep-alive is more efficient."