Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Connection: keep-alive to improve application performance #2571

Closed
milancermak opened this issue Mar 8, 2019 · 3 comments
Closed

Use Connection: keep-alive to improve application performance #2571

milancermak opened this issue Mar 8, 2019 · 3 comments
Assignees
Labels
closed-for-staleness feature-request A feature should be added or improved. needs-major-version Can only be considered for the next major release

Comments

@milancermak
Copy link

By default, NodeJS sets Connection: close and not Connection: keep-alive HTTP header which results in sub-optimal performance. Instead of reusing an already established connection between the client and the service, a new one has to be created for every operation.

It has been reported before (#146), recently publicized by an AWS Serverless Hero and it's also recommended by AWS for DynamoDB when using encryption at rest. DynamoDB itself responds with a Connection: keep-alive header.

Even though all these examples pertain to DynamoDB, I imagine other services will benefit from this change as well. For what it's worth, boto3, the official AWS SDK for Python does not explicitly set a Connection: close header.

Please consider setting keep-alive header the on the SDK level so that going forward, the whole community can benefit from this improvement.

@srchase srchase added the feature-request A feature should be added or improved. label Mar 8, 2019
@srchase srchase added the needs-major-version Can only be considered for the next major release label Mar 19, 2019
@srchase
Copy link
Contributor

srchase commented Mar 19, 2019

Changing the default behavior here could introduce issues for existing users of the SDK. If this were enabled by default, it would require a major version bump.

It is possible to enable keep-alive through configuration.

That can be done on the global config:

const AWS = require('aws-sdk')
const https = require('https');
const agent = new https.Agent({
  keepAlive: true
})

AWS.config.update({
  httpOptions: {
    agent: agent
  }
})

Or on a per-client basis:

const AWS = require('aws-sdk')
const https = require('https');
const agent = new https.Agent({
  keepAlive: true
});

const DDB = new AWS.DynamoDB({
  httpOptions: {
    agent: agent
  }
});

@github-actions
Copy link

Greetings! We’re closing this issue because it has been open a long time and hasn’t been updated in a while and may not be getting the attention it deserves. We encourage you to check if this is still an issue in the latest release and if you find that this is still a problem, please feel free to comment or open a new issue.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Apr 29, 2021
@github-actions github-actions bot closed this as completed May 4, 2021
bilalq added a commit to bilalq/aws-lambda-runtimes-performance that referenced this issue Oct 13, 2021
The AWS SDK variant that is available by default on Lambda runtime's is
v2. This version creates and closes a new TCP connection for each
network request. For backwards compatibility reasons, they didn't change
that default behavior in v2[0], but they did in v3[1].

However, this can be manually set in by passing in a configured http
Agent to the AWS SDK with `keepAlive` set to `true` or by simply setting
the `AWS_NODEJS_CONNECTION_REUSE_ENABLED` environment variable[2].

While this shouldn't really impact cold starts much, the performance
difference when measuring warm starts can be pretty significant. This is
a relevant excerpt from the AWS documentation:

> 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 AWS KMS, you may experience latencies from the
> database having to re-establish new AWS KMS cache entries for each
> operation.

  [0]: aws/aws-sdk-js#2571
  [1]: https://aws.amazon.com/blogs/developer/http-keep-alive-is-on-by-default-in-modular-aws-sdk-for-javascript/
  [2]: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-reusing-connections.html
@Aleksandr-Filichkin
Copy link

Aleksandr-Filichkin commented Oct 19, 2021

Just to understand the difference
Aleksandr-Filichkin/aws-lambda-runtimes-performance#6 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-for-staleness feature-request A feature should be added or improved. needs-major-version Can only be considered for the next major release
Projects
None yet
Development

No branches or pull requests

4 participants