Node.js › HTTP & Web Servers
What is HTTP keep-alive, and how does it improve performance for a Node.js server handling many requests from the same client?
IntermediateAnswer
Keep-alive allows a single underlying TCP connection to be reused for multiple sequential HTTP requests/responses, rather than opening and closing a brand new TCP connection (with its own handshake overhead) for every single request -- Node.js's http module (and the underlying http.Agent it uses) supports keep-alive by default in modern versions, meaningfully reducing latency and resource usage for clients making many requests to the same server.
const http = require('node:http');
const agent = new http.Agent({ keepAlive: true, maxSockets: 50 });
http.get('http://api.example.com/data', { agent }, (res) => { /* reuses an existing connection if available */ });Real-world example An internal service making hundreds of requests per second to another internal API configures an http.Agent with keepAlive enabled and a higher maxSockets limit, avoiding the significant overhead of establishing a brand new TCP connection for every single one of those frequent requests.