powertools-lambda-typescript icon indicating copy to clipboard operation
powertools-lambda-typescript copied to clipboard

Feature request: Improve Lambda Layer Performance

Open perpil opened this issue 2 years ago • 5 comments

Use case

I've been doing some benchmarking of using the lambda layer vs bundling the SDK using esbuild. There appears to be a noticeable performance penalty in using the lambda layer on coldstarts. I'm using dynamic import and importing Tracer, Logger, Metrics and initializing each of them in my initialization. I'm also making calls to secrets manager in initialization so I've timed them and removed their impact from the normalized init duration. I'm seeing a repeatable impact of 100+ ms when using the layer.

init duration secrets loading normalized lambda layer delta notes
849 121 728 none +0 ms This is with bundling using esbuild
1089 167 922 18 +194 ms This is Lambda layer 18
994 112 882 20 +154 ms This is Lambda layer 20

It's possible I've overlooked something, and I'll admit, my sample size is low but repeatable. This has more on the esbuild flags I'm using: https://speedrun.nobackspacecrew.com/blog/2023/09/23/optimizing-lambda-coldstarts.html

Solution/User Experience

If possible, conduct a similar benchmark and explore using esbuild or other techniques that could reduce the impact of using the layer. If there remains a performance penalty vs. bundling the SDK, document it.

Alternative solutions

No response

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

perpil avatar Sep 29 '23 01:09 perpil

Hey @perpil

thanks a lot for taking your time and looking into the performance of the layer 🙏.

This is something hat has been on our radar for a while and we will investigate it very soon. We are confident that using layer will not improve the performance, but it shouldn't also be a impactful increase. To get the best performance, it's best to bundle and tree shake into a single file.

The layer you tested has only tracer, metrics and logger. In our next release we will include all utilities AND selected aws-sdk clients, you can check this comment in #1712 . This will simplify the layer usage and prevent imports resolution issues, especially for node16 runtime. While we focused to keep the layer size small, we did not run any performance tests.

I am curious to see how the new layer version will perform. Is there a chance for you share the setup how you ran the experiments, so we can better understand the results. I am no expert on performance testing so this will also help me to learn a bit more about that.

Thank you.

am29d avatar Sep 29 '23 05:09 am29d

Is there a chance for you share the setup how you ran the experiments, so we can better understand the results. I am no expert on performance testing so this will also help me to learn a bit more about that.

I should be able to create a minimal repo you can use to experiment. It may take some time to get to it, but I'll send a link when I've built it.

perpil avatar Sep 29 '23 19:09 perpil

I noticed layer 21 was released today so I've added it to the table.

init duration secrets loading normalized lambda layer delta notes
849 121 728 none +0 ms This is with bundling using esbuild
1089 167 922 18 +194 ms This is Lambda layer 18
994 112 882 20 +154 ms This is Lambda layer 20
1053 159 894 21 +166 ms This is Lambda layer 21

perpil avatar Sep 29 '23 20:09 perpil

Here's a quick repo that you can comment on and off layers and see the results in x-ray/cloudwatch. It uses serverless, but I'm not doing anything that can't be done with the CDK. Instructions here.

perpil avatar Oct 03 '23 01:10 perpil

Thank you @perpil, we'll take a look and go from there.

dreamorosi avatar Oct 03 '23 07:10 dreamorosi