Adopting Java for the
Serverless world
from the perspective of the AWS developer
Vadym Kazulkin, ip.labs, Serverless Singapore, 16 February 2021
Contact
Vadym Kazulkin
ip.labs Bonn, Germany
Co-Organizer: Java User Group Bonn and
Serverless Bonn Meetup
v.kazulkin@gmail.com
@VKazulkin
@ServerlessBonn (Meetup)
https://www.linkedin.com/in/vadymkazulkin/
https://www.iplabs.de/
ip.labs
https://www.iplabs.de/
Java popularity
https://www.cleveroad.com/blog/programming-languages-ranking Vadym Kazulkin @VKazulkin , ip.labs GmbH
AWS and Serverless
2020 Magic Quadrant for Cloud Infrastructure & Platform Services
https://pages.awscloud.com/GLOBAL-multi-DL-gartner-mq-cips-2020-learn.html?pg=LWIAWS
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Vadym Kazulkin @VKazulkin , ip.labs GmbH
“State of Serverless 2020 report” https://codingsans.com/blog/serverless-trends
2020 AWS Lambda Benchmark Report for Developers, DevOps, and Decision Makers
https://newrelic.com/resources/ebooks/serverless-benchmark-report-aws-lambda-2020
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Life of the Java Serverless developer
on AWS
AWS Java Versions Support
• Java 8
• With extended long-term support
• Java 11 (since 2019)
• Only Long Term Support (LTS) by AWS
• Next LTS Java version is Java 17 in 2021
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: https://aws.amazon.com/de/corretto/
Java ist very fast
and mature
programming
language…
Image: burst.shopify.com/photos/a-look-across-the-landscape-with-view-of-the-sea Christian Bannes and Vadym Kazulkin @VKazulkin , ip.labs GmbH
… but
Serverless
adoption of Java
looks like this
2020 AWS Lambda Benchmark Report for Developers, DevOps, and Decision Makers
https://newrelic.com/resources/ebooks/serverless-benchmark-report-aws-lambda-2020
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Developers love Java and will be happy
to use it for Serverless
But what are the challenges
Vadym Kazulkin @VKazulkin , ip.labs GmbH
AWS Lambda Basics
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Creating AWS Lambda with Java 1/3
:
Source https://blog.runscope.com/posts/how-to-write-your-first-aws-lambda-function
Creating AWS Lambda with Java 2/3
:
Source https://blog.runscope.com/posts/how-to-write-your-first-aws-lambda-function
Creating AWS Lambda with Java 3/3
:
Source https://docs.aws.amazon.com/lambda/latest/dg/java-context.html
AWS Lambda Price Model
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Cost for Lambda
REQUEST DURATION
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Request Tier
$ 0.20
Per 1 Mio Requests
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Duration Tier
$ 0.00001667
Per GB-Second
Christian Bannes and Vadym Kazulkin @VKazulkin , ip.labs GmbH
GB-Second
ONE SECOND ONE GB
Christian Bannes and Vadym Kazulkin @VKazulkin , ip.labs GmbH
Example
• 1 Mio requests
• Lambda with 512MiB
• Each lambda takes 200ms
0.5 GiB * 0.2 sec * 1 Mio
= 100 000 GB-Seconds
Requests:
$0.20
GB-Seconds:
$1.67
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Challenge Number 1 with Java is a
big cold-start
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: https://www.serverless.com/blog/keep-your-lambdas-warm
Cold Start
:
Sources: Ajay Nair „Become a Serverless Black Belt” https://www.youtube.com/watch?v=oQFORsso2go
Tomasz Łakomy "Notes from Optimizing Lambda Performance for Your Serverless Applications“ https://tlakomy.com/optimizing-lambda-performance-for-serverless-applications
• Start Firecracker VM
• AWS Lambda starts the JVM
• Java runtime loads and initializes
handler class
• Static initializer block of the handler class is
executed
• Init-phase has full CPU access up to 10 seconds
for free for the managed execution environments
• Lambda calls the handler method
• Full CPU access only approx. at 1.8 GB
“assigned” memory to the function
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Sources: Ajay Nair „Become a Serverless Black Belt” https://www.youtube.com/watch?v=oQFORsso2go
Tomasz Łakomy "Notes from Optimizing Lambda Performance for Your Serverless Applications“ https://tlakomy.com/optimizing-lambda-performance-for-serverless-applications
Michael Hart: „Shave 99.93% off your Lambda bill with this one weird trick“
https://hichaelmart.medium.com/shave-99-93-off-your-lambda-bill-with-this-one-weird-trick-33c0acebb2ea
Function lifecycle- a full cold start
:
Sources: Ajay Nair „Become a Serverless Black Belt” https://www.youtube.com/watch?v=oQFORsso2go
Tomasz Łakomy "Notes from Optimizing Lambda Performance for Your Serverless Applications“ https://tlakomy.com/optimizing-lambda-performance-for-serverless-applications
AWS Lambda cold start duration
per programming language
Source: Mikhail Shilkov: „AWS Lambda: Cold Start Duration per Language. 2020 edition” https://mikhail.io/serverless/coldstarts/aws/languages/
Cold start duration with Java
• Below 1 second is best-case cold start duration for very simple
Lambda like HelloWorld with no dependencies
• It goes up significantly with more complex scenarios
• Instantiation outside of the handler method (static initializer block) to
communicate with other (AWS) services (e.g. DynamoDB, SNS, SQS, 3rd party)
• Artifact size
Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8
Sean O‘Toole „AWS Lambda Java Tutorial: Best Practices to Lower Cold Starts” https://www.capitalone.com/tech/cloud/aws-lambda-java-tutorial-reduce-cold-starts/
AWS Lambda cold starts by memory size, runtime and artifact size
Source: Mike Roberts "Analyzing Cold Start latency of AWS Lambda" https://blog.symphonia.io/posts/2020-06-30_analyzing_cold_start_latency_of_aws_lambda
Artifact Size:
• Small zip (1KB)
• Large zip (48MB)
• Large uberjar (53MB)
Cold start duration with Java
• Below 1 second is best-case cold start duration for very simple
Lambda like HelloWorld with no dependencies
• It goes up significantly with more complex scenarios
• Instantiation outside of the handler method (static instantiation) to communicate
with other (AWS) services (i.e. DynamoDB, SNS, SQS, 3rd party)
• Artifact size
• To minimize the cold start time apply best practices from this talk
• Worst-case cold starts can be higher than 10 and even 20 seconds
Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8
Sean O‘Toole „AWS Lambda Java Tutorial: Best Practices to Lower Cold Starts” https://www.capitalone.com/tech/cloud/aws-lambda-java-tutorial-reduce-cold-starts/
2020 AWS Lambda Benchmark Report for Developers, DevOps, and Decision Makers
https://newrelic.com/resources/ebooks/serverless-benchmark-report-aws-lambda-2020
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Provisioned
Concurrency for
Lambda Functions
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Yan Cui: https://lumigo.io/blog/provisioned-concurrency-the-end-of-cold-starts/
Jeremy Daly: “Mixing VPC and Non-VPC Lambda Functions for Higher Performing Microservices”
https://www.jeremydaly.com/mixing-vpc-and-non-vpc-lambda-functions-for-higher-performing-microservices/ Vadym Kazulkin @VKazulkin , ip.labs GmbH
Lambda behind the
Virtual Private
Cloud (VPC)
Lambda in VPC
As function’s execution environment
scales
• More network interfaces are created and
attached to the Lambda infrastructure
• The exact number of network interfaces
created and attached is a factor of your
function configuration and concurrency
• Caused additional the cold start up to
approx. 10 seconds
Chris Munns: "Announcing improved VPC networking for AWS Lambda functions”
https://aws.amazon.com/de/blogs/compute/announcing-improved-vpc-networking-for-aws-lambda-functions/
Lambda in VPC Improvements:
• The network interface creation happens
when Lambda function is created or its
VPC settings are updated.
• The network interfaces are shared across
execution environments
• Only a handful of network interfaces are
required per function
• Reduced additional cold start from approx.
10 seconds to below 1 second
Chris Munns: "Announcing improved VPC networking for AWS Lambda functions”
https://aws.amazon.com/de/blogs/compute/announcing-improved-vpc-networking-for-aws-lambda-functions/
Improvements 1/4
• Switch to the AWS SDK 2.0 for Java
• Lower footprint and more modular
• Allows to configure HTTP Client of your choice (i.e. Java own Basic HTTP Client
or newly introduced AWS Common Runtime async HTTP Client)
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8
Zoe Wang: „Introducing AWS Common Runtime HTTP Client in the AWS SDK for Java 2.x”
https://aws.amazon.com/de/blogs/developer/introducing-aws-common-runtime-http-client-in-the-aws-sdk-for-java-2-x/
S3AsyncClient.builder()
.httpClientBuilder(AwsCrtAsyncHttpClient.builder()
.maxConcurrency(50))
.build();
Improvements 1/4
Provide all known values (for building clients i.e. DynamoDB client)
to avoid auto-discovery
• credential provider, region, endpoint
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.US_WEST_2)
.withCredentials(new ProfileCredentialsProvider("myProfile"))
.build();
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8
Sean O‘Toole „AWS Lambda Java Tutorial: Best Practices to Lower Cold Starts” https://www.capitalone.com/tech/cloud/aws-lambda-java-tutorial-reduce-cold-starts/
Improvements 1/4
• Initialize and prime dependencies during initialization phase
• Use static initialization in the handler class, instead of in the handler method (e.g.
handleRequest) to take the advantage of the access to the full CPU core for max 10 seconds
• „Fake“ the calls to pre-initalize „things“, e.g. Jackson Marshallers in case of DynamoDB client
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()...build();
DynamoDB dynamoDB = new DynamoDB(client);
Table table = dynamoDB.getTable(„mytable");
Item item = table.getItem("Id", 210);
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8
Sean O‘Toole „AWS Lambda Java Tutorial: Best Practices to Lower Cold Starts” https://www.capitalone.com/tech/cloud/aws-lambda-java-tutorial-reduce-cold-starts/
getItem() call forces Jackson Marshallers to initialize
Improvements 1/4
• Less (dependencies, classes) is more
• Include only required dependencies (e.g. not the whole AWS SDK 2.0 for Java, but the
dependencies to the clients to be used in Lambda)
• Exclude dependencies, which you don‘t need at runtime e.g. test frameworks like Junit
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8
Sean O‘Toole „AWS Lambda Java Tutorial: Best Practices to Lower Cold Starts” https://www.capitalone.com/tech/cloud/aws-lambda-java-tutorial-reduce-cold-starts/
https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
<version>2.10.86</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.10.86</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Improvements 1/4
Further Improvements:
• Consider using JDK Class-Data Archive and Application Class-Data Sharing
available since Java 10 and improved in Java 13
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: Snicolai Parlow : "Application Class-Data Sharing In A Nutshell„ https://nipafx.dev/java-application-class-data-sharing/
Improvements 2/4
Avoid:
• reflection
• runtime byte code generation
• runtime generated proxies
• dynamic class loading
Use DI Frameworks like Micronaut, Quarkus or Dagger which aren‘t
reflection-based
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8
Sean O‘Toole „AWS Lambda Java Tutorial: Best Practices to Lower Cold Starts” https://www.capitalone.com/tech/cloud/aws-lambda-java-tutorial-reduce-cold-starts/
Improvements 3/4
Strive for cost optimization
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Cost for Lambda
REQUEST DURATION
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Cost scales
linearly with
memory
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Improvements 4/4
More memory = more expensive?
Christian Bannes and Vadym Kazulkin @VKazulkin , ip.labs GmbH
Tooling for AWS Lambda in Java
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Lambda Power Tuning 1/2
• Executes different
settings in parallel
• Outputs the optimal
setting
Image: https://github.com/alexcasalboni/aws-lambda-power-tuning Vadym Kazulkin @VKazulkin , ip.labs GmbH
Lambda Power Tuning 2/2
• Executes different
settings in parallel
• Outputs the optimal
setting
Image: https://github.com/alexcasalboni/aws-lambda-power-tuning
Alex Casalboni: “Deep dive: finding the optimal resources allocation for your Lambda functions“
https://dev.to/aws/deep-dive-finding-the-optimal-resources-allocation-for-your-lambda-functions-35a6
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Optimizing AWS Lambda cost
and performance using AWS
Compute Optimizer
Source: Chad Schmutzer „Optimizing AWS Lambda cost and performance using AWS Compute Optimizer”
https://aws.amazon.com/de/blogs/compute/optimizing-aws-lambda-cost-and-performance-using-aws-compute-optimizer/
AWS Lambda
Powertools Java 1/3
https://aws.amazon.com/de/blogs/opensource/simplifying-serverless-best-practices-with-aws-lambda-powertools-java/
AWS Lambda
Powertools Java 2/3
https://aws.amazon.com/de/blogs/opensource/simplifying-serverless-best-practices-with-aws-lambda-powertools-java/
When using different Java
annotations provided by the
library, configure the aspectj-
maven-plugin to compile-time
weave the aws-lambda-
powertools-java aspects into
your project.
AWS Lambda
Powertools Java 3/3
https://aws.amazon.com/de/blogs/opensource/simplifying-serverless-best-practices-with-aws-lambda-powertools-java/
Testing AWS Lambda functions written in
Java with aws-lambda-java-tests project
«Testing AWS Lambda functions written in Java” https://github.com/aws/aws-lambda-java-libs/tree/master/aws-lambda-java-tests
https://aws.amazon.com/de/blogs/opensource/testing-aws-lambda-functions-written-in-java/
AWS Lambda Java Libs
https://github.com/aws/aws-lambda-java-libs
Cost optimization
• Java is well optimized for long running server applications
• High startup times
• High memory utilization
Vadym Kazulkin @VKazulkin , ip.labs GmbH
And both memory and execution time are cost dimensions,
when using Serverless in the cloud
GraalVM enters the scene
Source: https://www.graalvm.org/
GraalVM
Goals:
Low footprint ahead-of-time mode for JVM-based languages
High performance for all languages
Convenient language interoperability and polyglot tooling
Source: „Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger” https://www.youtube.com/watch?v=ANN9rxYo5Hg
GraalVM
Architecture
Sources: Practical Partial Evaluation for High-Performance Dynamic Language Runtimes http://chrisseaton.com/rubytruffle/pldi17-truffle/pldi17-truffle.pdf
„The LLVM Compiler Infrastructure“ https://llvm.org/
SubstrateVM
Source: Oleg Šelajev, Thomas Wuerthinger, Oracle: “Deep dive into using GraalVM for Java and JavaScript”
https://www.youtube.com/watch?v=a-XEZobXspo
GraalVM and SubstrateVM
Source: Oleg Selajev, Oracle : “Run Code in Any Language Anywhere with GraalVM” https://www.youtube.com/watch?v=JoDOo4FyYMU
GraalVM on SubstrateVM
A game changer for Java & Serverless?
Java Function compiled into a native executable using
GraalVM on SubstrateVM reduces
• “cold start” times
• memory footprint
by order of magnitude compared to running on JVM.
And both memory and execution time are cost dimensions,
when using Serverless in the cloud
GraalVM on SubstrateVM
A game changer for Java & Serverless?
Current challenges with native executable using GraalVM :
• Most Cloud Providers (AWS) doesn’t provide GraalVM as Java
Runtime out of the box, only Open JDK (i.e. AWS provides Corretto)
• Some Cloud Providers (e.g. AWS) provide Custom Runtime Option
Lambda Layers
& Lambda
Runtime API
Christian Bannes and Vadym Kazulkin @VKazulkin , ip.labs GmbH
Custom Lambda Runtimes
GraalVM Complitation Modes
Source: „Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger” https://www.youtube.com/watch?v=ANN9rxYo5Hg
AOT vs JIT
Source: „Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger” https://www.youtube.com/watch?v=ANN9rxYo5Hg
Support of GraalVM native images in Frameworks
Spring Framework: working toward GraalVM native image support
without requiring additional configuration or workaround is one of the
themes of upcoming Spring Framework 5.3
Spring Boot: Ongoing work on experimental Spring Graal Native
project. Probably ready for the 2.4 release
Quarkus: a Kubernetes Native Java framework developed by Red Hat
tailored for GraalVM and HotSpot, crafted from best-of-breed Java
libraries and standards.
Micronaut: a modern, JVM-based, full-stack framework for building
modular, easily testable microservice and serverless applications.
Source: „GraalVM native image support“ https://github.com/spring-projects/spring-framework/wiki/GraalVM-native-image-support
Common principles for all Frameworks
• Rely on as little reflection as possible
• Avoid runtime byte code generation, runtime generated proxies and
dynamic class loading as much as possible
• Process annotations at compile time
• The common goals:
• increase developer productivity
• speed up start up times and decrease memory usage for Microservice and
Serverless Java applications
• with and without usage of GraalVM and native image
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Micronaut Framework
Source: https://micronaut.io/
AWS Lambda with Micronaut Framework
Micronaut Additional Features
• Custom Validators
• AWS API Gateway integration
• Spring annotation processor available
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8
Build GraalVM Native Image with Micronaut Framework
Maven plugin available since Micronaut 2.2 for building
GraalVM Native Image
./mvnw package -Dpackaging=native-image
Docker
./mvnw mn:dockerfile -Dpackaging=docker
Docker Native Image
./mvnw mn:dockerfile -Dpackaging=docker-native
Source: “What's new for Maven users in Micronaut 2.2” : https://micronaut.io/blog/2020-11-23-whats-new-maven.html
Quarkus
Source: https://quarkus.io/
AWS Lambda with Quarkus Framework
Build GraalVM Native Image with Quarkus Framework
mvn –Pnative package
Quarkus Additional Features
• Website for creating the App
• AWS API Gateway integration
• Funqy for multi cloud solutions
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8
Quarkus Fanqy
Source: https://quarkus.io/guides/funqy
Spring (Boot) Framework
Source: https://spring.io/
Spring GraalVM Native Project
Vadym Kazulkin @VKazulkin , ip.labs GmbH
AWS Lambda with Spring Framework
using Spring Graal Native and Spring Cloud Functions
Bean Registration with Spring Framework
using Spring Graal Native and Spring Cloud Functions
Build GraalVM Native Image with Spring Framework
mvn –Pnative package
Framework Comparison 1/2
• Project Initializer
• Programming Model
• Database Support
• Test Support
• Standards Support (i.e. MicroProfile)
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: „Battle Of The Microservice Frameworks: Micronaut versus Quarkus edition! by Michel Schudel“ https://www.youtube.com/watch?v=hnEXOqcNXPs
„Micronaut 2.0 vs Quarkus 1.3.1 vs Spring Boot 2.3 Performance on JDK 14“ https://www.youtube.com/watch?v=rJFgdFIs_k8
„Java EE, Jakarta EE, MicroProfile, or Maybe All of Them” https://www.eclipse.org/community/eclipse_newsletter/2019/february/Jakarta_Micro_All.php
Framework Comparison 2/2
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: „Battle Of The Microservice Frameworks: Micronaut versus Quarkus edition! by Michel Schudel“ https://www.youtube.com/watch?v=hnEXOqcNXPs
„Micronaut 2.0 vs Quarkus 1.3.1 vs Spring Boot 2.3 Performance on JDK 14“ https://www.youtube.com/watch?v=rJFgdFIs_k8
• Native Image with GraalVM
• Build time
• Framework compile-time activities
• GraalV Native Image compilation time
• Native Image Size
• (Cold) Startup Time
• Request/Invocation duration
• Memory Consumption
• Plain
• Application Size
• (Cold) Start Time
• Request/Invocation duration
• Memory Consumption
Lambda Container Image Support
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: „https://aws.amazon.com/de/blogs/aws/new-for-aws-lambda-container-image-support/
Lambda Container Image Support
Use Cases:
• What about the support of the current Java version?
• Only Long Term Support (LTS) by AWS
• Java 8 and Java 11, next Java 17 Fall 2021
• Use Container (Docker) Image with i.e. Java 15
• GraalVM without Native Image may be faster
than OpenJDK for your application
• Use Container (Docker) Image with GraalVM
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: https://aws.amazon.com/de/corretto/
https://renaissance.dev/
Conclusion 1/2
• GraalVM and Frameworks are really powerful with a lot of potential
• Micronaut and Quarkus will improve the cold starts of Java
applications significantly even without GraalVM and native image
• They use as little as possible reflection and other runtime optimizations
• Cold start duration will still be in the range of maximal single digit (e.g. 1.5) seconds
depending on your application and framework
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: „https://stackoverflow.com/questions/63328298/how-do-you-debug-a-no-instances-of-are-allowed-in-the-image-heap-when-buil
Conclusion 2/2
• But the combination of discussed frameworks with GraalVM and Native
Image currently not without challenges
• AWS Lambda Custom Runtime requires Linux executable only
• Managing Custom Runtime requires some additional effort
• You pay for the init-phase of the function packaged as AWS Lambda Custom Runtime
• Delivering execution runtime (i.e. Java) via Lambda Custom Runtime reduces performance
compared to the usage of the same managed Lambda Runtime
• Reduce cold starts to several hundred milliseconds and very low memory consumption
• Lots of experimentation with additional build arguments like “initialize-at-runtime” or “delay-
class-initialization-to-runtime“ required
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Source: https://stackoverflow.com/questions/63328298/how-do-you-debug-a-no-instances-of-are-allowed-in-the-image-heap-when-buil
Personal Recommendations (highly opinionated)
• By default use Java Long Term Support Version with Amazon Corretto and your
favourite Framework (Micronaut, Quarkus)
• If your function needs constantly low response times for the known period of time ?
• Use Provisioned Concurrency additionally
• If your function needs constantly low response time and low cost is a requirement?
• Use GraalVM Native Image with your favourite Framework (Micronaut, Quarkus, Spring Boot
GraalVM Native) and AWS Lambda Custom Runtime
• Define 2 pipelines: „standard“ for quick and local testing and „native image“ for production usage
• If you don‘t want to miss 3 years of innovation and use the newest Java Version?
• Use Lambda Container Image Support
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Where AWS can help (highly opinionated again) 1/2
• Optimize Amazon Corretto for Lambda with Java on AWS
• Rethink garbage collection
• Find trade-off between increase of memory consumption because of the garbage and performance
loss of GC algorithms for the short running Lambda functions (max. 1 hour or so)
• Rethink HotSpot Just-In-Time Compiler optimizations
• Most JIT optimizations apply by default only after multiple thousands executions (i.e. 10.000) of the
function (so never), so find the write configuration for the short running Lambda functions
• Provide support for jlink
• jlink is tool that generates a custom Java runtime image that contains only the platform modules that
are required for a given application
• See what Oracle introduced for the Fn Project
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Sources: „Guide to jlink” https://www.baeldung.com/jlink
„Open Source. Container-native. Serverless platform“ https://fnproject.io/
Try it yourselves
• Micronaut
• https://github.com/micronaut-guides/micronaut-function-aws-lambda
• Quarkus
• https://github.com/JosemyDuarte/quarkus-terraform-lambda-demo/tree/dynamo-terraform
• Spring Boot
• https://github.com/spring-projects-experimental/spring-graalvm-native/tree/master/spring-
graalvm-native-samples/function-aws
• Misc
• https://github.com/awslabs/aws-serverless-java-container/tree/master/samples
Vadym Kazulkin @VKazulkin , ip.labs GmbH
What about Project Helidon?
Source: https://helidon.io
Project Leyden
Source: https://mail.openjdk.java.net/pipermail/discuss/2020-April/005429.html
www.iplabs.de
Thank You!

Adopting Java for the Serverless world at Serverless Meetup Singapore

  • 1.
    Adopting Java forthe Serverless world from the perspective of the AWS developer Vadym Kazulkin, ip.labs, Serverless Singapore, 16 February 2021
  • 2.
    Contact Vadym Kazulkin ip.labs Bonn,Germany Co-Organizer: Java User Group Bonn and Serverless Bonn Meetup [email protected] @VKazulkin @ServerlessBonn (Meetup) https://www.linkedin.com/in/vadymkazulkin/ https://www.iplabs.de/
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
    2020 Magic Quadrantfor Cloud Infrastructure & Platform Services https://pages.awscloud.com/GLOBAL-multi-DL-gartner-mq-cips-2020-learn.html?pg=LWIAWS Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 8.
    Vadym Kazulkin @VKazulkin, ip.labs GmbH “State of Serverless 2020 report” https://codingsans.com/blog/serverless-trends
  • 9.
    2020 AWS LambdaBenchmark Report for Developers, DevOps, and Decision Makers https://newrelic.com/resources/ebooks/serverless-benchmark-report-aws-lambda-2020 Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 10.
    Life of theJava Serverless developer on AWS
  • 11.
    AWS Java VersionsSupport • Java 8 • With extended long-term support • Java 11 (since 2019) • Only Long Term Support (LTS) by AWS • Next LTS Java version is Java 17 in 2021 Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: https://aws.amazon.com/de/corretto/
  • 12.
    Java ist veryfast and mature programming language… Image: burst.shopify.com/photos/a-look-across-the-landscape-with-view-of-the-sea Christian Bannes and Vadym Kazulkin @VKazulkin , ip.labs GmbH … but Serverless adoption of Java looks like this
  • 13.
    2020 AWS LambdaBenchmark Report for Developers, DevOps, and Decision Makers https://newrelic.com/resources/ebooks/serverless-benchmark-report-aws-lambda-2020 Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 14.
    Developers love Javaand will be happy to use it for Serverless But what are the challenges Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 15.
    AWS Lambda Basics VadymKazulkin @VKazulkin , ip.labs GmbH
  • 16.
    Creating AWS Lambdawith Java 1/3 : Source https://blog.runscope.com/posts/how-to-write-your-first-aws-lambda-function
  • 17.
    Creating AWS Lambdawith Java 2/3 : Source https://blog.runscope.com/posts/how-to-write-your-first-aws-lambda-function
  • 18.
    Creating AWS Lambdawith Java 3/3 : Source https://docs.aws.amazon.com/lambda/latest/dg/java-context.html
  • 19.
    AWS Lambda PriceModel Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 20.
    Cost for Lambda REQUESTDURATION Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 21.
    Request Tier $ 0.20 Per1 Mio Requests Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 22.
    Duration Tier $ 0.00001667 PerGB-Second Christian Bannes and Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 23.
    GB-Second ONE SECOND ONEGB Christian Bannes and Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 24.
    Example • 1 Miorequests • Lambda with 512MiB • Each lambda takes 200ms 0.5 GiB * 0.2 sec * 1 Mio = 100 000 GB-Seconds Requests: $0.20 GB-Seconds: $1.67 Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 25.
    Challenge Number 1with Java is a big cold-start Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: https://www.serverless.com/blog/keep-your-lambdas-warm
  • 26.
    Cold Start : Sources: AjayNair „Become a Serverless Black Belt” https://www.youtube.com/watch?v=oQFORsso2go Tomasz Łakomy "Notes from Optimizing Lambda Performance for Your Serverless Applications“ https://tlakomy.com/optimizing-lambda-performance-for-serverless-applications
  • 27.
    • Start FirecrackerVM • AWS Lambda starts the JVM • Java runtime loads and initializes handler class • Static initializer block of the handler class is executed • Init-phase has full CPU access up to 10 seconds for free for the managed execution environments • Lambda calls the handler method • Full CPU access only approx. at 1.8 GB “assigned” memory to the function Vadym Kazulkin @VKazulkin , ip.labs GmbH Sources: Ajay Nair „Become a Serverless Black Belt” https://www.youtube.com/watch?v=oQFORsso2go Tomasz Łakomy "Notes from Optimizing Lambda Performance for Your Serverless Applications“ https://tlakomy.com/optimizing-lambda-performance-for-serverless-applications Michael Hart: „Shave 99.93% off your Lambda bill with this one weird trick“ https://hichaelmart.medium.com/shave-99-93-off-your-lambda-bill-with-this-one-weird-trick-33c0acebb2ea
  • 28.
    Function lifecycle- afull cold start : Sources: Ajay Nair „Become a Serverless Black Belt” https://www.youtube.com/watch?v=oQFORsso2go Tomasz Łakomy "Notes from Optimizing Lambda Performance for Your Serverless Applications“ https://tlakomy.com/optimizing-lambda-performance-for-serverless-applications
  • 29.
    AWS Lambda coldstart duration per programming language Source: Mikhail Shilkov: „AWS Lambda: Cold Start Duration per Language. 2020 edition” https://mikhail.io/serverless/coldstarts/aws/languages/
  • 30.
    Cold start durationwith Java • Below 1 second is best-case cold start duration for very simple Lambda like HelloWorld with no dependencies • It goes up significantly with more complex scenarios • Instantiation outside of the handler method (static initializer block) to communicate with other (AWS) services (e.g. DynamoDB, SNS, SQS, 3rd party) • Artifact size Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8 Sean O‘Toole „AWS Lambda Java Tutorial: Best Practices to Lower Cold Starts” https://www.capitalone.com/tech/cloud/aws-lambda-java-tutorial-reduce-cold-starts/
  • 31.
    AWS Lambda coldstarts by memory size, runtime and artifact size Source: Mike Roberts "Analyzing Cold Start latency of AWS Lambda" https://blog.symphonia.io/posts/2020-06-30_analyzing_cold_start_latency_of_aws_lambda Artifact Size: • Small zip (1KB) • Large zip (48MB) • Large uberjar (53MB)
  • 32.
    Cold start durationwith Java • Below 1 second is best-case cold start duration for very simple Lambda like HelloWorld with no dependencies • It goes up significantly with more complex scenarios • Instantiation outside of the handler method (static instantiation) to communicate with other (AWS) services (i.e. DynamoDB, SNS, SQS, 3rd party) • Artifact size • To minimize the cold start time apply best practices from this talk • Worst-case cold starts can be higher than 10 and even 20 seconds Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8 Sean O‘Toole „AWS Lambda Java Tutorial: Best Practices to Lower Cold Starts” https://www.capitalone.com/tech/cloud/aws-lambda-java-tutorial-reduce-cold-starts/
  • 33.
    2020 AWS LambdaBenchmark Report for Developers, DevOps, and Decision Makers https://newrelic.com/resources/ebooks/serverless-benchmark-report-aws-lambda-2020 Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 34.
    Provisioned Concurrency for Lambda Functions VadymKazulkin @VKazulkin , ip.labs GmbH Yan Cui: https://lumigo.io/blog/provisioned-concurrency-the-end-of-cold-starts/
  • 35.
    Jeremy Daly: “MixingVPC and Non-VPC Lambda Functions for Higher Performing Microservices” https://www.jeremydaly.com/mixing-vpc-and-non-vpc-lambda-functions-for-higher-performing-microservices/ Vadym Kazulkin @VKazulkin , ip.labs GmbH Lambda behind the Virtual Private Cloud (VPC)
  • 36.
    Lambda in VPC Asfunction’s execution environment scales • More network interfaces are created and attached to the Lambda infrastructure • The exact number of network interfaces created and attached is a factor of your function configuration and concurrency • Caused additional the cold start up to approx. 10 seconds Chris Munns: "Announcing improved VPC networking for AWS Lambda functions” https://aws.amazon.com/de/blogs/compute/announcing-improved-vpc-networking-for-aws-lambda-functions/
  • 37.
    Lambda in VPCImprovements: • The network interface creation happens when Lambda function is created or its VPC settings are updated. • The network interfaces are shared across execution environments • Only a handful of network interfaces are required per function • Reduced additional cold start from approx. 10 seconds to below 1 second Chris Munns: "Announcing improved VPC networking for AWS Lambda functions” https://aws.amazon.com/de/blogs/compute/announcing-improved-vpc-networking-for-aws-lambda-functions/
  • 38.
    Improvements 1/4 • Switchto the AWS SDK 2.0 for Java • Lower footprint and more modular • Allows to configure HTTP Client of your choice (i.e. Java own Basic HTTP Client or newly introduced AWS Common Runtime async HTTP Client) Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8 Zoe Wang: „Introducing AWS Common Runtime HTTP Client in the AWS SDK for Java 2.x” https://aws.amazon.com/de/blogs/developer/introducing-aws-common-runtime-http-client-in-the-aws-sdk-for-java-2-x/ S3AsyncClient.builder() .httpClientBuilder(AwsCrtAsyncHttpClient.builder() .maxConcurrency(50)) .build();
  • 39.
    Improvements 1/4 Provide allknown values (for building clients i.e. DynamoDB client) to avoid auto-discovery • credential provider, region, endpoint AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withRegion(Regions.US_WEST_2) .withCredentials(new ProfileCredentialsProvider("myProfile")) .build(); Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8 Sean O‘Toole „AWS Lambda Java Tutorial: Best Practices to Lower Cold Starts” https://www.capitalone.com/tech/cloud/aws-lambda-java-tutorial-reduce-cold-starts/
  • 40.
    Improvements 1/4 • Initializeand prime dependencies during initialization phase • Use static initialization in the handler class, instead of in the handler method (e.g. handleRequest) to take the advantage of the access to the full CPU core for max 10 seconds • „Fake“ the calls to pre-initalize „things“, e.g. Jackson Marshallers in case of DynamoDB client AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()...build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable(„mytable"); Item item = table.getItem("Id", 210); Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8 Sean O‘Toole „AWS Lambda Java Tutorial: Best Practices to Lower Cold Starts” https://www.capitalone.com/tech/cloud/aws-lambda-java-tutorial-reduce-cold-starts/ getItem() call forces Jackson Marshallers to initialize
  • 41.
    Improvements 1/4 • Less(dependencies, classes) is more • Include only required dependencies (e.g. not the whole AWS SDK 2.0 for Java, but the dependencies to the clients to be used in Lambda) • Exclude dependencies, which you don‘t need at runtime e.g. test frameworks like Junit Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8 Sean O‘Toole „AWS Lambda Java Tutorial: Best Practices to Lower Cold Starts” https://www.capitalone.com/tech/cloud/aws-lambda-java-tutorial-reduce-cold-starts/ https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2 <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.4.2</version> <scope>test</scope> </dependency> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>dynamodb</artifactId> <version>2.10.86</version> </dependency> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>bom</artifactId> <version>2.10.86</version> <type>pom</type> <scope>import</scope> </dependency>
  • 42.
    Improvements 1/4 Further Improvements: •Consider using JDK Class-Data Archive and Application Class-Data Sharing available since Java 10 and improved in Java 13 Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: Snicolai Parlow : "Application Class-Data Sharing In A Nutshell„ https://nipafx.dev/java-application-class-data-sharing/
  • 43.
    Improvements 2/4 Avoid: • reflection •runtime byte code generation • runtime generated proxies • dynamic class loading Use DI Frameworks like Micronaut, Quarkus or Dagger which aren‘t reflection-based Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8 Sean O‘Toole „AWS Lambda Java Tutorial: Best Practices to Lower Cold Starts” https://www.capitalone.com/tech/cloud/aws-lambda-java-tutorial-reduce-cold-starts/
  • 44.
    Improvements 3/4 Strive forcost optimization Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 45.
    Cost for Lambda REQUESTDURATION Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 46.
    Cost scales linearly with memory VadymKazulkin @VKazulkin , ip.labs GmbH
  • 47.
    Improvements 4/4 More memory= more expensive? Christian Bannes and Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 48.
    Tooling for AWSLambda in Java Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 49.
    Lambda Power Tuning1/2 • Executes different settings in parallel • Outputs the optimal setting Image: https://github.com/alexcasalboni/aws-lambda-power-tuning Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 50.
    Lambda Power Tuning2/2 • Executes different settings in parallel • Outputs the optimal setting Image: https://github.com/alexcasalboni/aws-lambda-power-tuning Alex Casalboni: “Deep dive: finding the optimal resources allocation for your Lambda functions“ https://dev.to/aws/deep-dive-finding-the-optimal-resources-allocation-for-your-lambda-functions-35a6 Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 51.
    Optimizing AWS Lambdacost and performance using AWS Compute Optimizer Source: Chad Schmutzer „Optimizing AWS Lambda cost and performance using AWS Compute Optimizer” https://aws.amazon.com/de/blogs/compute/optimizing-aws-lambda-cost-and-performance-using-aws-compute-optimizer/
  • 52.
    AWS Lambda Powertools Java1/3 https://aws.amazon.com/de/blogs/opensource/simplifying-serverless-best-practices-with-aws-lambda-powertools-java/
  • 53.
    AWS Lambda Powertools Java2/3 https://aws.amazon.com/de/blogs/opensource/simplifying-serverless-best-practices-with-aws-lambda-powertools-java/ When using different Java annotations provided by the library, configure the aspectj- maven-plugin to compile-time weave the aws-lambda- powertools-java aspects into your project.
  • 54.
    AWS Lambda Powertools Java3/3 https://aws.amazon.com/de/blogs/opensource/simplifying-serverless-best-practices-with-aws-lambda-powertools-java/
  • 55.
    Testing AWS Lambdafunctions written in Java with aws-lambda-java-tests project «Testing AWS Lambda functions written in Java” https://github.com/aws/aws-lambda-java-libs/tree/master/aws-lambda-java-tests https://aws.amazon.com/de/blogs/opensource/testing-aws-lambda-functions-written-in-java/
  • 56.
    AWS Lambda JavaLibs https://github.com/aws/aws-lambda-java-libs
  • 57.
    Cost optimization • Javais well optimized for long running server applications • High startup times • High memory utilization Vadym Kazulkin @VKazulkin , ip.labs GmbH And both memory and execution time are cost dimensions, when using Serverless in the cloud
  • 58.
    GraalVM enters thescene Source: https://www.graalvm.org/
  • 59.
    GraalVM Goals: Low footprint ahead-of-timemode for JVM-based languages High performance for all languages Convenient language interoperability and polyglot tooling Source: „Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger” https://www.youtube.com/watch?v=ANN9rxYo5Hg
  • 60.
    GraalVM Architecture Sources: Practical PartialEvaluation for High-Performance Dynamic Language Runtimes http://chrisseaton.com/rubytruffle/pldi17-truffle/pldi17-truffle.pdf „The LLVM Compiler Infrastructure“ https://llvm.org/
  • 61.
    SubstrateVM Source: Oleg Šelajev,Thomas Wuerthinger, Oracle: “Deep dive into using GraalVM for Java and JavaScript” https://www.youtube.com/watch?v=a-XEZobXspo
  • 62.
    GraalVM and SubstrateVM Source:Oleg Selajev, Oracle : “Run Code in Any Language Anywhere with GraalVM” https://www.youtube.com/watch?v=JoDOo4FyYMU
  • 63.
    GraalVM on SubstrateVM Agame changer for Java & Serverless? Java Function compiled into a native executable using GraalVM on SubstrateVM reduces • “cold start” times • memory footprint by order of magnitude compared to running on JVM. And both memory and execution time are cost dimensions, when using Serverless in the cloud
  • 64.
    GraalVM on SubstrateVM Agame changer for Java & Serverless? Current challenges with native executable using GraalVM : • Most Cloud Providers (AWS) doesn’t provide GraalVM as Java Runtime out of the box, only Open JDK (i.e. AWS provides Corretto) • Some Cloud Providers (e.g. AWS) provide Custom Runtime Option
  • 65.
    Lambda Layers & Lambda RuntimeAPI Christian Bannes and Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 66.
  • 67.
    GraalVM Complitation Modes Source:„Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger” https://www.youtube.com/watch?v=ANN9rxYo5Hg
  • 68.
    AOT vs JIT Source:„Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger” https://www.youtube.com/watch?v=ANN9rxYo5Hg
  • 69.
    Support of GraalVMnative images in Frameworks Spring Framework: working toward GraalVM native image support without requiring additional configuration or workaround is one of the themes of upcoming Spring Framework 5.3 Spring Boot: Ongoing work on experimental Spring Graal Native project. Probably ready for the 2.4 release Quarkus: a Kubernetes Native Java framework developed by Red Hat tailored for GraalVM and HotSpot, crafted from best-of-breed Java libraries and standards. Micronaut: a modern, JVM-based, full-stack framework for building modular, easily testable microservice and serverless applications. Source: „GraalVM native image support“ https://github.com/spring-projects/spring-framework/wiki/GraalVM-native-image-support
  • 70.
    Common principles forall Frameworks • Rely on as little reflection as possible • Avoid runtime byte code generation, runtime generated proxies and dynamic class loading as much as possible • Process annotations at compile time • The common goals: • increase developer productivity • speed up start up times and decrease memory usage for Microservice and Serverless Java applications • with and without usage of GraalVM and native image Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 71.
  • 72.
    AWS Lambda withMicronaut Framework
  • 73.
    Micronaut Additional Features •Custom Validators • AWS API Gateway integration • Spring annotation processor available Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8
  • 74.
    Build GraalVM NativeImage with Micronaut Framework Maven plugin available since Micronaut 2.2 for building GraalVM Native Image ./mvnw package -Dpackaging=native-image Docker ./mvnw mn:dockerfile -Dpackaging=docker Docker Native Image ./mvnw mn:dockerfile -Dpackaging=docker-native Source: “What's new for Maven users in Micronaut 2.2” : https://micronaut.io/blog/2020-11-23-whats-new-maven.html
  • 75.
  • 76.
    AWS Lambda withQuarkus Framework
  • 77.
    Build GraalVM NativeImage with Quarkus Framework mvn –Pnative package
  • 78.
    Quarkus Additional Features •Website for creating the App • AWS API Gateway integration • Funqy for multi cloud solutions Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: Stefano Buliani : "Best practices for AWS Lambda and Java„ https://www.youtube.com/watch?v=ddg1u5HLwg8
  • 79.
  • 80.
    Spring (Boot) Framework Source:https://spring.io/
  • 81.
    Spring GraalVM NativeProject Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 82.
    AWS Lambda withSpring Framework using Spring Graal Native and Spring Cloud Functions
  • 83.
    Bean Registration withSpring Framework using Spring Graal Native and Spring Cloud Functions
  • 84.
    Build GraalVM NativeImage with Spring Framework mvn –Pnative package
  • 85.
    Framework Comparison 1/2 •Project Initializer • Programming Model • Database Support • Test Support • Standards Support (i.e. MicroProfile) Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: „Battle Of The Microservice Frameworks: Micronaut versus Quarkus edition! by Michel Schudel“ https://www.youtube.com/watch?v=hnEXOqcNXPs „Micronaut 2.0 vs Quarkus 1.3.1 vs Spring Boot 2.3 Performance on JDK 14“ https://www.youtube.com/watch?v=rJFgdFIs_k8 „Java EE, Jakarta EE, MicroProfile, or Maybe All of Them” https://www.eclipse.org/community/eclipse_newsletter/2019/february/Jakarta_Micro_All.php
  • 86.
    Framework Comparison 2/2 VadymKazulkin @VKazulkin , ip.labs GmbH Source: „Battle Of The Microservice Frameworks: Micronaut versus Quarkus edition! by Michel Schudel“ https://www.youtube.com/watch?v=hnEXOqcNXPs „Micronaut 2.0 vs Quarkus 1.3.1 vs Spring Boot 2.3 Performance on JDK 14“ https://www.youtube.com/watch?v=rJFgdFIs_k8 • Native Image with GraalVM • Build time • Framework compile-time activities • GraalV Native Image compilation time • Native Image Size • (Cold) Startup Time • Request/Invocation duration • Memory Consumption • Plain • Application Size • (Cold) Start Time • Request/Invocation duration • Memory Consumption
  • 87.
    Lambda Container ImageSupport Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: „https://aws.amazon.com/de/blogs/aws/new-for-aws-lambda-container-image-support/
  • 88.
    Lambda Container ImageSupport Use Cases: • What about the support of the current Java version? • Only Long Term Support (LTS) by AWS • Java 8 and Java 11, next Java 17 Fall 2021 • Use Container (Docker) Image with i.e. Java 15 • GraalVM without Native Image may be faster than OpenJDK for your application • Use Container (Docker) Image with GraalVM Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: https://aws.amazon.com/de/corretto/ https://renaissance.dev/
  • 89.
    Conclusion 1/2 • GraalVMand Frameworks are really powerful with a lot of potential • Micronaut and Quarkus will improve the cold starts of Java applications significantly even without GraalVM and native image • They use as little as possible reflection and other runtime optimizations • Cold start duration will still be in the range of maximal single digit (e.g. 1.5) seconds depending on your application and framework Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: „https://stackoverflow.com/questions/63328298/how-do-you-debug-a-no-instances-of-are-allowed-in-the-image-heap-when-buil
  • 90.
    Conclusion 2/2 • Butthe combination of discussed frameworks with GraalVM and Native Image currently not without challenges • AWS Lambda Custom Runtime requires Linux executable only • Managing Custom Runtime requires some additional effort • You pay for the init-phase of the function packaged as AWS Lambda Custom Runtime • Delivering execution runtime (i.e. Java) via Lambda Custom Runtime reduces performance compared to the usage of the same managed Lambda Runtime • Reduce cold starts to several hundred milliseconds and very low memory consumption • Lots of experimentation with additional build arguments like “initialize-at-runtime” or “delay- class-initialization-to-runtime“ required Vadym Kazulkin @VKazulkin , ip.labs GmbH Source: https://stackoverflow.com/questions/63328298/how-do-you-debug-a-no-instances-of-are-allowed-in-the-image-heap-when-buil
  • 91.
    Personal Recommendations (highlyopinionated) • By default use Java Long Term Support Version with Amazon Corretto and your favourite Framework (Micronaut, Quarkus) • If your function needs constantly low response times for the known period of time ? • Use Provisioned Concurrency additionally • If your function needs constantly low response time and low cost is a requirement? • Use GraalVM Native Image with your favourite Framework (Micronaut, Quarkus, Spring Boot GraalVM Native) and AWS Lambda Custom Runtime • Define 2 pipelines: „standard“ for quick and local testing and „native image“ for production usage • If you don‘t want to miss 3 years of innovation and use the newest Java Version? • Use Lambda Container Image Support Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 92.
    Where AWS canhelp (highly opinionated again) 1/2 • Optimize Amazon Corretto for Lambda with Java on AWS • Rethink garbage collection • Find trade-off between increase of memory consumption because of the garbage and performance loss of GC algorithms for the short running Lambda functions (max. 1 hour or so) • Rethink HotSpot Just-In-Time Compiler optimizations • Most JIT optimizations apply by default only after multiple thousands executions (i.e. 10.000) of the function (so never), so find the write configuration for the short running Lambda functions • Provide support for jlink • jlink is tool that generates a custom Java runtime image that contains only the platform modules that are required for a given application • See what Oracle introduced for the Fn Project Vadym Kazulkin @VKazulkin , ip.labs GmbH Sources: „Guide to jlink” https://www.baeldung.com/jlink „Open Source. Container-native. Serverless platform“ https://fnproject.io/
  • 93.
    Try it yourselves •Micronaut • https://github.com/micronaut-guides/micronaut-function-aws-lambda • Quarkus • https://github.com/JosemyDuarte/quarkus-terraform-lambda-demo/tree/dynamo-terraform • Spring Boot • https://github.com/spring-projects-experimental/spring-graalvm-native/tree/master/spring- graalvm-native-samples/function-aws • Misc • https://github.com/awslabs/aws-serverless-java-container/tree/master/samples Vadym Kazulkin @VKazulkin , ip.labs GmbH
  • 94.
    What about ProjectHelidon? Source: https://helidon.io
  • 95.
  • 97.