TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Enable HTTPS keepAlive in Node.js to increase request speed by up to 70%

3 pointsby gajusalmost 3 years ago
No clue how I am learning this only now, but better later than never: HTTPS `keepAlive` is not enabled by default in Node.js, and it has severe performance implications to network-intense applications.<p>Just to illustrate the impact, assuming that your server is hosted in `us-central1` and it is talking to a service in `us-east1`, just the network latency is ~20ms. Since a TCP handshake is a 3-packet event, this means that ~60ms are going to be allocated just to establish TLS handshake.<p>You can test that with a simple script:<p><pre><code> const got = require(&#x27;got&#x27;); const main = async () =&gt; { const response0 = await got(&#x27;https:&#x2F;&#x2F;posthog.com&#x2F;&#x27;); console.log(response0.timings.phases); const response1 = await got(&#x27;https:&#x2F;&#x2F;posthog.com&#x2F;&#x27;); console.log(response1.timings.phases); }; main(); </code></pre> In this scenario, the above will produce:<p><pre><code> { wait: 1, dns: 20, tcp: 72, tls: 74, request: 0, firstByte: 79, download: 222, total: 468 } { wait: 0, dns: 1, tcp: 67, tls: 69, request: 1, firstByte: 73, download: 234, total: 445 } </code></pre> However, watch the `total` time if we enable `keepAlive`:<p><pre><code> const got = require(&#x27;got&#x27;); const https = require(&#x27;https&#x27;); https.globalAgent = new https.Agent({ keepAlive:true }); const main = async () =&gt; { const response0 = await got(&#x27;https:&#x2F;&#x2F;posthog.com&#x2F;&#x27;); console.log(response0.timings.phases); const response1 = await got(&#x27;https:&#x2F;&#x2F;posthog.com&#x2F;&#x27;); console.log(response1.timings.phases); }; main(); { wait: 1, dns: 27, tcp: 77, tls: 75, request: 0, firstByte: 75, download: 220, total: 475 } { wait: 0, dns: 0, tcp: 0, tls: 0, request: 0, firstByte: 77, download: 83, total: 160 } </code></pre> The second request is 70% faster than the first request!<p>If your application relies on making many HTTPS calls, then simply enabling `keepAlive` is going to result in a significant performance improvement.

no comments

no comments