High performance Serverless Java on AWS at We Are Developers 2024

VadymKazulkin 19 views 88 slides Sep 21, 2024
Slide 1
Slide 1 of 88
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88

About This Presentation

Java is for many years one of the most popular programming languages, but it used to have hard times in the Serverless community. Java is known for its high cold start times and high memory footprint, comparing to other programming languages like Node.js and Python. In this talk I'll look at the...


Slide Content

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
High PerformanceServerless Java on AWS
Vadym Kazulkin, ip.labs, WeAreDevelopers - Java Developer Day, 11 September 2024

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Vadym Kazulkin
ip.labs GmbH Bonn, Germany
Co-Organizer of the Java User Group Bonn
[email protected]
@VKazulkin
https://dev.to/vkazulkin
https://github.com/Vadym79/
https://de.slideshare.net/VadymKazulkin/
https://www.linkedin.com/in/vadymkazulkin
https://www.iplabs.de/
Contact

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
About ip.labs
High performance Serverless Java on AWS4

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Java Popularity
5 High performance Serverless Java on AWS Vadym Kazulkin | @VKazulkin | ip.labs GmbH

Vadym Kazulkin| @VKazulkin |ip.labsGmbH6 High performance Serverless Java on AWS
https://distantjob.com/blog/programming-languages-rank/

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Life of the Java
(Serverless)
Developer
on AWS
7 High performance Serverless Java on AWS

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪Corretto Java 8
▪With extended long-term support until 2026
▪Coretto Java 11 (since 2019)
▪Coretto Java 17 (since April 2023)
▪Corretto Java 21(since November 2023)
▪Only Long Term Support (LTS) by AWS
AWS Java Versions Support
High performance Serverless Java on AWS8

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
AWS People behind Amazon Corretto
High performance Serverless Java on AWS9

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
…but
serverless
adoption of
Java looks like
this!
10 High performance Serverless Java on AWS
Java is a very
fast and
mature
programming
language…

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Percent of AWS Lambda Invocations by Language
2021 vs 2023
High performance Serverless Java on AWS11
https://www.datadoghq.com/state-of-serverless-2021
https://www.datadoghq.com/state-of-serverless/
PHYTON IS THE
MOST POPULAR
LAMDA
RUNTIME

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Developers love Java and will be happy
to use it for Serverless applications
But what are the challenges ?
High performance Serverless Java on AWS12

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Creating AWS Lambda with Java runtime
High performance Serverless Java on AWS13
Full CPU access only
approx. at1.8 GB
memory allocated

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪“cold start” times (latencies)
▪memory footprint (high cost in AWS)
Serverless with Java Challenges
High performance Serverless Java on AWS14

