question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lambda Cold Start Time

See original GitHub issue

Bug description

I want to use Prisma in my AWS serverless lambda functions. I’m using Serverless Stack and NodejsFunction for bundling (which uses esbuild).

I enable the minify option when bundling.

My problem: 7.5MB of generated prisma client code is included in my application. I already have a prisma lambda layer containing the engines, my issue is with the massive amount of minified JS code included in my lambda bundle. This is an issue because it makes my lambda functions start in about 2.5 seconds compared to about 600ms for my functions that don’t have the prisma client bundled:

No prisma:

image

With prisma:

image

Here is what a typical lambda bundle with prisma looks like: image

I’m continuing to gather data to try to work out what fraction of my cold start time is prisma. See below comments for more details on that.

How to reproduce

Create a NodejsFunction with AWS CDK that imports a generated prisma client.

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-nodejs-readme.html

Expected behavior

Since I’m using esbuild (and a lot of next-gen JS tooling is starting to use it) it would be helpful if the generated client used ES modules or at least ES6 import syntax. CommonJS is death for bundling.

I don’t know why the prisma clients are 7.5MB of minified JS. That seems highly strange. My schema file is about 800 lines. My dmmfString is 6,492,708 characters long it seems.

Screen Shot 2021-12-16 at 14 01 59

☝🏻 CommonJS.

https://esbuild.github.io/api/#tree-shaking

This way esbuild will only bundle the parts of your libraries that you actually use, which can sometimes be a substantial size savings. Note that esbuild’s tree shaking implementation relies on the use of ECMAScript module import and export statements. It does not work with CommonJS modules. Many libraries on npm include both formats and esbuild tries to pick the format that works with tree shaking by default.

Prisma information

3.6.0

Environment & setup

  • OS: linux- lambda
  • Database: postgres, aurora serverless
  • Node.js version: 14

Prisma Version

prisma                  : 3.6.0
@prisma/client          : 3.6.0
Current platform        : darwin
Query Engine (Node-API) : libquery-engine dc520b92b1ebb2d28dc3161f9f82e875bd35d727 (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli dc520b92b1ebb2d28dc3161f9f82e875bd35d727 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core dc520b92b1ebb2d28dc3161f9f82e875bd35d727 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt dc520b92b1ebb2d28dc3161f9f82e875bd35d727 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash    : dc520b92b1ebb2d28dc3161f9f82e875bd35d727
Studio                  : 0.440.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:29 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
revmischacommented, Dec 27, 2021

Continuing to profile prisma during nodejs startup. According to this profile, 62% of my ~1.5s startup time is initializing the prisma library, with significant time spent on “findSync”

image

Screen Shot 2021-12-27 at 2 48 47 PM Screen Shot 2021-12-27 at 2 50 22 PM
1reaction
renchapcommented, Mar 3, 2022

Can you split that information out into its own (cross-linked) issue @renchap? That would indeed be super useful for us. Thanks!

Here it is: https://github.com/prisma/prisma/issues/12132

Read more comments on GitHub >

github_iconTop Results From Across the Web

Operating Lambda: Performance optimization – Part 1
The duration of a cold start varies from under 100 ms to over 1 second. Since the Lambda service reuses warmed environments for...
Read more >
Cold Starts in AWS Lambda | Mikhail Shilkov
Cold starts happen 5 to 7 minutes after the previous request. Read more: When Does Cold Start Happen on AWS Lambda? How Slow...
Read more >
AWS makes Lambda cold start latency a thing of the past with ...
With its Firecracker microVMs, AWS already improved cold start times from multiple seconds to well under a second. Now, the company promises a ......
Read more >
AWS Lambda Cold Starts: Solving the Problem - Lumigo
Cold starts in Lambda account for less than 0.25% of requests but the impact can be huge, sometimes requiring 5 seconds to execute...
Read more >
AWS Lambda Function Execution and Cold Start - YouTube
In this video, we learn about the anatomy of a Lambda Function execution and how it can create a phenomenon called " cold...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found