As the lead for Node 24 services that call payment and inventory APIs, which outbound HTTP client defaults would you standardise, and which built-in defaults would you refuse to inherit?
answer
- one client stack per service
- unlimited pools by default
- minutes-long idle timers
- bodies, proxies and shutdown
basics
~10 sStandardise one client, explicit per-upstream connection caps, a mandatory per-call AbortSignal.timeout, a settle-every-body rule and environment-driven proxying. Refuse Node 24's unlimited per-origin pools and undici's 300-second header and body timers.
solid answer
~40 sI would pick one client per service, usually `fetch()` with an undici `Agent` per upstream, so pooling, proxying and timeouts are configured in one place instead of split between undici's dispatcher and `http.Agent`. Each upstream gets an explicit connection cap, because both stacks default to unlimited sockets per origin, and the payment API is the dependency we least want to flood. Every call carries its own `AbortSignal.timeout()`, and the dispatcher lowers undici's 300-second `headersTimeout` and `bodyTimeout` as a backstop. A shared wrapper settles every response body. Proxying comes from `NODE_USE_ENV_PROXY` in deployment config with `NO_PROXY` for internal hosts. The tradeoffs are real: caps add queueing latency, the npm `undici` dependency can drift from `process.versions.undici`, and a wrapper is code the team now owns.
go deeper
Recall that Node's default clients have no per-origin connection cap and that every outbound call should carry its own timeout signal.
Explain which defaults differ between fetch() and http.request(), and why a team picks one stack per service.
Show which incidents each rule prevents: socket floods, multi-minute stalls, leaked connections and proxy bypasses.
Own the tradeoffs: cap size against queueing, a shared wrapper against a lint rule, and the npm undici dependency against bundled-only.
## The decision a lead owns Node 24 gives every service working HTTP clients with no configuration, and those defaults are reasonable for a script. A fleet of services calling a slow payment API and a busy internal inventory API needs a **team standard**, because each default fails in a specific way at volume and each service rediscovering it is expensive. There is no single right standard; what follows is one defensible position and what it costs. ## Built-in defaults worth refusing | Built-in default | Why it hurts at volume | |---|---| | undici `Pool` `connections: null` (unlimited per origin) | a slow upstream turns into thousands of sockets | | `http.Agent` `maxSockets: Infinity` | the same, on the `node:http` stack | | undici `headersTimeout` / `bodyTimeout` of 300 s | a stalled call holds a request slot for minutes | | `http.request()` `timeout` only emits `'timeout'` | code believes it has a timeout and has none | | proxy variables ignored | egress-restricted environments fail in confusing ways | | unread bodies cleaned up only by garbage collection | connection leaks that grow with heap size | ## A standard worth proposing 1. **One client stack per service.** Choose `fetch()` (portable, promise-based, undici underneath) or `http.request()` (socket-level control), not both. Mixing them means two pools, two proxy paths and two sets of timers. If the team picks `fetch()`, it accepts a dependency on the npm `undici` package to create dispatchers, and pins it close to the version Node bundles. 2. **A dispatcher per upstream with an explicit cap.** For example an undici `Agent` with `connections` sized to what the payment API's owners will accept, passed in `dispatcher`. The cap turns overload into local queueing, which a deadline then bounds. 3. **A deadline on every call.** `signal: AbortSignal.timeout(ms)` sized per dependency, created per call, covering the body read. On the dispatcher, lower `headersTimeout` and `bodyTimeout` well below 300 seconds as a backstop for code paths that forget. 4. **Body discipline.** The shared wrapper reads, drains or cancels every body on every branch. This is the cheapest rule with the largest payoff. 5. **Keep-alive kept on, with idle limits shorter than the upstream's.** undici's `keepAliveTimeout` (4 s, adjusted by server hints) and `http.Agent`'s `agentKeepAliveTimeoutBuffer` already lean that way; the standard is not to disable them with a careless custom agent. 6. **Proxying from the environment.** `NODE_USE_ENV_PROXY=1` plus `NO_PROXY` for internal hosts, set in deployment configuration rather than code, so a service moved between networks needs no change. 7. **Closing pools on shutdown.** Wire the dispatcher's `close()` or `agent.destroy()` into the service's shutdown path; draining itself is part of the shutdown standard. ## Where the judgment is - **Cap size versus latency.** A tight cap protects the payment API but queues callers; too loose and the cap is decorative. Many teams start from measured peak concurrency per upstream and revisit after incidents. - **Wrapper versus raw API.** A shared helper enforces deadlines and body handling, but it is code to version, document and keep in step with Node upgrades. Some teams prefer a lint rule plus review checklists. - **Bundled versus npm undici.** Relying only on the bundled client means no dependency but no dispatcher tuning beyond the documented hooks; the npm package unlocks pools and interceptors but can diverge from the version behind the global `fetch()`. - **Per-service exceptions.** A streaming export may legitimately need long body timers; the standard should say how exceptions are declared, not forbid them. - **What stays out of the client layer.** Retry and backoff policy, deadline budgets across a call chain and idempotency keys are resilience decisions made with the downstream owners; the client standard only makes them possible. ## How to tell it is working - Socket and upstream connection counts track traffic and fall back when it drops. - Timeouts surface as named `TimeoutError` failures within the configured budget, not as five-minute stalls. - A Node upgrade review includes reading the `added:` and `changes:` history for agents, fetch and proxy support, because these defaults have moved between releases.
- Why not keep the unlimited per-origin default and rely on the upstream to push back?Because the push-back arrives as failures after the damage: thousands of sockets, handshakes and queued work on a slow payment API. An explicit cap keeps the overload on your side as bounded queueing that a per-call deadline can cut off, and it makes the service's maximum load on that dependency a number someone chose.
- What is the cost of standardising on fetch() with dispatchers from the npm undici package?A runtime dependency whose version can differ from `process.versions.undici`, the client behind the global `fetch()`. The team must pin and upgrade it deliberately, keep `fetch` and `FormData` from the same implementation, and check both when a Node upgrade changes the bundled client.
- Node's built-in client defaults are already production-safe for every service.
- An http.request timeout option is enough to bound every outbound call.
- A connection cap alone protects the upstream, so no per-call deadline is needed.
- Setting http.globalAgent once configures both fetch and http.request.
- Unread bodies are a style issue, not a capacity issue.
Assumes Node 24 with its bundled undici 7.x: unlimited per-origin pools, 300-second headersTimeout and bodyTimeout, and opt-in environment proxying (NODE_USE_ENV_PROXY since v24.0.0, node:http support since v24.5.0).