Vadym Kazulkin| @VKazulkin |ip.labsGmbH16 High performance Serverless Java on AWS
Challenge No. 1
A Big Cold-Start

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Lambda function lifecycle –a full cold start
How to develop, run and optimize Spring Boot 3 application on AWS Lambda17
Sources: Ajay Nair „Become a ServerlessBlack 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

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪Start Firecracker VM (execution environment)
▪AWS Lambda starts the Java runtime
▪Java runtime loads and initializes Lambda function code
(Lambda handler Java class)
▪Class loading
▪Static initializer block of the handler class is executed (i.e. AWS service client
creation)
▪Runtime dependency injection
▪Just-in-Time (JIT) compilation
▪Init-phase has full CPU access up to 10 seconds for free for the managed execution
environments)
▪Lambda invokes the handler method
High performance Serverless Java on AWS18
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
Lambda function lifecycle

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
public class GetProductByIdHandlerimplements
RequestHandler<APIGatewayProxyRequestEvent, Optional<Product>> {
private static final ProductDaoproductDao= new DynamoProductDao();
@Override
public Optional<Product> handleRequest(APIGatewayProxyRequestEventevent,
Context context) {
String id = event.getPathParameters().get("id");
return productDao.getProduct(id);
}}
AWS Lambda Function with Java runtime
How to develop, run and optimize Spring Boot 3 application on AWS Lambda19
Invocation of the
handeRequest method is
the warm start

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Demo Application
High performance Serverless Java on AWS20
https://github.com/Vadym79/AWSLambdaJavaSnapStart
▪Lambda has 1024 MB memory setting
▪Lambda uses x86 architecture
▪Default (Apache) Http Client for
communication with DynamoDB
▪18 MB artifact size, , all dependencies in
the POM file
▪Java compilation option -
XX:+TieredCompilation -
XX:TieredStopAtLevel=1
▪Info about the experiments:
▪Approx. 1 hour duration
▪Approx. first* 100 cold starts
▪Approx. first 100.000 warm starts
*after Lambda function being re-deployed

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k
Measurements in
ms
p50 p75 p90 p99 p99.9 max
Amazon Corretto
Java 21 cold start
3158 3214 3270 3428 3601 3725
Amazon Corretto
Java 21 warm start
5,77 6,50 7,81 20,65 90,20 1423,63
Cold and warm starts with Java 21

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Latency is the amount of time it takes from when a request
is made by the user to the time it takes for the response to
get back to that user
Latency
How to develop, run and optimize Spring Boot 3 application on AWS Lambda22

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
For our application:
Overall latency =
t(client to create and send request to the API Gateway endpoint) +
t(API Gateway to process request and invoke Lambda function) +
t(Lambda cold start time)+
t(Lambda warm start time) +
t(API Gateway to processes Lambda response and sends it back to the client) +
t(client to receive and process response from the API Gateway endpoint)
Latency
How to develop, run and optimize Spring Boot 3 application on AWS Lambda23
involves DNS resolve
and SSL handshake

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪AWS SnapStart
▪GraalVM(Native Image)
Options To Reduce Cold Start Time
High performance Serverless Java on AWS24

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k
Measurements in
ms
p50 p75 p90 p99 p99.9 max
Amazon Corretto
Java 21 cold start
3158 3214 3270 3428 3601 3725
Amazon Corretto
Java 21 warm start
5,77 6,50 7,81 20,65 90,20 1423,63
Cold and warm starts with Java 21

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
AWS Lambda
SnapStart
33 How to develop, run and optimize Spring Boot 3 application on AWS Lambda

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
„AWS Lambda SnapStart „ series
How to develop, run and optimize Spring Boot 3 application on AWS Lambda35
https://dev.to/vkazulkin/measuring-java-11-lambda-cold-starts-with-snapstart-part-1-first-impressions-30a4
Article series covers they why and what
behind Lambda SnapStartand priming
techniques including measurements for cold
and warm starts with different settings for
▪Java 11
▪Java 17
▪Java 21
▪Micronaut
▪Quarkus
▪Spring Boot 2.7

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪Lambda SnapStartfor Java can improve startup performance for latency-
sensitive applications
▪SnapStartis fully managed
AWS Lambda SnapStart
How to develop, run and optimize Spring Boot 3 application on AWS Lambda36
https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
AWS SnapStartDeployment & Invocation
How to develop, run and optimize Spring Boot 3 application on AWS Lambda37
https://dev.to/vkazulkin/measuring-java-11-lambda-cold-starts-with-snapstart-part-1-first-impressions-30a4
https://aws.amazon.com/de/blogs/compute/using-aws-lambda-snapstart-with-infrastructure-as-code-and-ci-cd-pipelines/

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪Currently available for Lambda managed Java Runtimes (Java 11, 17 and 21)
▪Not available for all other Lambda runtimes:
▪Docker Container Image
▪Custom (Lambda) Runtime (a way to ship GraalVMNative Image)
AWS Lambda SnapStart
How to develop, run and optimize Spring Boot 3 application on AWS Lambda38
https://github.com/Vadym79/AWSLambdaJavaDockerImage/

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
AWS SnapStartDeployment & Invocation
High performance Serverless Java on AWS47
https://aws.amazon.com/de/blogs/compute/reducing-java-cold-starts-on-aws-lambda-functions-with-snapstart/
Vadym Kazulkin@VKazulkin, ip.labsGmbH
Lambda uses the
CRaCAPIsfor
runtime hooks
for Priming
C
Create
Snapshot
Firecracker microVM
create & restore snapshot

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪Primedependencies during initialization phase (when it worth doing)
▪„Fake“ the calls to pre-initialize „some other expensive stuff“ (this technique is called
Priming)
▪In case of DynamoDB client put the following code outside of the handler method to
pre-initialize the HTTP Client and Jackson Marshaller:
DynamoDbClientclient = DynamoDbClientBuilder.builder().region(Regions.US_WEST_2).build();
GetItemResponse getItemResponse = client.getItem(GetItemRequest.builder()
.key(Map.of("PK", AttributeValue.builder().s(id).build()))
.tableName(PRODUCT_TABLE_NAME).build());
……
Best Practices & Recommendations
How to develop, run and optimize Spring Boot 3 application on AWS Lambda50
invocation forces HTTP Client and Jackson
Marshallers to pre-initialize
getProductById (int id)
method

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
AWS SnapStartEnabled with Java Priming
High performance Serverless Java on AWS51
https://dev.to/aws-builders/measuring-java-11-lambda-cold-starts-with-snapstart-part-5-priming-end-to-end-latency-and-deployment-time-jem
<dependency>
<groupId>io.github.crac</groupId>
<artifactId>org-crac</artifactId>
<version>0.1.3</version>
</dependency>
if a refer to Priming in this
talk, I mean concretely
DynamoDB request
invocation priming

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Lambda SnapStart Priming Guide
High performance Serverless Java on AWS52
guide aims to explain techniques
for priming Java applications.
It assumes a base understanding
of AWS Lambda, Lambda
SnapStart, and CRaC.
https://github.com/marksailes/snapstart-priming-guide

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
500
1000
1500
2000
2500
3000
3500
4000
w/o SnapStart with SnapStart w/o Priming with SnapStart with Priming
Cold starts of Lambda functionwith Java 21 runtime with
1024 MB memory setting, Apache Http Client, compilation
-XX:+TieredCompilation -XX:TieredStopAtLevel=1
p50p75p90p99p99.9max
https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0,00
5,00
10,00
15,00
20,00
25,00
w/o SnapStart with SnapStart w/o Priming with SnapStart with Priming
Warm starts of Lambda functionwith Java 21 runtimewith
1024 MB memory setting, Apache Http Client compilation -
XX:+TieredCompilation -XX:TieredStopAtLevel=1
p50p75p90p99
https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
200
400
600
800
1000
1200
1400
1600
w/o SnapStart with SnapStart w/o Priming with SnapStart with Priming
Warm starts of Lambda functionwith Java 21 runtimewith
1024 MB memory setting, Apache Http Client compilation -
XX:+TieredCompilation -XX:TieredStopAtLevel=1
p99.9max
https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Demo Application
High performance Serverless Java on AWS57
https://github.com/Vadym79/AWSLambdaJavaSnapStart
▪Lambda has 1024 MB memory setting
▪Lambda uses x86 architecture
▪Default (Apache) Http Client for
communication with DynamoDB
▪18 MB artifact size, , all dependencies in
the POM file
▪Java compilation option -
XX:+TieredCompilation -
XX:TieredStopAtLevel=1
▪Info about the experiments:
▪Approx. 1 hour duration
▪Approx. first* 100 cold starts
▪Approx. first 100.000 warm starts
*after Lambda function being re-deployed

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Lambda Memory Setting
High performance Serverless Java on AWS58
ONE SECOND ONE GB

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k
0
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
256 MB 512 MB 768 MB 1024 MB 1536 MB 2048 MB
Cold starts of Lambda functionwith Java 21 runtime
without SnapStartfor different memory settings
p50p75p90p99p99.9max
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
1000
2000
3000
4000
5000
6000
7000
8000
256 MB 512 MB 768 MB 1024 MB 1536 MB 2048 MB
Cold starts of Lambda functionwith Java 21 runtimewith
SnapStartwithout Primingfor different memory settings
p50p75p90p99p99.9max
https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
200
400
600
800
1000
1200
1400
1600
1800
2000
256 MB 512 MB 768 MB 1024 MB 1536 MB 2048 MB
Cold starts of Lambda functionwith Java 21 runtimewith
SnapStartwith Primingfor different memory settings
p50p75p90p99p99.9max
https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Lambda Deployment Artifact Size
High performance Serverless Java on AWS65

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
500
1000
1500
2000
2500
3000
3500
w/o SnapStart with SnapStart w/o Priming with SnapStart with Priming
Cold starts of Lambda functionwith Java 21 runtime
using deployment artifact sizes for p90
p90 smallp90 medium p90 big
ms
https://dev.to/aws-builders/aws-snapstart-part-11-measuring-cold-starts-with-java-21-using-different-deployment-artifact-sizes-4g29
▪Small -137 KB (“Hello World”)
▪Medium –18 MB (our sample
application)
▪Big -50 MB (our sample
application + additional
dependencies other to AWS
services)

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪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 i.e. test frameworks like Junit
Best Practices & Recommendations
High performance Serverless Java on AWS67
<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.22.2</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.22.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Using Lambda Layers
High performance Serverless Java on AWS68
https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html
https://docs.aws.amazon.com/lambda/latest/dg/java-layers.html#java-layer-compatibility
•aws-lambda-java-core
•aws-lambda-java-events
•org-crac
•slf4j-simple
•jackson-dataformat-xml
Type: AWS::Serverless::Function
Properties:
FunctionName: MyFunctionWithLayer
Layers:
- !Sub
arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:a
ws-pure-java-21-common-lambda-layer:1
Handler:
software…handler.GetProductByIdHandler::handleRequest

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
500
1000
1500
2000
2500
3000
3500
4000
w/o SnapStart with SnapStart w/o Priming with SnapStart with Priming
Cold starts of Lambda functionwith Java 21 runtimewith
and without the usage of the Lambda Layer options for
p90
all dependencies in the POM fileusing Lambda Layer
https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/pure-lambda-21-with-common-layer
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Java Compilation
High performance Serverless Java on AWS71
https://www.geeksforgeeks.org/what-are-the-roles-of-java-compiler-and-interpreter/

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Java Compilation Options
High performance Serverless Java on AWS72
Mark Sailes: "Optimizing AWS Lambda function performance for Java” https://aws.amazon.com/de/blogs/compute/optimizing-aws-lambda-function-performance-for-java/

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
1000
2000
3000
4000
5000
6000
w/o SnapStart with SnapStart w/o Priming with SnapStart with Priming
Cold starts of Lambda functionwith Java 21 runtime
using different compilation options for p90
Tiered StopAtLevel 1StopAtLevel 2StopAtLevel 3StopAtLevel 4
https://dev.to/aws-builders/aws-snapstart-part-14-measuring-cold-and-warm-starts-with-java-21-using-different-compilation-options-el4
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0,00
2,00
4,00
6,00
8,00
10,00
12,00
w/o SnapStart with SnapStart w/o Priming with SnapStart with Priming
Warm starts of Lambda functionwith Java 21 runtime
using different compilation options for p90
Tiered StopAtLevel 1StopAtLevel 2StopAtLevel 3StopAtLevel 4
https://dev.to/aws-builders/aws-snapstart-part-14-measuring-cold-and-warm-starts-with-java-21-using-different-compilation-options-el4
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Java Garbage Collection Algorithms
High performance Serverless Java on AWS75
•Tested with default GC algorithms settings
•G1 default GC produces the best results
for cold and warm starts without and with
SnapStart (and priming) followed by :
•Parallel GC
•Shenandoah GC
•Couldn‘tmakeitworkwithZGC
(generational) GC

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
HTTP Clients to connect to DynamoDB
High performance Serverless Java on AWS76

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
DynamoDbClientclient = DynamoDbClient.builder().region(Region.EU_CENTRAL_1)
. httpClient(ApacheHttpClient.create())
//.httpClient(UrlConnectionHttpClient.create())
//.httpClient(AwsCrtHttpClient.create())
.build();
Add dependency to the synchronous HTTP Client in use to pom.xml, i.e.
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-crt-client</artifactId>
</dependency>
Setting synchronous DynamoDB Client
High performance Serverless Java on AWS77

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
500
1000
1500
2000
2500
3000
3500
w/o AWS SnapStart with AWS SnapStart w/o priming with AWS SnapStart with priming
Cold starts of Lambda function with Java 21 runtime and StopAtLevel1 compilation
and different synchronousHTTP Clients for p90
URL Connection Apache AWS CRT
https://dev.to/aws-builders/aws-snapstart-part-15-measuring-cold-and-warm-starts-with-java-21-using-different-synchronous-http-clients-579o
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
HTTP client recommendations
High performance Serverless Java on AWS84
https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/http-configuration.html

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Lambda x86_64 vs arm64 architecture
High performance Serverless Java on AWS85
https://aws.amazon.com/de/about-aws/whats-new/2024/07/aws-lambda-snapstart-java-functions-arm64-architecture/

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Lambda x86_64 vs arm64 architecture
High performance Serverless Java on AWS86
https://aws.amazon.com/lambda/pricing/?nc1=h_ls
For the same memory setting Lambda and
Lambda function execution duration the choice of
arm64 over x86_64 architecture is approx. 25%
cheaper

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
500
1000
1500
2000
2500
3000
3500
4000
4500
w/o AWS SnapStart with AWS SnapStart w/o priming with AWS SnapStart with priming
Cold starts of Lambda functionwith Java 21 runtime with
1024 MB memory setting, Apache Http Client, compilation
-XX:+TieredCompilation -XX:TieredStopAtLevel=1 for p90
x86_64arm64
ms
https://dev.to/aws-builders/aws-snapstart-part-16-measuring-cold-and-warm-starts-with-java-21-using-different-asynchronous-http-clients-4n2
+8,6%
+23,9%
+24,8%

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
6,40
6,60
6,80
7,00
7,20
7,40
7,60
7,80
8,00
w/o AWS SnapStart with AWS SnapStart w/o priming with AWS SnapStart with priming
Warm starts of Lambda functionwith Java 21 runtime with
1024 MB memory setting, Apache Http Client, compilation
-XX:+TieredCompilation -XX:TieredStopAtLevel=1 for p90
x86_64arm64
ms
https://dev.to/aws-builders/aws-snapstart-part-16-measuring-cold-and-warm-starts-with-java-21-using-different-asynchronous-http-clients-4n2
+9,1%
+10,1%
+4,8%

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Demo Application
High performance Serverless Java on AWS89
https://github.com/Vadym79/AWSLambdaJavaSnapStart
▪Lambda has 1024 MB memory setting
▪Lambda uses x86 architecture
▪Default (Apache) Http Client for
communication with DynamoDB
▪18 MB artifact size, , all dependencies in
the POM file
▪Java compilation option -
XX:+TieredCompilation -
XX:TieredStopAtLevel=1
▪Info about the experiments:
▪Approx. 1 hour duration
▪Approx. first* 100 cold starts
▪Approx. first 100.000 warm starts
*after Lambda function being re-deployed

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
AWS SnapStartDeployment & Invocation
High performance Serverless Java on AWS90
https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html
https://aws.amazon.com/de/blogs/compute/reducing-java-cold-starts-on-aws-lambda-functions-with-snapstart/
Firecracker microVM
create & restore
snapshot

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
AWS SnapStarttiered cache
How to develop, run and optimize Spring Boot 3 application on AWS Lambda91
https://dev.to/aws-builders/aws-snapstart-part-17-impact-of-the-snapshot-tiered-cache-on-the-cold-starts-with-java-21-52ef
•Due to the effect of
snapshot tiered cache, cold
start times reduces with the
number of invocations
•After certain number of
invocations reached the
cold start times becomes
stable

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
500
1000
1500
2000
2500
with SnapStart w/o Primingwith SnapStart w/o Priming
(last 50)
with SnapStart with
Priming
with SnapStart with
Priming (last 50)
Cold starts of all approx.100 and last 50Lambda functionswith
Java 21 runtime with
1024 MB memory setting, Apache Http Client, compilation
-XX:+TieredCompilation -XX:TieredStopAtLevel=1
p50p75p90p99p99.9max
https://dev.to/aws-builders/aws-snapstart-part-17-impact-of-the-snapshot-tiered-cache-on-the-cold-starts-with-java-21-52ef
Due to the effect of
snapshot tiered cache,
cold start times
reduces with the
number of invocations
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪Deployment with SnapStartenabled takes more than 2-2,5 minutes
additionally
▪Snapshot is deleted from cache if Lambda function is not invoked for 14
days
▪SnapStartcurrently does not support :
▪Provisioned concurrency
▪arm64 architecture (supports only x86). Supports arm64 architecture since
18 July 2024
▪Amazon Elastic File System (Amazon EFS)
▪Ephemeral storage greater than 512 MB
AWS SnapStart Challenges & Limitations
High performance Serverless Java on AWS94
https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
AWS Lambda under the Hood
High performance Serverless Java on AWS95
https://www.infoq.com/articles/aws-lambda-under-the-hood/
https://www.infoq.com/presentations/aws-lambda-arch/

