intermediate
Keep-alive and connection reuse
Explain persistent connections, agent pools, idle timeouts, socket exhaustion, upstream resets, and why reuse affects latency.
Persistent HTTP connections amortize TCP and TLS handshakes across multiple requests. Node's `http.Agent` pools sockets with `keepAlive: true` by default in modern versions — critical for services that call many upstream APIs.
import { Agent, request } from 'node:https';
const agent = new Agent({ keepAlive: true, maxSockets: 50, timeout: 30_000 });
request('https://api.example.com/data', { agent }, (res) => { /* ... */ });
Idle timeouts on load balancers, NAT gateways, and upstream servers can close connections silently — the next request may fail and retry on a fresh socket. Socket exhaustion happens when `maxSockets` is too low or connections leak.
On interviews: explain why connection reuse improves P99 latency; align idle timeouts client < LB < server; diagnose `ECONNRESET` after idle periods.
Common pitfalls: creating a new Agent per request; mismatched keep-alive between proxy and app; not handling half-open connections during deploy.
The trade-off is balancing simplicity, performance, safety, and operability — name which axis you optimized and what cost you accepted.
Checklist:
- Enable pooled agents for outbound HTTP.
- Tune maxSockets to concurrency needs.
- Align timeouts across hops.
- Monitor open socket counts under load.