Node.js HTTPS Agent keepAlive: Impact on Integration Test Performance Without Production Credentials
26.5K reputation · 25 Mar 2021, 11:40 UTC
Node.js HTTPS Agent keepAlive in Tests
When running integration tests that hit an HTTPS endpoint without using production credentials, the default keepAlive setting on the https.Agent may influence both the number of TLS handshakes and the overall test duration. The Node.js documentation states that keepAlive defaults to false, but the actual behavior can vary between LTS and current releases (e.g., Node.js 12 through 18).
Enabling keepAlive can reduce latency by reusing TCP sockets, yet it also risks concealing bugs that manifest only when each request establishes a fresh connection. In a test environment that does not replicate production credentials, it is unclear whether the performance gains outweigh the potential masking of connection‑related issues.
Key constraints include:
- Testing must be performed against the target Node.js version in production.
- Connection teardown logic may differ when
keepAliveis toggled. - Test results should be reproducible across multiple runs.
Unresolved questions:
- Does enabling
keepAliveconsistently reduce TLS handshakes across Node.js 12‑18 in integration tests? - Can
keepAlivemask connection teardown bugs that only appear when each request uses a new socket? - Should integration tests mimic the production
keepAlivesetting, or should they run withkeepAlivedisabled to surface hidden issues?
1 answer
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
26,525 reputation · 25 Mar 2021, 18:29 UTC
A practical way to quantify the handshake reduction is to instrument the test server itself: listen for the secureConnection event and increment a counter. With keepAlive enabled, that count should be far lower than the number of requests; with it disabled, it should equal the request count. This gives a direct, reproducible metric without needing production credentials.
Also be aware that a shared agent across parallel tests can exhaust maxSockets. Use a fresh agent per test or set maxSockets deliberately, and always call agent.destroy() in teardown to avoid lingering sockets that could cause cross-test interference.