Vadym Kazulkin| @VKazulkin |ip.labsGmbH96 High performance Serverless Java on AWS

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪Low footprint ahead-of-time mode for JVM-based languages
▪High performance for all languages
▪Convenient language interoperability and polyglot tooling
GraalVMGoals
High performance Serverless Java on AWS97
Source: „Everything you need to know about GraalVMby Oleg Šelajev& Thomas Wuerthinger” https://www.youtube.com/watch?v=ANN9rxYo5Hg

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
GraalVMArchitecture
High performance Serverless Java on AWS98

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
GraalVMAhead-of-Time Compilation
High performance Serverless Java on AWS99
Source: Oleg Šelajev, Thomas Wuerthinger, Oracle: “Deep dive into using GraalVMfor Java and JavaScript”
https://www.youtube.com/watch?v=a-XEZobXspo

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Compilationintoa native executableusing GraalVMreduces
▪“cold start” times
▪memory footprint
by order of magnitude compared to running on JVM.
GraalVMNative Image
High performance Serverless Java on AWS101

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪AWS doesn’t provide GraalVM(Native Image) as Java Runtime out of the box
▪AWS provides Custom Runtime Option
Current Challenges with Native Executable using
GraalVM
High performance Serverless Java on AWS102

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Custom Lambda Runtimes
High performance Serverless Java on AWS103
https://github.com/Vadym79/AWSLambdaGraalVMNativeImage

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
500
1000
1500
2000
2500
3000
3500
4000
w/o SnapStart with SnapStart w/o Primingwith SnapStart with
Priming
GraalVM Native Image
Cold starts of Lambda functionwith Java 21 runtime with.
1024 MB memory setting, Apache Http Client
p50p75p90p99p99.9max
https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0,00
5,00
10,00
15,00
20,00
25,00
w/o SnapStart with SnapStart w/o
Priming
with SnapStart with
Priming
GraalVM Native Image
Warm starts of Lambda functionwith Java 21 runtime with
1024 MB memory setting, Apache Http Client
p50p75p90p99
https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
200
400
600
800
1000
1200
1400
1600
w/o SnapStart with SnapStart w/o Primingwith SnapStart with
Priming
GraalVM Native Image
Max warm starts of Lambda functionwith Java 21 runtime with
1024 MB memory setting, Apache Http Client
p99.9max
https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Lambda Memory Setting for Custom Runtime (GraalVM
Native Image)
High performance Serverless Java on AWS109
ONE SECOND ONE GB

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0
200
400
600
800
1000
1200
256 MB 512 MB 768 MB 1024 MB 1280 MB 1536 MB
Cold starts of Lambda functionwith GraalVM 21 Native
Imagefor different memory settings
p50p75p90p99p99.9max
https://github.com/Vadym79/AWSLambdaGraalVMNativeImage/tree/master/pure-lambda-graalvm-jdk-21-native-image
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0,00
2,00
4,00
6,00
8,00
10,00
12,00
14,00
16,00
18,00
20,00
256 MB 512 MB 768 MB 1024 MB 1280 MB 1536 MB
Warm starts of Lambda functionwith GraalVM 21 Native
Imagefor different memory settings
p50p75p90p99
https://github.com/Vadym79/AWSLambdaGraalVMNativeImage/tree/master/pure-lambda-graalvm-jdk-21-native-image
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
0,00
50,00
100,00
150,00
200,00
250,00
300,00
350,00
400,00
450,00
500,00
256 MB 512 MB 768 MB 1024 MB 1280 MB 1536 MB
Warm starts of Lambda functionwith GraalVM 21 Native
Imagefor different memory settings
p99.9max
https://github.com/Vadym79/AWSLambdaGraalVMNativeImage/tree/master/pure-lambda-graalvm-jdk-21-native-image
ms

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪You can run into errors when application
is running
GraalVM runtime class loading
High performance Serverless Java on AWS113
https://github.com/Vadym79/AWSLambdaGraalVMNativeImage/blob/master/pure-lambda-graalvm-jdk-21-native-image/src/main/reflect.json

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Frameworks and libraries Ready for GraalVMNative Image
High performance Serverless Java on AWS114
https://www.graalvm.org/native-image/libraries-and-frameworks/

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪GraalVMis really powerful and has a lot of potential
▪GraalVMNative Image improves cold starts and memory footprint
significantly
▪GraalVMNative Image is currently not without challenges
▪AWS Lambda Custom Runtime requires Linux executable only
▪Building Custom Runtime requires some additional effort
▪e.g. you need a scalable CI/CD pipeline to build memory-intensive native image
▪Build time is a factor (2-3 minutes for this application)
▪You pay for the init-phase of the function packaged as AWS Lambda
Custom and Docker Runtime
▪Init-phase is free for the managed runtimes like Java 11, Java17 and Java 21
GraalVM Conclusion
High performance Serverless Java on AWS115

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
“Spring Boot 3 application on AWS Lambda” series
High performance Serverless Java on AWS118
Article series covers different
ways to write Spring Boot 3
application on AWS Lambda
▪AWS Serverless Java Container
▪AWS Lambda Web Adapter
▪Spring Cloud Functions
▪Custom Docker Image
▪GraalVMNative Image
Cold and warm start time
measurements are also provided
https://dev.to/aws-builders/spring-boot-3-application-on-aws-lambda-part-1-introduction-to-the-series-2m5g

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
“Data API for Amazon Aurora Serverless v2
with AWS SDK for Java” series
High performance Serverless Java on AWS119
Article series also covers
cold and warm start time
measurements and
optimization techniques
https://dev.to/aws-builders/data-api-for-amazon-aurora-serverless-v2-with-aws-sdk-for-java-part-1-introduction-and-set-up-of-the-sample-application-3g71

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
▪With AWS SnapStartand GraalVMNative Image you can reduce cold
start timesof the AWS Lambda with Java 21 runtime to the acceptable
values
▪If you’re willing to accept slightly higher cold and warm start times for
certain the Lambda function(s) and solid priming is applicable -> use fully
managed AWS SnapStartwith priming
▪If a very high performance for certain the Lambda function(s) is really
crucial for your business -> go for GraalVMNative Image
Wrap up and personal suggestions
How to develop, run and optimize Spring Boot 3 application on AWS Lambda120

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Project Leyden
High performance Serverless Java on AWS121
The primary goal of
this Project is to
improve the startup
time, time to peak
performance, and
footprint of Java
programs.
https://www.youtube.com/watch?v=teXijm79vno
https://openjdk.org/projects/leyden/

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
Word of caution
High performance Serverless Java on AWS122
Re-measure for your use case!
Even with my examples measurements
might already produce different results due
to:
▪Lambda Amazon CorrettoJava 21
managed runtime minor version changes
▪Lambda SnapStartsnapshot create and
restore improvements
▪Firecracker VM improvements
▪GraalVM(major and minor version) and
Native Image improvements
▪There are still servers behind Lambda
▪Java Memory Model impact (L or RAM
caches hits and misses)

Vadym Kazulkin| @VKazulkin |ip.labsGmbH
FAQ Ask me Anything
High performance Serverless Java on AWS123

Vadym Kazulkin| @VKazulkin |ip.labsGmbH124 High performance Serverless Java on AWS
Thank you