The keepAliveTimeout
in a NestJS application (or any Node.js application) is a crucial configuration setting that controls how long an idle connection is kept open before it is closed. Understanding keepAliveTimeout
and its implications can help you configure your server for better performance and stability, especially when dealing with long-running requests or integrations with load balancers.
- Definition: The
keepAliveTimeout
is a setting on the HTTP server that specifies how long to keep an idle connection open before closing it. - Idle Connection: An idle connection is one where no data is being transferred between the client and the server after the initial request has been fulfilled.
-
Persistent Connections: HTTP/1.1 introduced the concept of persistent connections (or keep-alive connections), where a single TCP connection can be reused for multiple HTTP requests/responses between a client and server. This reduces the overhead of establishing new connections for every request.
-
Timeout Period: The
keepAliveTimeout
determines how long the server will keep the connection alive after the last request is completed, waiting for a new request from the same client.For example, if you set
keepAliveTimeout
to 5000 milliseconds (5 seconds), the server will wait for 5 seconds after a response has been sent before closing the connection if no new request comes in.
- The default
keepAliveTimeout
in Node.js is 5 seconds (5000 milliseconds). - This means that after fulfilling a request, the server will keep the connection open for 5 seconds waiting for another request from the same client.
There are several scenarios where you might want to adjust the keepAliveTimeout
:
- ALB Timeout Mismatch: Many cloud-based load balancers, such as AWS Application Load Balancer (ALB), have their own idle timeout settings (e.g., 60 seconds for ALB). If the server’s
keepAliveTimeout
is shorter than the load balancer's idle timeout, it can lead to premature connection termination, causing 502 Bad Gateway errors. - Solution: Set the
keepAliveTimeout
slightly higher than the load balancer's idle timeout (e.g., 65 seconds if the ALB timeout is 60 seconds). This ensures the server doesn’t close the connection before the load balancer does, reducing the risk of errors.
- Long-Lived Connections: In applications where requests can take a long time to process (e.g., large file uploads, streaming data, or document generation), having a short
keepAliveTimeout
might cause the server to close connections prematurely, disrupting the request. - Solution: Increase the
keepAliveTimeout
to accommodate the duration of typical long-running tasks.
- Connection Reuse: For applications experiencing high traffic with many small, quick requests, you might want to keep connections alive longer to reduce the overhead of establishing new connections for every request. This can improve throughput.
- Solution: Set a moderate
keepAliveTimeout
(e.g., 30 seconds) to balance resource usage and connection reuse.
While increasing the keepAliveTimeout
can help prevent issues like premature connection closure, it also has trade-offs:
- Resource Consumption: Keeping connections open longer increases resource usage (e.g., memory, file descriptors). On a busy server, this can lead to resource exhaustion if not managed properly.
- Scalability Concerns: If your application handles a very high number of concurrent connections, setting a high
keepAliveTimeout
might limit the server's ability to handle new incoming connections.
In a NestJS application, you typically configure the keepAliveTimeout
in the main.ts
file where the server is started:
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const config = app.get(ConfigService);
const server = await app.listen(config.get('port'));
// Set keepAliveTimeout to 65 seconds (65000 milliseconds)
server.keepAliveTimeout = 65000;
// Ensure headersTimeout is greater than keepAliveTimeout
server.headersTimeout = 66000;
console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();
keepAliveTimeout
Setting: This sets the keep-alive timeout to 65 seconds.headersTimeout
: It is essential to ensure that theheadersTimeout
(which specifies the time the server waits to receive the complete request headers) is greater thankeepAliveTimeout
. Otherwise, the connection may close before the client has a chance to send the request.
- Match or Exceed Load Balancer Timeouts: Always set
keepAliveTimeout
slightly higher than your load balancer’s idle timeout. - Monitor Resource Usage: Use monitoring tools to observe the impact of your
keepAliveTimeout
settings on server performance. - Test Under Load: Ensure that your server behaves as expected under different load conditions with your configured
keepAliveTimeout
.
keepAliveTimeout
is a critical setting for optimizing the performance and reliability of a NestJS server, particularly in scenarios involving load balancers or long-running requests. By carefully configuring this timeout, you can reduce the likelihood of connection-related errors and improve the efficiency of your server in handling client requests. However, it's essential to balance this with the server's resource constraints to maintain scalability and performance.