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

Reduce Lambda function cold start from 11 seconds to less than 2 seconds #26

Merged
merged 8 commits into from
Mar 22, 2022

Conversation

dmahapatro
Copy link
Member

Description of changes:

Existing implementation of Lambda functions had below problems:

  • Used Java SDK v1 which is less performant than v2
  • Targeted for JDK 8 Lambda runtime which is old
  • Had a cold start of more than 11 seconds

With this PR below items are taken care of:

  • Upgrade Gradle to 7.1 and use Gradle wrapper
  • Upgrade to use AWS Java SDK v2
  • Upgrade app to use DynamoDbBean and DynamoDbEnhancedAsyncClient
  • Use AWS CRT http client for better performance
  • Upgrade dependencies to latest as of 3/19/2022
  • Upgrade to use Java 11 Corretto instead of Java 1.8
  • Added .editorconfig for consistent formatting
  • Removed unused classes and code
  • Implement Java Lambda best practices

Result:
Lambda function cold start reduced from 11 seconds to below 2 seconds (~1.7s for GetUnicornBasket API with multiple unicorns)

Configuration

  • Runtime: Java 11
  • Memory: 512MB
  • Architecture: arm64

How to create Lambda artifact?
Use below command to create the artifact MonoToMicroLambda-0.0.1.jar:

./gradlew clean shadowJar

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@deki
Copy link
Collaborator

deki commented Mar 19, 2022

Nice work, this was on the backlog for a long time :-)

We need to adapt the instructions https://catalog.us-east-1.prod.workshops.aws/workshops/f2c0706c-7192-495f-853c-fd3341db265a/en-US/microservices and can merge it afterwards.

@msailes
Copy link

msailes commented Mar 20, 2022

Can I suggest we add the tiered compilation stop at level 1 config as well.

Example:
https://github.com/aws-samples/aws-lambda-java-tiered-compilation-example/blob/46cb440fe32b285b2abd8ad77c5fac10d458b1f3/infrastructure/src/main/java/example/InfrastructureStack.java#L70

@dmahapatro
Copy link
Member Author

The workshop uses manual creation of a Lambda function via AWS Console. Most likely this has to be a step in the workshop guide.

@dmahapatro
Copy link
Member Author

Tested adding tiered compilation stop at level 1 config. Init duration (cold start) reduced from 1769ms to 1560ms @msailes

@deki deki self-assigned this Mar 21, 2022
@@ -367,7 +367,7 @@
"configure" : {
"commands" : {
"0-clone-repo" : { "command" :
"git clone https://github.com/aws-samples/unishop-monolith-to-microservices.git /home/ec2-user/MonoToMicro"
"git clone https://github.com/dmahapatro/unishop-monolith-to-microservices.git /home/ec2-user/MonoToMicro"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deki Assume this is just for testing purposes?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will fix it before merging

@deki deki merged commit 087084f into aws-samples:master Mar 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants