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
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
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