Reusing HTTP connections with 'keepalive' in Serverless
Blog post from Upstash
Keepalive is a technique that reduces latency by maintaining an open connection between a client and a server, which is particularly beneficial when making multiple requests to the same server in quick succession. Although serverless functions like AWS Lambda and Vercel are stateless, they can temporarily hold state when a "hot" function is reused, allowing data or connections to persist between requests. By placing variables or objects like an HTTPS agent outside the handler function, subsequent invocations can reuse these resources, enhancing performance. Testing on a Next.js app with Vercel and an Upstash Redis database showed that enabling keepalive and caching connections significantly reduced latency, with initial requests taking longer due to connection establishment but subsequent requests achieving much lower latency. This optimization is realized in the @upstash/redis SDK, enabling keepalive by default to improve performance across multiple calls without additional configuration.