Reusing Connections with Keep-Alive in Node.js - Amazon SDK for JavaScript
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

The Amazon SDK for JavaScript version 3 (v3) is a rewrite of v2 with some great new features, including modular architecture. For more information, see the Amazon SDK for JavaScript v3 Developer Guide.

Reusing Connections with Keep-Alive in Node.js

By default, the default Node.js HTTP/HTTPS agent creates a new TCP connection for every new request. To avoid the cost of establishing a new connection, you can reuse an existing connection.

For short-lived operations, such as DynamoDB queries, the latency overhead of setting up a TCP connection might be greater than the operation itself. Additionally, since DynamoDB encryption at rest is integrated with Amazon KMS, you may experience latencies from the database having to re-establish new Amazon KMS cache entries for each operation.

The easiest way to configure SDK for JavaScript to reuse TCP connections is to set the AWS_NODEJS_CONNECTION_REUSE_ENABLED environment variable to 1. This feature was added in the 2.463.0 release.

Alternatively, you can set the keepAlive property of an HTTP or HTTPS agent set to true, as shown in the following example.

const AWS = require('aws-sdk'); // http or https const http = require('http'); const agent = new http.Agent({ keepAlive: true, // Infinity is read as 50 sockets maxSockets: Infinity }); AWS.config.update({ httpOptions: { agent } });

The following example shows how to set keepAlive for just a DynamoDB client:

const AWS = require('aws-sdk') // http or https const https = require('https'); const agent = new https.Agent({ keepAlive: true }); const dynamodb = new AWS.DynamoDB({ httpOptions: { agent } });

If keepAlive is enabled, you can also set the initial delay for TCP Keep-Alive packets with keepAliveMsecs, which by default is 1000ms. See the Node.js documentation for details.