aws-sdk-js-v3: Coldstart Performance/Bundle Size Regression in 3.577.0

Checkboxes for prior research

Describe the bug

I’m seeing a larger than normal regression in coldstart performance and treeshaken bundle size after 3.575. This chart shows cold start latency and esbuild built bundle size by sdk. It’s a small sample size (3 runs), but I’m seeing +24 ms in coldstart and +146K in bundle size.


aws sdk node version avg(@initDuration) bundleSize # runs
3.575.0 v20.13.1 246.79 316588 3
3.577.0 v20.13.1 270.0433 463023 3

SDK version number

@aws-sdk/client-dynamodb@3.577.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v20.13.1

Reproduction Steps

Read the README in this Repro

Observed Behavior

Increased coldstart times and bundle size.

Expected Behavior

Minimal impact to coldstart and bundle size on tree-shaken code.

Possible Solution

Looking at the bundle analyzer, the biggest size impact are from the @smithy/* packages, but it also seems like fast-xml-parser appeared too, I haven’t dug much beyond that. 3 575-repro 3 577-repro

Additional Information/Context

In my app that I upgraded, I saw higher impacts to coldstart performance (>90ms) between 3.563 and 3.577. I was using more clients however: client-sts, client-dynamodb, client-iam, and lib-dynamodb. To keep things simple, I did the repro with just client-dynamodb.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 12
  • Comments: 27 (20 by maintainers)

Most upvoted comments

I’ve narrowed it down to the exact line that got added in 3.575.

If I patch that line from:

export const keepAliveSupport = {
  supported: Boolean(typeof Request !== "undefined" && "keepalive" in new Request("https://[::1]")),
};

To:

export const keepAliveSupport = {
  supported: true,
};

Then the 50 ms latency hit disappears in 3.575. Wild guess: maybe DNS is getting involved and there is a 50 ms timeout?

@kuhe I can confirm SDK 3.598 with @smithy/fetch-http-handler 3.0.3 no longer has the coldstart issue. The Request code is treeshaken from my bundle now that it isn’t in the module scope. Thanks so much! 🚀

Turns out our problem was package-lock.json containing duplicate versions of the AWS SDK packages. Regenerating our package-lock.json fixed our issue.

Credit to @divporter for teaching me about the npm dedupe command.

@perpil sorry for the misunderstanding I can now reproduce the issue. Great finding! The reason for this regression is that when Request class is being used, it’s actually lazily loaded and pulls in a lot of dependencies (undicii etc) that adds this 40-50ms cold starts. @kuhe reproducible can be done by simply doing:

const a = Request;

export const handler = async (event) => {
    const response = {
        statusCode: 200,
        body: JSON.stringify("slow"),
    };
    return response;
};

Comment out first line and we’re back to normal. Request further pulls in more lazily evaluated classes such as URL/Stream etc when instantiated.

Source: https://github.com/nodejs/node/blob/f2381bc7cb120e43bb97ffeccd19dfdc946c8b2c/lib/internal/bootstrap/web/exposed-window-or-worker.js#L86 (basically a property that does require)

@richarddd the smithy dependencies were upgraded in 3.575 so this new code got pulled in. When you bundle using the repro further up in the thread you’ll see these lines in the generated index.mjs output which weren’t in 3.574:

// node_modules/@smithy/fetch-http-handler/dist-es/fetch-http-handler.js
var keepAliveSupport = {
  supported: Boolean(typeof Request !== "undefined" && "keepalive" in new Request("https://[::1]"))
};

You’ll also note, there are no references to that variable in the generated output, so it isn’t necessary in the node environment. When I remove that code, or hardcode it to true, I no longer see the 50 ms hit to @initDuration (it never showed up in @duration because it is loaded outside of the handler). If you want to do an accurate repro with your code, you should move the code outside the handler, compare it to not using the Request at all, and look at the resulting @initDuration. As I understand, fetch-http-handler is only supposed to be used in browser environments and node-http-handler is meant to be used when running in Lambda.

@kuhe Thanks for looking into it, I think there are a couple ways of solving it so reach out if you need help. One thing you mentioned was to try and use performance.now() to wrap the code outside the handler to do the benchmark. The issue with this approach is when it is bundled, some of the code in index.mjs is actually executed before any of the imports so you miss some of the latency. In the resulting bundle you get something like this, so @initDuration is what you should be using for an accurate benchmark.

var keepAliveSupport = ...
var start = performance.now();
...other code
var end = performance.now()

Hey @dreamorosi, the negligible is between 3.575 and 3.592. However, I then compared 3.568 to 3.592 and saw the 50ms difference when I replicated what I saw in my production code. I agree that 50ms is a big regression and you should be able to replicate my results with the code I’ve currently checked into the repo. I looked at a few things to see if I could identify the source and I found some tiny things (shared-ini-loader and one other package I’m blanking on) but nothing that explains the 50 ms. The source of the latency no longer seems to be related to code size because I tried minification and didn’t see much difference.

50ms on these number is 25% slower, not sure I agree with the negligible part.

Would be great to understand what was the trade off for adding this and if there’s anything at all that we can do to reverse it besides pinning to 3.575.

When we publish the fix in #6149 you shouldn’t have to do anything different anymore. Taking the dist-es (ESM, tree-shakeable) code for bundling should avoid importing both tslib and fast-xml-parser.