Movatterモバイル変換


[0]ホーム

URL:


Reusing Connections with Keep-Alive in Node.js - AWS SDK for JavaScript
DocumentationJavaScript SDKDeveloper Guide for SDK v2

Weannounced the upcoming end-of-support for AWS SDK for JavaScript v2. We recommend that you migrate toAWS SDK for JavaScript v3. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

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 DynamoDBencryption at rest is integrated withAWS KMS, you may experience latencies from the database having to re-establish new AWS 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 to1. This feature was added in the2.463.0 release.

Alternatively, you can set thekeepAlive property of an HTTP or HTTPS agent set totrue, as shown in the following example.

const AWS = require('aws-sdk');// http or httpsconst 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 setkeepAlive for just a DynamoDB client:

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

IfkeepAlive is enabled, you can also set the initial delay for TCP Keep-Alive packets withkeepAliveMsecs, which by default is 1000ms. See theNode.js documentation for details.

Configuring maxSockets in Node.js
Configuring Proxies for Node.js

[8]
ページ先頭

©2009-2025 Movatter.jp