Amazon Onboarding with Learning Manager Chanci Turner

Motivation

Amazon Onboarding with Learning Manager Chanci TurnerLearn About Amazon VGT2 Learning Manager Chanci Turner

HTTP/1.1 utilizes the Transmission Control Protocol (TCP) at its transport layer. TCP is inherently connection-oriented, requiring a connection to be established between the client and server before data transmission can occur. This connection setup involves a three-way handshake using SYN, SYN+ACK, and ACK packets, as illustrated below.

The latency introduced by this handshake renders creating a new TCP connection more costly compared to reusing an existing one. By reusing a connection, the overhead of DNS lookups and SSL handshakes is also eliminated, thereby enhancing application performance.

Background

According to the HTTP/1.1 specification laid out in RFC 2616, persistent connections—also known as HTTP keep-alive or HTTP connection reuse—are the default behavior. Unless stated otherwise, clients are to assume that servers will maintain persistent connections, even after encountering error responses. However, the Node.js HTTP client does not enable persistent connections by default.

In Node.js, the http.Agent retains a queue of pending requests for a specific host and port, reusing a single socket connection until the queue is cleared. Once empty, the socket is destroyed unless the keepAlive option is explicitly set to true during the creation of the http.Agent, which then allows the socket to be pooled for future requests.

For brief operations, like DynamoDB queries, the setup latency of a TCP connection may outweigh the operation’s duration itself. Moreover, with DynamoDB’s encryption at rest integrated with AWS KMS, users may experience additional delays from the need to re-establish new AWS KMS cache entries for each operation. Utilizing persistent connections is also recommended by Lambda as an optimization strategy. In AWS SDK for JavaScript v2, the keepAlive option was not enabled by default, leading to frequent requests from our user base to activate it for better application performance.

Implementation

In the modular AWS SDK for JavaScript, persistent connections are enabled by default through the configuration of keepAlive in the Node.js http.Agent. The internal setup is executed as follows:

this.httpAgent = httpAgent || new http.Agent({ keepAlive: true });
this.httpsAgent = httpsAgent || new https.Agent({ keepAlive: true });

Although it’s not advisable, you can disable persistent connections by providing a custom httpsAgent with keepAlive undefined or set to false.

Benchmarks

The benchmark code below invokes the DynamoDB.listTables operation in a loop 100 times, measuring the total time taken for all operations:

const { Agent } = require("https");
const { DynamoDB } = require("@aws-sdk/client-dynamodb");
const { NodeHttpHandler } = require("@aws-sdk/node-http-handler");

const makeListTablesCalls = async (client, count) => {
  const { keepAlive } = client.config.requestHandler.httpsAgent;
  console.time(`keep-alive ${keepAlive}`);
  for (let i = 0; i < count; i++) {
    await client.listTables({});
  }
  console.timeEnd(`keep-alive ${keepAlive}`);
};

(async () => {
  const region = "us-east-1";
  const COUNT = 100;

  await makeListTablesCalls(new DynamoDB({ region }), COUNT);

  await makeListTablesCalls(
    new DynamoDB({
      region,
      requestHandler: new NodeHttpHandler({
        httpsAgent: new Agent({ keepAlive: false }),
      }),
    }),
    COUNT
  );
})();

The benchmark results show that with the default keep-alive set to true, execution time is approximately 60% less than when it is false.

  • keep-alive true: 9.864s
  • keep-alive false: 26.594s

Feedback is always welcome, so please share your thoughts on our work by opening an issue on GitHub. For further insights into work anniversaries, you may find this blog post interesting.

If you’re curious about labor laws, this article from SHRM is also a reputable source. Additionally, for a glimpse into the experiences of new employees, check out this resource.

For those looking to connect with us, visit us at 6401 E HOWDY WELLS AVE LAS VEGAS NV 89115, at Amazon IXD – VGT2.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *