SlideShare a Scribd company logo
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
How to develop, run and optimize Spring Boot 3
(web) application on AWS Lambda
Vadym Kazulkin, ip.labs, Java Forum Stuttgart, 31 July 2024
How to develop,
run and optimize
Spring Boot 3
1
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Vadym Kazulkin
ip.labs GmbH Bonn, Germany
Co-Organizer of the Java User Group Bonn
v.kazulkin@gmail.com
@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.labs GmbH
About ip.labs
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
4
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ Your organization has profound Java and Spring Boot development skills
▪ You have an existing Spring Boot 3 (Web) application, or you want to develop
a new one
Challenge
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
7
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ You want to use AWS Lambda for hosting your Spring Boot 3 web application
▪ To benefit from the advantages of the Serverless architectures
▪ Scalability
▪ Less focus on classical operational tasks
▪ API Gateway and Lambda offer fully managed approach
▪ Requires securing the application only
▪ With less possible engineering effort in the business logic
Challenge
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
8
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ It’s a bad idea
▪ Performance is bad
▪ Too much runtime annotation processing, reflection, dynamic class loading used
▪ High cold (application start up) and warm start times
Most frequent replies and assumptions
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
9
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ I’ll use NoSQL Amazon DynamoDB for my examples
▪ You can make it work with the same introduced approaches by
managing your own database or using Amazon RDS (i.e. PostgreSQL)
▪ Challenges around managing database connection. Possible solutions are:
▪ Using Amazon RDS Proxy and putting Lambda functions into VPC
▪ Using (PostgreSQL) Aurora Serverless v2 Data API
▪ You need to re-measure performance
Database usage
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
10
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Demo Application
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
12
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot
▪ PUT
https://{YOUR_API_GATEWAY_URL}/prod
/products/
▪ GET
https://{YOUR_API_GATEWAY_URL}/prod
/products/ {productId}
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Spring Boot 3 Application class
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
13
https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
public record Product(String id, String name, BigDecimal price) { }
Product Entity class
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
14
https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@Repository
public class DynamoProductDao implements ProductDao {
private static final String PRODUCT_TABLE_NAME =
System.getenv("PRODUCT_TABLE_NAME");
private static final DynamoDbClient dynamoDbClient =
DynamoDbClient.builder().build();
@Override
public void putProduct(Product product) {
dynamoDbClient.putItem(PutItemRequest.builder()
.tableName(PRODUCT_TABLE_NAME)
.item(ProductMapper.productToDynamoDb(product))
.build());
}
Product Repository DAO class
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
15
https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@Override
public Optional<Product> getProduct(String id) {
GetItemResponse getItemResponse =
dynamoDbClient.getItem(GetItemRequest.builder().key(Map.of("PK",
AttributeValue.builder().s(id).build())).tableName(PRODUCT_TABLE_NAME).build())
;
if (getItemResponse.hasItem()) {
return
Optional.of(ProductMapper.productFromDynamoDB(getItemResponse.item()));
} else {
return Optional.empty();
}
}
..
}
Product Repository DAO class
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
16
https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ AWS Serverless Java Container
▪ AWS Lambda Web Adapter
▪ Spring Cloud Function (on AWS Lambda)
▪ Custom (Docker) Container Image
▪ Optimization techniques
▪ AWS (Lambda) SnapStart
▪ GraalVM Native Image
▪ Cold and warm start measurements for all approaches and optimizations
Agenda
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
17
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS Serverless Java Container
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
18
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ The AWS Serverless Java Container makes it
easier to run Java applications written with
frameworks such as Struts, Spring, Spring Boot
2 and 3, or JAX-RS/Jersey in Lambda.
▪ The container provides adapter logic to
minimize code changes. Incoming events are
translated to the Jakarta EE Servlet specification
so that frameworks work as before
AWS Serverless Java Container
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
19
https://github.com/aws/serverless-java-container/
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@RestController @EnableWebMvc
public class ProductController {
@Autowired
private DynamoProductDao productDao;
@RequestMapping(path = “/products/{id}”, method = RequestMethod.GET, produces =
MediaType.APPLICATION_JSON_VALUE)
public Optional<Product> getProductById(@PathVariable(“id”) String id) {
return productDao.getProduct(id);
}
@RequestMapping(path = “/products/{id}”, method = RequestMethod.PUT, consumes =
MediaType.APPLICATION_JSON_VALUE)
public void createProduct(@PathVariable(“id”) String id, @RequestBody Product product) {
product.setId(id);
productDao.putProduct(product);
}
Spring Boot 3 Product (Rest)Controller class
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
20
https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
pom.xml
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-springboot3</artifactId>
<version>2.0.0</version>
</dependency>
AWS Serverless Java Container
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
21
https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Automatic proxying through AWS Serverless Java Container
AWS SAM Template (template.yaml)
GetProductByIdFunction:
Type: AWS::Serverless::Function
Properties:
Handler: com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler
CodeUri: target/***.jar
Runtime: java21
MemorySize: 1024
Environment:
Variables:
MAIN_CLASS: com.amazonaws.serverless.sample.springboot3.Application
AWS Serverless Java Container
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
22
https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Self-written Lambda function
AWS SAM Template (template.yaml)
GetProductByIdFunction:
Type: AWS::Serverless::Function
Properties:
Handler: software.amazonaws.example.product.handler.StreamLambdaHandler::handleRequest
CodeUri: target/***.jar
Runtime: java21
MemorySize: 1024
AWS Serverless Java Container
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
23
https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
public class StreamLambdaHandler implements RequestStreamHandler {
SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler =
SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context
context) throws IOException {
handler.proxyStream(inputStream, outputStream, context);
}
}
AWS Serverless Java Container
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
24
https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS Lambda Web Adapter
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
25
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ AWS Lambda Web Adapter is a tool written in Rust programming language
to run web applications on AWS Lambda.
▪ It allows developers to build web apps (HTTP API) with familiar frameworks
(e.g. Express.js, Next.js, Flask, Spring Boot, ASP.NET and Laravel, anything
that speaks HTTP 1.1/1.0) and run it on AWS Lambda.
▪ The same Docker image can run also on Amazon EC2, AWS Fargate, and
local computers
AWS Lambda Web Adapter
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
26
https://github.com/awslabs/aws-lambda-web-adapter
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ Run web applications on AWS Lambda
▪ Supports Amazon API Gateway Rest API and HTTP
API endpoints, Lambda Function URLs, and
Application Load Balancer
▪ Supports all non-HTTP event triggers, such as SQS,
SNS, S3, DynamoDB, Kinesis, Kafka, EventBridge,
and Bedrock Agents
AWS Lambda Web Adapter
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
27
https://github.com/awslabs/aws-lambda-web-adapter
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Can be deployed in several ways:
▪ Lambda functions packaged as Docker Images
▪ Lambda functions packaged as OCI Images
▪ Lambda functions packaged as Zip package for
AWS managed runtimes and attached as Lambda
layer to your Lambda function
AWS Lambda Web Adapter
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
28
https://github.com/awslabs/aws-lambda-web-adapter
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@RestController @EnableWebMvc
public class ProductController {
@Autowired
private DynamoProductDao productDao;
@RequestMapping(path = “/products/{id}”, method = RequestMethod.GET, produces =
MediaType.APPLICATION_JSON_VALUE)
public Optional<Product> getProductById(@PathVariable(“id”) String id) {
return productDao.getProduct(id);
}
@RequestMapping(path = “/products/{id}”, method = RequestMethod.PUT, consumes =
MediaType.APPLICATION_JSON_VALUE)
public void createProduct(@PathVariable(“id”) String id, @RequestBody Product product) {
product.setId(id);
productDao.putProduct(product);
}
Spring Boot 3 Product (Rest)Controller class
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
29
https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS SAM Template (template.yaml)
GetProductByIdFunction:
Type: AWS::Serverless::Function
Properties:
Handler: run.sh
CodeUri: target/***.jar
Runtime: java21
MemorySize: 1024
Layers:
- !Sub arn:aws:lambda:${AWS::Region}:753240598075:layer:LambdaAdapterLayerX86:20
Environment:
Variables:
AWS_LAMBDA_EXEC_WRAPPER: /opt/bootstrap
AWS Lambda Web Adapter
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
30
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-lambda-web-adapter
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
run.sh
#!/bin/sh
exec java -cp "./:lib/*" "software.amazonaws.Application"
AWS Lambda Web Adapter
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
31
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-lambda-web-adapter
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS SAM Template (template.yaml)
GetProductByIdFunction:
Type: AWS::Serverless::Function
Properties:
Handler: run.sh
CodeUri: target/***.jar
Runtime: java21
MemorySize: 1024
Layers:
- !Sub arn:aws:lambda:${AWS::Region}:753240598075:layer:LambdaAdapterLayerX86:20
Environment:
Variables:
AWS_LAMBDA_EXEC_WRAPPER: /opt/bootstrap
AWS Lambda Web Adapter
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
32
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-lambda-web-adapter
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS_LAMBDA_EXEC_WRAPPER : /opt/bootstrap
▪ When we add a layer to a Lambda function, Lambda extracts the layer contents
into the /opt directory in our function’s execution environment
▪ All natively supported Lambda runtimes include paths to specific directories
within the /opt directory
▪ This gives our Lambda function access to our layer content
AWS Lambda Web Adapter
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
33
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-lambda-web-adapter
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Spring Cloud Function for AWS Lambda
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
34
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ Promote the implementation of business logic via functions.
▪ A simple function application (in context or Spring) is an application that
contains beans of type Supplier, Java 8 Function interface or Consumer.
Spring Cloud Function for AWS Lambda
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
35
https://spring.io/projects/spring-cloud-function https://spring.io/projects/spring-cloud-aws
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ With Spring Cloud Function on AWS Lambda, AWS adapter takes a Spring
Cloud Function app and converts it to a form that can run in AWS Lambda
▪ With AWS it means that a simple function bean should be recognized and
executed in AWS Lambda environment
▪ Fits well into the AWS Lambda model with Amazon API Gateway in front
which similar to the Java 8 function receives the (HTTP) request, executes
some business logic and then sends the (HTTP) response to the caller
Spring Cloud Function for AWS Lambda
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
37
https://spring.io/projects/spring-cloud-function https://spring.io/projects/spring-cloud-aws
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ AWS Request Adapter converts the JSON coming
from Lambda function to the HttpServletRequest
which then invokes the Spring Dispatcher Servlet
which then interacts with our Spring Boot
application on API level without starting web server
▪ Then response flows back and AWS Response
Adapter converts HttpServletResponse to JSON
which Lambda function sends back to API Gateway.
Spring Cloud Function for AWS Lambda
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
38
https://spring.io/projects/spring-cloud-function https://spring.io/projects/spring-cloud-aws
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-aws</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-web</artifactId>
</dependency>
Spring Cloud Function for AWS Lambda
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
40
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-spring-cloud-function
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS SAM Template (template.yaml)
GetProductByIdFunction:
Type: AWS::Serverless::Function
Properties:
Handler: org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest
CodeUri: target/***.jar
Runtime: java21
MemorySize: 1024
Environment:
Variables:
MAIN_CLASS: software.amazonaws.Application
SPRING_CLOUD_FUNCTION_DEFINITION: getProductByIdHandler
Spring Cloud Function for AWS Lambda
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
41
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-spring-cloud-function
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@Component
public class GetProductByIdHandler implements
Function<APIGatewayProxyRequestEvent, Product> {
@Autowired
private DynamoProductDao productDao;
...
public APIGatewayProxyResponseEvent apply(APIGatewayProxyRequestEvent requestEvent) {
String id = requestEvent.getPathParameters().get("id");
return productDao.getProduct(id);
}
Spring Cloud Function for AWS Lambda
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
42
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-spring-cloud-function
SPRING_CLOUD_FUNCTION_DEFINITION:
getProductByIdHandler
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Docker Image with Spring Cloud Function
for AWS Lambda
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
43
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ Re-use Spring Cloud Function
▪ Use Base Java 21 Docker Image from Amazon ECR
public.ecr.aws/lambda/java:21
▪ Docker build image
▪ Docker tag image
▪ Create Amazon ECR repository (if not exists)
▪ Docker push image to Amazon ECR repository
Docker Container Image with Spring Cloud Function
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
44
https://github.com/Vadym79/AWSLambdaJavaDockerImage/
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Dockerfile
FROM public.ecr.aws/lambda/java:21
# Copy function code and runtime dependencies from Maven layout
COPY target/classes ${LAMBDA_TASK_ROOT}
COPY target/dependency/* ${LAMBDA_TASK_ROOT}/lib/
Docker Container Image with Spring Cloud Function
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
45
https://github.com/Vadym79/AWSLambdaJavaDockerImage/
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS SAM Template (template.yaml)
GetProductByIdFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
ImageUri: !Sub ${AWS::AccountId}.dkr.ecr.eu-central-1.amazonaws.com/aws-spring-boot-
3.2-java21-custom-docker-image:v1
ImageConfig:
Command:
["org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest"]
MemorySize: 1024
Environment:
Variables:
MAIN_CLASS: software.amazonaws.Application
SPRING_CLOUD_FUNCTION_DEFINITION: getProductByIdHandler
Docker Container Image with Spring Cloud Function
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
46
https://github.com/Vadym79/AWSLambdaJavaDockerImage/
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Demo Application
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
47
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot
▪ Used Spring 3.2 with Amazon Corretto
Java 21
▪ Lambda has 1024 MB memory setting
▪ Lambda uses x86 architecture
▪ 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.labs GmbH
Lambda function lifecycle – a full cold start
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
48
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
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ 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
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
49
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.labs GmbH
0
1000
2000
3000
4000
5000
6000
7000
8000
AWS Serverless Java
Container
AWS Lambda Web Adapter Spring Cloud Function for
AWS
Docker Image based on
Spring Cloud Function for
AWS
p50 p75 p90 p99 p99.9 max
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.labs GmbH
0
1000
2000
3000
4000
5000
6000
7000
8000
AWS Serverless Java
Container
AWS Lambda Web
Adapter
Spring Cloud
Function for AWS
Docker Image based
on Spring Cloud
Function for AWS
Pure Java w/o
framework usage
p50 p75 p90 p99 p99.9 max
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.labs GmbH
0,00
5,00
10,00
15,00
20,00
25,00
30,00
AWS Serverless Java
Container
AWS Lambda Web Adapter Spring Cloud Function for
AWS
Docker Image based on
Spring Cloud Function for
AWS
p50 p75 p90 p99
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
0,00
5,00
10,00
15,00
20,00
25,00
30,00
AWS Serverless Java
Container
AWS Lambda Web
Adapter
Spring Cloud
Function for AWS
Docker Image based
on Spring Cloud
Function for AWS
Pure Java w/o
framework usage
p50 p75 p90 p99
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
0
500
1000
1500
2000
2500
AWS Serverless Java
Container
AWS Lambda Web Adapter Spring Cloud Function for
AWS
Docker Image based on
Spring Cloud Function for
AWS
p99.9 max
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
0
500
1000
1500
2000
2500
AWS Serverless Java
Container
AWS Lambda Web
Adapter
Spring Cloud
Function for AWS
Docker Image based
on Spring Cloud
Function for AWS
Pure Java w/o
framework usage
p99.9 max
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS Lambda
SnapStart
59 How to develop, run and optimize Spring Boot 3 application on AWS Lambda
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
„AWS Lambda SnapStart „ series
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
60
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 SnapStart and 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.labs GmbH
▪ Lambda SnapStart for Java can improve startup performance for latency-
sensitive applications
▪ SnapStart is fully managed
AWS Lambda SnapStart
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
61
https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS SnapStart Deployment & Invocation
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
62
https://dev.to/vkazulkin/measuring-java-11-lambda-cold-starts-with-snapstart-part-1-first-impressions-30a4
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ 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 GraalVM Native Image)
AWS Lambda SnapStart
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
63
https://github.com/Vadym79/AWSLambdaJavaDockerImage/
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
available for:
▪ AWS Serverless Java Container
▪ AWS Lambda Web Adapter
▪ Spring Cloud Function (on AWS Lambda)
▪ Custom (Docker) Container Image
AWS Lambda SnapStart optimizations
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
64
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS SnapStart Deployment & Invocation
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
66
https://aws.amazon.com/de/blogs/compute/reducing-java-cold-starts-on-aws-lambda-functions-with-snapstart/
Vadym Kazulkin @VKazulkin , ip.labs GmbH
C
Create
Snapshot
Firecracker microVM
create & restore
snapshot
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS SnapStart Deployment & Invocation
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
67
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.labs GmbH
▪ Linux CRIU available since 2012 allows a running application to be paused and restarted at some
point later in time, potentially on a different machine.
▪ The overall goal of the project is to support the migration of containers.
▪ When performing a checkpoint, essentially, the full context of the process is saved: program
counter, registers, stacks, memory-mapped and shared memory
▪ To restore the application, all this data can be reloaded and (theoretically) it continues from the
same point.
▪ Challenges
▪ open files
▪ network connections
▪ sudden change in the value of the system clock
▪ time-based caches
CRIU (Checkpoint/Restore in Userspace)
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
68
https://criu.org/Main_Page
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ Compared to CRIU and Docker checkpoint and restore, Firecracker
snapshotting is different in the sense that it does not only saves a single
process or container but a whole running OS.
▪ Advantages of the Firecracker snapshot : we don't have to care about
file handles because they will be still valid after resume.
▪ Drawbacks the Firecracker snapshot : the need to reseed /dev/random
and to sync the system clock.
Snapshot /checkpointing and restore
Firecracker microVM vs CRIU
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
69
https://mail.openjdk.org/pipermail/discuss/2021-July/005864.html
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS SnapStart Deployment & Invocation
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
72
https://aws.amazon.com/de/blogs/compute/reducing-java-cold-starts-on-aws-lambda-functions-with-snapstart/
Vadym Kazulkin @VKazulkin , ip.labs GmbH
Lambda uses the
CRaC APIs for
runtime hooks
for Priming
C
Create
Snapshot
Firecracker microVM
create & restore snapshot
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ CRaC is a JDK project that allows you to start Java programs with a
shorter time to first transaction, combined with less time and resources
to achieve full code speed.
▪ CRaC effectively takes a snapshot of the Java process (checkpoint) when
it is fully warmed up, then uses that snapshot to launch any number of
JVMs from this captured state.
▪ CRaC is based on CRIU
Ideas behind CRaC (Coordinated Restore at
Checkpoint)
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
74
https://www.azul.com/blog/superfast-application-startup-java-on-crac/ https://github.com/CRaC/docs
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS Lambda SnapStart with Priming
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
75
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS Lambda SnapStart with Priming
Approach 1 Priming of DynamoDB request
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
77
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ Prime dependencies 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:
DynamoDbClient client = 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());
……
Priming
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
78
invocation forces HTTP Client and Jackson
Marshallers to pre-initialize
getProductById (int id)
method
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@Configuration
public class Priming implements Resource {
@Autowired
private DynamoProductDao productDao;
public Priming() {
Core.getGlobalContext().register(this);
}
@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
productDao.getProduct("0");
}
@Override
public void afterRestore(Context<? extends Resource> context) throws Exception { }
}
AWS Serverless Java Container (Priming of DynamoDB request)
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
79
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
public class StreamLambdaHandler implements RequestStreamHandler, Resource {
SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler =
SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
@Autowired
private DynamoProductDao productDao;
public StreamLambdaHandler() {
Core.getGlobalContext().register(this);
}
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) {
handler.proxyStream(inputStream, outputStream, context);
}
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
80
AWS Serverless Java Container (Priming of DynamoDB request)
Another alternative to
perform priming (within
custom Lambda function)
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
productDao.getProduct("0");
}
@Override
public void afterRestore(Context<? extends Resource> context) throws Exception { }
}
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
81
AWS Serverless Java Container (Priming of DynamoDB request)
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@Configuration
public class Priming implements Resource {
@Autowired
private DynamoProductDao productDao;
public Priming() {
Core.getGlobalContext().register(this);
}
@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
productDao.getProduct("0");
}
@Override
public void afterRestore(Context<? extends Resource> context) throws Exception { }
}
AWS Lambda Web Adapter (Priming of DynamoDB request)
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
82
Looks the same as with AWS
Serverless Java Container
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@Component
public class GetProductByIdHandler implements
Function<APIGatewayProxyRequestEvent, Product>, Resource {
@Autowired
private DynamoProductDao productDao;
public GetProductByIdWithDynamoDBRequestPrimingHandler () {
Core.getGlobalContext().register(this);
}
...
public Product apply(APIGatewayProxyRequestEvent requestEvent) {
String id = requestEvent.getPathParameters().get("id");
return productDao.getProduct(id);
}
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
83
Spring Cloud Function for AWS (Priming of DynamoDB request)
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
productDao.getProduct("0");
}
@Override
public void afterRestore(Context<? extends Resource> context) throws Exception { }
}
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
84
Spring Cloud Function for AWS (Priming of DynamoDB request)
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS Lambda SnapStart with Priming
Approach 2 Priming/Proxying the whole web
(API Gateway) request without going via
network
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
85
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
public class StreamLambdaHandler implements RequestStreamHandler, Resource {
SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler =
SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
@Autowired
private DynamoProductDao productDao;
public StreamLambdaHandler() {
Core.getGlobalContext().register(this);
}
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) {
handler.proxyStream(inputStream, outputStream, context);
}
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
86
AWS Serverless Java Container (Priming of Web request)
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
handler.proxy(getAwsProxyRequest(), new MockLambdaContext());
}
@Override
public void afterRestore(Context<? extends Resource> context) throws Exception { }
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
87
AWS Serverless Java Container (Priming of Web request)
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
private static AwsProxyRequest getAwsProxyRequest () {
final AwsProxyRequest awsProxyRequest = new AwsProxyRequest ();
awsProxyRequest.setHttpMethod("GET");
awsProxyRequest.setPath("/products/0");
awsProxyRequest.setResource("/products/{id}");
awsProxyRequest.setPathParameters(Map.of("id","0"));
final AwsProxyRequestContext awsProxyRequestContext = new AwsProxyRequestContext();
final ApiGatewayRequestIdentity apiGatewayRequestIdentity= new ApiGatewayRequestIdentity();
apiGatewayRequestIdentity.setApiKey("blabla");
awsProxyRequestContext.setIdentity(apiGatewayRequestIdentity);
awsProxyRequest.setRequestContext(awsProxyRequestContext);
return awsProxyRequest;
}
}
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
88
AWS Serverless Java Container (Priming of Web request)
Simulate
ApiGatewayProxyRequest
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
This type of priming doesn’t work for AWS Web Adapter
as doesn’t offer low-level API to stream/proxy the web
request
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
89
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@Component
public class GetProductByIdHandler implements
Function<APIGatewayProxyRequestEvent, Product>, Resource {
@Autowired
private DynamoProductDao productDao;
public GetProductByIdWithDynamoDBRequestPrimingHandler () {
Core.getGlobalContext().register(this);
}
...
public Product apply(APIGatewayProxyRequestEvent requestEvent) {
String id = requestEvent.getPathParameters().get("id");
return productDao.getProduct(id);
}
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
90
Spring Cloud Function for AWS (Priming of Web request)
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
new FunctionInvoker().handleRequest(new
ByteArrayInputStream(getAPIGatewayRequestMultiLine().getBytes(StandardCharsets.UTF_8)),
new ByteArrayOutputStream(), new MockLambdaContext()); }
@Override
public void afterRestore(Context<? extends Resource> context) throws Exception { }
}
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
91
Spring Cloud Function for AWS (Priming of Web request)
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
private static String getAPIGatewayRequestMultiLine () {
return “”” {
"resource": "/products/{id}",
"path": "/products/0",
"httpMethod": "GET",
"pathParameters": {
"id": "0"
},
"requestContext": {
"identity": {
"apiKey": "blabla"
} } }
“””;
} }
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
92
Spring Cloud Function for AWS (Priming of Web request)
Simulate
ApiGatewayProxyRequest
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Lambda SnapStart Priming Guide
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
93
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.labs GmbH
0
1000
2000
3000
4000
5000
6000
7000
AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS
w/o SnapStart w SnapStart no Priming
w SnapStart & DynamoDB request priming w SnapStart and Web request priming
p90
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
0
1000
2000
3000
4000
5000
6000
7000
AWS Serverless Java
Container
AWS Lambda Web Adapter Spring Cloud Function for
AWS
Pure Java w/o framework
usage
w/o SnapStart w SnapStart no Priming
w SnapStart & DynamoDB request priming w SnapStart and Web request priming
p90
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
0
500
1000
1500
2000
2500
AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS
w/o SnapStart w SnapStart no Priming
w SnapStart & DynamoDB request priming w SnapStart and Web request priming
max value
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
0
500
1000
1500
2000
2500
AWS Serverless Java
Container
AWS Lambda Web Adapter Spring Cloud Function for
AWS
Pure Java w/o framework
usage
w/o SnapStart w SnapStart no Priming
w SnapStart & DynamoDB request priming w SnapStart and Web request priming
max value
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Other optimizations
High performance Serverless Java on AWS
100
• Measure the cold and warm start of Lambda
with SnapStart enabled (and with priming) for
arm64 architecture and compare it to x86
• Measure the impact of the different Java
compilation options
• Measure the impact of setting the different
synchronous HTTP clients
DynamoDbClient client = DynamoDbClient.builder().region(Region.EU_CENTRAL_1)
. httpClient(ApacheHttpClient.create())
//.httpClient(UrlConnectionHttpClient.create())
//.httpClient(AwsCrtHttpClient.create())
.build();
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AWS SnapStart Deployment & Invocation
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
101
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.labs GmbH
Demo Application
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
102
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot
▪ Used Spring 3.2 with Amazon Corretto
Java 21
▪ Lambda has 1024 MB memory setting
▪ Lambda uses x86 architecture
▪ 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.labs GmbH
AWS SnapStart tiered cache
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
103
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.labs GmbH
AWS Lambda under the Hood
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
104
https://www.infoq.com/articles/aws-lambda-under-the-hood/
https://www.infoq.com/presentations/aws-lambda-arch/
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ SnapStart supports the Java 11, 17 and 21 (Corretto) managed runtime
only
▪ Deployment with SnapStart enabled takes more than 2-2,5 minutes
additionally
▪ Snapshot is deleted from cache if Lambda function is not invoked for 14
days
▪ SnapStart currently 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
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
105
https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ AWS Serverless Java Container and AWS Lambda Web Adapter offer similar
functionality
▪ AWS Serverless Java Container a bit lower warm start times
▪ AWS Serverless Java Container offers more flexibility to apply priming techniques to the
individual Lambda functions in the deployment artifact through its API
▪ AWS Lambda Web Adapter offers lower cold start times (with SnapStart and priming)
▪ AWS Lambda Web Adapter offers more event triggers (non HTTP events)
▪ Spring Cloud Function makes sense if already in use
▪ AWS Serverless Java Container (since 2.0.2) and Spring Cloud Function both
support GraalVM Native Image
▪ Using Docker Image leads to the highest cold start. You can use the recent Java
version though
Framework Comparison
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
107
https://www.azul.com/blog/superfast-application-startup-java-on-crac/ https://github.com/CRaC/docs
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
108 How to develop, run and optimize Spring Boot 3 application on AWS Lambda
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ Low footprint ahead-of-time mode for JVM-based languages
▪ High performance for all languages
▪ Convenient language interoperability and polyglot tooling
GraalVM Goals
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
109
Source: „Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger” https://www.youtube.com/watch?v=ANN9rxYo5Hg
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
GraalVM Architecture
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
110
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
GraalVM Ahead-of-Time Compilation
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
111
Source: Oleg Šelajev, Thomas Wuerthinger, Oracle: “Deep dive into using GraalVM for Java and JavaScript”
https://www.youtube.com/watch?v=a-XEZobXspo
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
AOT vs JIT
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
112
Source: „Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger” https://www.youtube.com/watch?v=ANN9rxYo5Hg
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Promise: Java Function compiled into a native executable using GraalVM Native
Image significantly reduces
▪ “cold start” times
▪ memory footprint
GraalVM Native Image
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
113
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ 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
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
114
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Custom Lambda Runtimes
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
115
https://github.com/Vadym79/AWSLambdaGraalVMNativeImage
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
GraalVM Release Calendar
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
116
https://www.graalvm.org/release-calendar/
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
GraalVM CE is based on OpenJDK
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
117
https://www.graalvm.org/2022/openjdk-announcement/
https://blogs.oracle.com/java/post/graalvm-free-license
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Spring Boot 3+ provides official support for
compiling Spring Boot applications into the
GraalVM Native Image
Spring Supprt for GraalVM Native Image
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
118
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
pom.xml
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
....
</profile>
</profiles>
GraalVM Native Image with Spring Cloud Function
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
119
mvn clean package -Pnative
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-graalvm-native-image
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
pom.xml
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>process-aot</id>
<goals>
<goal>process-aot</goal>
</goals>
</execution>
</executions>
</plugin>
GraalVM Native Image with Spring Cloud Function
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
120
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-graalvm-native-image
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
pom.xml
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<configuration>
<mainClass>software.amazonaws.Application</mainClass>
</configuration>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
GraalVM Native Image with Spring Cloud Function
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
121
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-graalvm-native-image
<phase>package</phase>
</execution>
</executions>
</plugin>
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ You can run into errors when
application is running
GraalVM Native Image
High performance Serverless Java on AWS
122
https://github.com/Vadym79/AWSLambdaGraalVMNativeImage/blob/master/pure-lambda-graalvm-jdk-21-native-image/src/main/reflect.json
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import
org.springframework.aot.hint.annotation.RegisterReflection
ForBinding;
@Configuration
@RegisterReflectionForBinding({DateTime.class,
APIGatewayProxyRequestEvent.class, HashSet.class,
APIGatewayProxyRequestEvent.ProxyRequestContext.class,
APIGatewayProxyRequestEvent.RequestIdentity.class,
Product.class, Products.class})
@ImportRuntimeHints(ApplicationConfiguration.Application
RuntimeHintsRegistrar.class)
GraalVM Native Image with Spring Cloud Function
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
123
public class ApplicationConfiguration {
public static class ApplicationRuntimeHintsRegistrar
implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints,
ClassLoader classLoader) {
hints.reflection() .registerType(Product.class,
PUBLIC_FIELDS, INVOKE_PUBLIC_METHODS,
INVOKE_PUBLIC_CONSTRUCTORS
;
}
}
}
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-graalvm-native-image
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
950
1000
1050
1100
1150
1200
1250
GraalVM 21 Native Image with Spring Cloud Function for AWS
p50 p75 p90 p99 p99.9 max
1024 MB
memory
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
0
50
100
150
200
250
GraalVM 21 Native Image with Spring Cloud Function for AWS
p50 p75 p90 p99 p99.9 max
1024 MB
memory
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
0
1000
2000
3000
4000
5000
6000
7000
AWS Serverless Java
Container
AWS Lambda Web Adapter Spring Cloud Function for
AWS
Graal VM 21 Native Image
with Spring Cloud Function
for AWS
w/o SnapStart w SnapStart no Priming
w SnapStart & DynamoDB priming w SnapStart and Web priming
p90
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
0
500
1000
1500
2000
2500
AWS Serverless Java
Container
AWS Lambda Web Adapter Spring Cloud Function for
AWS
Graal VM 21 Native Image
with Spring Cloud Function
for AWS
w/o SnapStart w SnapStart no Priming
w SnapStart & DynamoDB priming w SnapStart and Web priming
max value
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
0
200
400
600
800
1000
1200
1400
GraalVM 21 Native Image with Spring Cloud Function
for AWS
GraalVM 21 Native Image with Pure Java w/o
framework usage
p50 p75 p90 p99 p99.9 max
1024 MB
memory
https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-graalvm-native-image
https://github.com/Vadym79/AWSLambdaGraalVMNativeImage/tree/master/pure-lambda-graalvm-jdk-21-native-image
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
GraalVM 21 Native Image for Spring Cloud Function for
AWS
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
129
ONE SECOND ONE GB
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
0
200
400
600
800
1000
1200
1400
1600
1800
2000
256 MB 512 MB 768 MB 1024 MB 1280 MB 1536 MB
p50 p75 p90 p99 p99.9 max
GraalVM 21
Native Image
with Spring
Cloud
Function for
AWS
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
0
100
200
300
400
500
600
700
800
900
1000
256 MB 512 MB 768 MB 1024 MB 1280 MB 1536 MB
p50 p75 p90 p99 p99.9 max
GraalVM 21
Native Image
with Spring
Cloud
Function for
AWS
ms
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ GraalVM is really powerful and has a lot of potential
▪ GraalVM Native Image improves cold starts and memory footprint
significantly
▪ GraalVM Native 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
▪ 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
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
132
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
Frameworks and libraries Ready for GraalVM Native Image
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
133
https://www.graalvm.org/native-image/libraries-and-frameworks/
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
“Spring Boot 3 application on AWS Lambda” series
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
134
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
▪ GraalVM Native 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.labs GmbH
“Data API for Amazon Aurora Serverless v2
with AWS SDK for Java” series
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
135
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.labs GmbH
Word of caution
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
136
Re-measure for your use case!
Even with my examples measurements might
already produce different results due to:
▪ Lambda Amazon Corretto Java 21 managed
runtime minor version changes
▪ Spring Boot 3 version updates (already 3.3.
released, tested with 3.2.)
▪ Lambda SnapStart snapshot create and
restore improvements
▪ Firecracker VM improvements
▪ GraalVM and Native Image improvements
(already version 22 released, tested with 21)
▪ There are still servers behind Lambda
▪ Java Memory Model impact (L or RAM
caches hits and misses)
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
▪ Spring Boot natively supports CRaC
▪ -Dspring.context.checkpoint=onRefresh -XX:CRaCCheckpointTo={path}
▪ Amazon Corretto doesn’t support CRaC
▪ Azul Zulu OpenJDK supports CRaC
▪ Can be deployed on Lambda with Docker Container Image
▪ Firecracker microVM throws error when performing CRaC snapshot/checkpoint and
it seems that kernel not to support CRIU/CRaC
Spring Boot Native CRaC Support
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
137
https://github.com/CRaC/example-lambda/issues/7
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
FAQ Ask me Anything
How to develop, run and optimize Spring Boot 3 application on AWS Lambda
138
Vadym Kazulkin | @VKazulkin |ip.labs GmbH
139 How to develop, run and optimize Spring Boot 3 application on AWS Lambda
Thank you

More Related Content

PDF
How to develop, run and optimize Spring Boot 3 application on AWS Lambda at A...
PDF
How to develop, run and optimize Spring Boot 3 application on AWS Lambda at J...
PDF
How to develop, run and optimize Spring Boot 3 application on AWS Lambda at ...
PDF
How to develop, run and optimize Spring Boot 3 application on AWS Lambda at V...
PDF
How to develop, run and optimize Spring Boot 3 application on AWS Lambda-OBI ...
PDF
How to develop, run and optimize Spring Boot 3 application on AWS Lambda at I...
PDF
High performance Serverless Java on AWS at We Are Developers 2024
PDF
High performance Serverless Java on AWS- Serverless Architecture Conference B...
How to develop, run and optimize Spring Boot 3 application on AWS Lambda at A...
How to develop, run and optimize Spring Boot 3 application on AWS Lambda at J...
How to develop, run and optimize Spring Boot 3 application on AWS Lambda at ...
How to develop, run and optimize Spring Boot 3 application on AWS Lambda at V...
How to develop, run and optimize Spring Boot 3 application on AWS Lambda-OBI ...
How to develop, run and optimize Spring Boot 3 application on AWS Lambda at I...
High performance Serverless Java on AWS at We Are Developers 2024
High performance Serverless Java on AWS- Serverless Architecture Conference B...

Similar to How to develop, run and optimize Spring Boot 3 application on AWS Lambda at Java Forum Stuttgart 2024 (20)

PDF
How to develop, run and optimize Spring Boot 3 application on AWS Lambda - Wa...
PDF
High performance Serverless Java on AWS at AWS Community Day Belfast 2024
PDF
High performance Serverless Java on AWS- Serverless Meetup Toronto
PDF
High performance Serverless Java on AWS at Froscon 2024
PDF
High performance Serverless Java on AWS- Serverless Architecture Javaland 2025
PDF
High performance Serverless Java on AWS- AWS Community Day Budapest 2024
PDF
Serverless in java Lessons learnt
PDF
Serverless in Java Lessons learnt
PDF
How to reduce cold starts for Java Serverless applications in AWS at InfoShar...
PDF
2018 10-19-jc conf-embrace-legacy-java-ee-by-aws-serverless
PDF
Adopting Java for the Serverless World at VoxxedDays Luxemburg
PPTX
Adopting Java for the Serverless World at JAX 2022
PDF
Adopting Java for the Serverless World at JUG Darmstadt 2022
PDF
Adopting Java for the Serverless World at JUG Bonn 2022
PDF
Legacy java ee meet lambda
PDF
Adopting Java for the Serverless world at JUG London
PDF
Adopting Java for the Serverless World at JUG Hessen 2022
PDF
Adopting Java for the Serverless world at AWS User Group Pretoria
PDF
Run Code, Not Servers: AWS Lambda
PDF
How to reduce cold starts for Java Serverless applications in AWS at Serverle...
How to develop, run and optimize Spring Boot 3 application on AWS Lambda - Wa...
High performance Serverless Java on AWS at AWS Community Day Belfast 2024
High performance Serverless Java on AWS- Serverless Meetup Toronto
High performance Serverless Java on AWS at Froscon 2024
High performance Serverless Java on AWS- Serverless Architecture Javaland 2025
High performance Serverless Java on AWS- AWS Community Day Budapest 2024
Serverless in java Lessons learnt
Serverless in Java Lessons learnt
How to reduce cold starts for Java Serverless applications in AWS at InfoShar...
2018 10-19-jc conf-embrace-legacy-java-ee-by-aws-serverless
Adopting Java for the Serverless World at VoxxedDays Luxemburg
Adopting Java for the Serverless World at JAX 2022
Adopting Java for the Serverless World at JUG Darmstadt 2022
Adopting Java for the Serverless World at JUG Bonn 2022
Legacy java ee meet lambda
Adopting Java for the Serverless world at JUG London
Adopting Java for the Serverless World at JUG Hessen 2022
Adopting Java for the Serverless world at AWS User Group Pretoria
Run Code, Not Servers: AWS Lambda
How to reduce cold starts for Java Serverless applications in AWS at Serverle...
Ad

More from Vadym Kazulkin (16)

PDF
Event-driven architecture patterns in highly scalable image storage solution-...
PPTX
Making sense of AWS Serverless operations- AWS User Group Nuremberg
PPTX
Making sense of AWS Serverless operations at Believe in Serverless community ...
PDF
Making sense of AWS Serverless operations - Amarathon Geek China 2024
PDF
Event-driven architecture patterns in highly scalable image storage solution-...
PDF
Making sense of AWS Serverless operations- Serverless Architecture Conference...
PDF
Detect operational anomalies in Serverless Applications with Amazon DevOps Gu...
PDF
Detect operational anomalies in Serverless Applications with Amazon DevOps Gu...
PDF
Making sense of AWS Serverless operations AWS Community Day NL 2024-
PDF
Event-driven architecture patterns in highly scalable image storage solution ...
PDF
Detect operational anomalies in Serverless Applications with Amazon DevOps Gu...
PDF
Amazon DevOps Guru for Serverless Applications at JAWS Pankration 2024
PDF
Developing highly scalable image storage solution with AWS Serverless at GoTo...
PDF
High performance Serverless Java on AWS- JavaDays Lviv 2024
PDF
High performance Serverless Java on AWS- GoTo Amsterdam 2024
PDF
High performance Serverless Java on AWS at GeeCon 2024 Krakow
Event-driven architecture patterns in highly scalable image storage solution-...
Making sense of AWS Serverless operations- AWS User Group Nuremberg
Making sense of AWS Serverless operations at Believe in Serverless community ...
Making sense of AWS Serverless operations - Amarathon Geek China 2024
Event-driven architecture patterns in highly scalable image storage solution-...
Making sense of AWS Serverless operations- Serverless Architecture Conference...
Detect operational anomalies in Serverless Applications with Amazon DevOps Gu...
Detect operational anomalies in Serverless Applications with Amazon DevOps Gu...
Making sense of AWS Serverless operations AWS Community Day NL 2024-
Event-driven architecture patterns in highly scalable image storage solution ...
Detect operational anomalies in Serverless Applications with Amazon DevOps Gu...
Amazon DevOps Guru for Serverless Applications at JAWS Pankration 2024
Developing highly scalable image storage solution with AWS Serverless at GoTo...
High performance Serverless Java on AWS- JavaDays Lviv 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS at GeeCon 2024 Krakow
Ad

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
KodekX | Application Modernization Development
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Electronic commerce courselecture one. Pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
A Presentation on Artificial Intelligence
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation theory and applications.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Diabetes mellitus diagnosis method based random forest with bat algorithm
NewMind AI Monthly Chronicles - July 2025
Reach Out and Touch Someone: Haptics and Empathic Computing
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Building Integrated photovoltaic BIPV_UPV.pdf
KodekX | Application Modernization Development
Review of recent advances in non-invasive hemoglobin estimation
Spectral efficient network and resource selection model in 5G networks
Digital-Transformation-Roadmap-for-Companies.pptx
Electronic commerce courselecture one. Pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
A Presentation on Artificial Intelligence
Understanding_Digital_Forensics_Presentation.pptx
Encapsulation_ Review paper, used for researhc scholars
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Machine learning based COVID-19 study performance prediction
Encapsulation theory and applications.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...

How to develop, run and optimize Spring Boot 3 application on AWS Lambda at Java Forum Stuttgart 2024

  • 1. Vadym Kazulkin | @VKazulkin |ip.labs GmbH How to develop, run and optimize Spring Boot 3 (web) application on AWS Lambda Vadym Kazulkin, ip.labs, Java Forum Stuttgart, 31 July 2024 How to develop, run and optimize Spring Boot 3 1
  • 2. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Vadym Kazulkin ip.labs GmbH Bonn, Germany Co-Organizer of the Java User Group Bonn v.kazulkin@gmail.com @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
  • 3. Vadym Kazulkin | @VKazulkin |ip.labs GmbH About ip.labs How to develop, run and optimize Spring Boot 3 application on AWS Lambda 4
  • 4. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ Your organization has profound Java and Spring Boot development skills ▪ You have an existing Spring Boot 3 (Web) application, or you want to develop a new one Challenge How to develop, run and optimize Spring Boot 3 application on AWS Lambda 7
  • 5. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ You want to use AWS Lambda for hosting your Spring Boot 3 web application ▪ To benefit from the advantages of the Serverless architectures ▪ Scalability ▪ Less focus on classical operational tasks ▪ API Gateway and Lambda offer fully managed approach ▪ Requires securing the application only ▪ With less possible engineering effort in the business logic Challenge How to develop, run and optimize Spring Boot 3 application on AWS Lambda 8
  • 6. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ It’s a bad idea ▪ Performance is bad ▪ Too much runtime annotation processing, reflection, dynamic class loading used ▪ High cold (application start up) and warm start times Most frequent replies and assumptions How to develop, run and optimize Spring Boot 3 application on AWS Lambda 9
  • 7. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ I’ll use NoSQL Amazon DynamoDB for my examples ▪ You can make it work with the same introduced approaches by managing your own database or using Amazon RDS (i.e. PostgreSQL) ▪ Challenges around managing database connection. Possible solutions are: ▪ Using Amazon RDS Proxy and putting Lambda functions into VPC ▪ Using (PostgreSQL) Aurora Serverless v2 Data API ▪ You need to re-measure performance Database usage How to develop, run and optimize Spring Boot 3 application on AWS Lambda 10
  • 8. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Demo Application How to develop, run and optimize Spring Boot 3 application on AWS Lambda 12 https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot ▪ PUT https://{YOUR_API_GATEWAY_URL}/prod /products/ ▪ GET https://{YOUR_API_GATEWAY_URL}/prod /products/ {productId}
  • 9. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } Spring Boot 3 Application class How to develop, run and optimize Spring Boot 3 application on AWS Lambda 13 https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
  • 10. Vadym Kazulkin | @VKazulkin |ip.labs GmbH public record Product(String id, String name, BigDecimal price) { } Product Entity class How to develop, run and optimize Spring Boot 3 application on AWS Lambda 14 https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
  • 11. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @Repository public class DynamoProductDao implements ProductDao { private static final String PRODUCT_TABLE_NAME = System.getenv("PRODUCT_TABLE_NAME"); private static final DynamoDbClient dynamoDbClient = DynamoDbClient.builder().build(); @Override public void putProduct(Product product) { dynamoDbClient.putItem(PutItemRequest.builder() .tableName(PRODUCT_TABLE_NAME) .item(ProductMapper.productToDynamoDb(product)) .build()); } Product Repository DAO class How to develop, run and optimize Spring Boot 3 application on AWS Lambda 15 https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
  • 12. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @Override public Optional<Product> getProduct(String id) { GetItemResponse getItemResponse = dynamoDbClient.getItem(GetItemRequest.builder().key(Map.of("PK", AttributeValue.builder().s(id).build())).tableName(PRODUCT_TABLE_NAME).build()) ; if (getItemResponse.hasItem()) { return Optional.of(ProductMapper.productFromDynamoDB(getItemResponse.item())); } else { return Optional.empty(); } } .. } Product Repository DAO class How to develop, run and optimize Spring Boot 3 application on AWS Lambda 16 https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
  • 13. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ AWS Serverless Java Container ▪ AWS Lambda Web Adapter ▪ Spring Cloud Function (on AWS Lambda) ▪ Custom (Docker) Container Image ▪ Optimization techniques ▪ AWS (Lambda) SnapStart ▪ GraalVM Native Image ▪ Cold and warm start measurements for all approaches and optimizations Agenda How to develop, run and optimize Spring Boot 3 application on AWS Lambda 17
  • 14. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS Serverless Java Container How to develop, run and optimize Spring Boot 3 application on AWS Lambda 18
  • 15. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ The AWS Serverless Java Container makes it easier to run Java applications written with frameworks such as Struts, Spring, Spring Boot 2 and 3, or JAX-RS/Jersey in Lambda. ▪ The container provides adapter logic to minimize code changes. Incoming events are translated to the Jakarta EE Servlet specification so that frameworks work as before AWS Serverless Java Container How to develop, run and optimize Spring Boot 3 application on AWS Lambda 19 https://github.com/aws/serverless-java-container/
  • 16. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @RestController @EnableWebMvc public class ProductController { @Autowired private DynamoProductDao productDao; @RequestMapping(path = “/products/{id}”, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Optional<Product> getProductById(@PathVariable(“id”) String id) { return productDao.getProduct(id); } @RequestMapping(path = “/products/{id}”, method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) public void createProduct(@PathVariable(“id”) String id, @RequestBody Product product) { product.setId(id); productDao.putProduct(product); } Spring Boot 3 Product (Rest)Controller class How to develop, run and optimize Spring Boot 3 application on AWS Lambda 20 https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
  • 17. Vadym Kazulkin | @VKazulkin |ip.labs GmbH pom.xml <dependency> <groupId>com.amazonaws.serverless</groupId> <artifactId>aws-serverless-java-container-springboot3</artifactId> <version>2.0.0</version> </dependency> AWS Serverless Java Container How to develop, run and optimize Spring Boot 3 application on AWS Lambda 21 https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
  • 18. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Automatic proxying through AWS Serverless Java Container AWS SAM Template (template.yaml) GetProductByIdFunction: Type: AWS::Serverless::Function Properties: Handler: com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler CodeUri: target/***.jar Runtime: java21 MemorySize: 1024 Environment: Variables: MAIN_CLASS: com.amazonaws.serverless.sample.springboot3.Application AWS Serverless Java Container How to develop, run and optimize Spring Boot 3 application on AWS Lambda 22 https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
  • 19. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Self-written Lambda function AWS SAM Template (template.yaml) GetProductByIdFunction: Type: AWS::Serverless::Function Properties: Handler: software.amazonaws.example.product.handler.StreamLambdaHandler::handleRequest CodeUri: target/***.jar Runtime: java21 MemorySize: 1024 AWS Serverless Java Container How to develop, run and optimize Spring Boot 3 application on AWS Lambda 23 https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
  • 20. Vadym Kazulkin | @VKazulkin |ip.labs GmbH public class StreamLambdaHandler implements RequestStreamHandler { SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class); @Override public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException { handler.proxyStream(inputStream, outputStream, context); } } AWS Serverless Java Container How to develop, run and optimize Spring Boot 3 application on AWS Lambda 24 https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
  • 21. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS Lambda Web Adapter How to develop, run and optimize Spring Boot 3 application on AWS Lambda 25
  • 22. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ AWS Lambda Web Adapter is a tool written in Rust programming language to run web applications on AWS Lambda. ▪ It allows developers to build web apps (HTTP API) with familiar frameworks (e.g. Express.js, Next.js, Flask, Spring Boot, ASP.NET and Laravel, anything that speaks HTTP 1.1/1.0) and run it on AWS Lambda. ▪ The same Docker image can run also on Amazon EC2, AWS Fargate, and local computers AWS Lambda Web Adapter How to develop, run and optimize Spring Boot 3 application on AWS Lambda 26 https://github.com/awslabs/aws-lambda-web-adapter
  • 23. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ Run web applications on AWS Lambda ▪ Supports Amazon API Gateway Rest API and HTTP API endpoints, Lambda Function URLs, and Application Load Balancer ▪ Supports all non-HTTP event triggers, such as SQS, SNS, S3, DynamoDB, Kinesis, Kafka, EventBridge, and Bedrock Agents AWS Lambda Web Adapter How to develop, run and optimize Spring Boot 3 application on AWS Lambda 27 https://github.com/awslabs/aws-lambda-web-adapter
  • 24. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Can be deployed in several ways: ▪ Lambda functions packaged as Docker Images ▪ Lambda functions packaged as OCI Images ▪ Lambda functions packaged as Zip package for AWS managed runtimes and attached as Lambda layer to your Lambda function AWS Lambda Web Adapter How to develop, run and optimize Spring Boot 3 application on AWS Lambda 28 https://github.com/awslabs/aws-lambda-web-adapter
  • 25. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @RestController @EnableWebMvc public class ProductController { @Autowired private DynamoProductDao productDao; @RequestMapping(path = “/products/{id}”, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Optional<Product> getProductById(@PathVariable(“id”) String id) { return productDao.getProduct(id); } @RequestMapping(path = “/products/{id}”, method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) public void createProduct(@PathVariable(“id”) String id, @RequestBody Product product) { product.setId(id); productDao.putProduct(product); } Spring Boot 3 Product (Rest)Controller class How to develop, run and optimize Spring Boot 3 application on AWS Lambda 29 https://github.com/Vadym79/AWSLambdaJavaSnapStart/tree/main/spring-boot-3.2
  • 26. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS SAM Template (template.yaml) GetProductByIdFunction: Type: AWS::Serverless::Function Properties: Handler: run.sh CodeUri: target/***.jar Runtime: java21 MemorySize: 1024 Layers: - !Sub arn:aws:lambda:${AWS::Region}:753240598075:layer:LambdaAdapterLayerX86:20 Environment: Variables: AWS_LAMBDA_EXEC_WRAPPER: /opt/bootstrap AWS Lambda Web Adapter How to develop, run and optimize Spring Boot 3 application on AWS Lambda 30 https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-lambda-web-adapter
  • 27. Vadym Kazulkin | @VKazulkin |ip.labs GmbH run.sh #!/bin/sh exec java -cp "./:lib/*" "software.amazonaws.Application" AWS Lambda Web Adapter How to develop, run and optimize Spring Boot 3 application on AWS Lambda 31 https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-lambda-web-adapter
  • 28. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS SAM Template (template.yaml) GetProductByIdFunction: Type: AWS::Serverless::Function Properties: Handler: run.sh CodeUri: target/***.jar Runtime: java21 MemorySize: 1024 Layers: - !Sub arn:aws:lambda:${AWS::Region}:753240598075:layer:LambdaAdapterLayerX86:20 Environment: Variables: AWS_LAMBDA_EXEC_WRAPPER: /opt/bootstrap AWS Lambda Web Adapter How to develop, run and optimize Spring Boot 3 application on AWS Lambda 32 https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-lambda-web-adapter
  • 29. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS_LAMBDA_EXEC_WRAPPER : /opt/bootstrap ▪ When we add a layer to a Lambda function, Lambda extracts the layer contents into the /opt directory in our function’s execution environment ▪ All natively supported Lambda runtimes include paths to specific directories within the /opt directory ▪ This gives our Lambda function access to our layer content AWS Lambda Web Adapter How to develop, run and optimize Spring Boot 3 application on AWS Lambda 33 https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-lambda-web-adapter
  • 30. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Spring Cloud Function for AWS Lambda How to develop, run and optimize Spring Boot 3 application on AWS Lambda 34
  • 31. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ Promote the implementation of business logic via functions. ▪ A simple function application (in context or Spring) is an application that contains beans of type Supplier, Java 8 Function interface or Consumer. Spring Cloud Function for AWS Lambda How to develop, run and optimize Spring Boot 3 application on AWS Lambda 35 https://spring.io/projects/spring-cloud-function https://spring.io/projects/spring-cloud-aws
  • 32. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ With Spring Cloud Function on AWS Lambda, AWS adapter takes a Spring Cloud Function app and converts it to a form that can run in AWS Lambda ▪ With AWS it means that a simple function bean should be recognized and executed in AWS Lambda environment ▪ Fits well into the AWS Lambda model with Amazon API Gateway in front which similar to the Java 8 function receives the (HTTP) request, executes some business logic and then sends the (HTTP) response to the caller Spring Cloud Function for AWS Lambda How to develop, run and optimize Spring Boot 3 application on AWS Lambda 37 https://spring.io/projects/spring-cloud-function https://spring.io/projects/spring-cloud-aws
  • 33. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ AWS Request Adapter converts the JSON coming from Lambda function to the HttpServletRequest which then invokes the Spring Dispatcher Servlet which then interacts with our Spring Boot application on API level without starting web server ▪ Then response flows back and AWS Response Adapter converts HttpServletResponse to JSON which Lambda function sends back to API Gateway. Spring Cloud Function for AWS Lambda How to develop, run and optimize Spring Boot 3 application on AWS Lambda 38 https://spring.io/projects/spring-cloud-function https://spring.io/projects/spring-cloud-aws
  • 34. Vadym Kazulkin | @VKazulkin |ip.labs GmbH pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-function-adapter-aws</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-function-web</artifactId> </dependency> Spring Cloud Function for AWS Lambda How to develop, run and optimize Spring Boot 3 application on AWS Lambda 40 https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-spring-cloud-function
  • 35. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS SAM Template (template.yaml) GetProductByIdFunction: Type: AWS::Serverless::Function Properties: Handler: org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest CodeUri: target/***.jar Runtime: java21 MemorySize: 1024 Environment: Variables: MAIN_CLASS: software.amazonaws.Application SPRING_CLOUD_FUNCTION_DEFINITION: getProductByIdHandler Spring Cloud Function for AWS Lambda How to develop, run and optimize Spring Boot 3 application on AWS Lambda 41 https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-spring-cloud-function
  • 36. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @Component public class GetProductByIdHandler implements Function<APIGatewayProxyRequestEvent, Product> { @Autowired private DynamoProductDao productDao; ... public APIGatewayProxyResponseEvent apply(APIGatewayProxyRequestEvent requestEvent) { String id = requestEvent.getPathParameters().get("id"); return productDao.getProduct(id); } Spring Cloud Function for AWS Lambda How to develop, run and optimize Spring Boot 3 application on AWS Lambda 42 https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-spring-cloud-function SPRING_CLOUD_FUNCTION_DEFINITION: getProductByIdHandler
  • 37. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Docker Image with Spring Cloud Function for AWS Lambda How to develop, run and optimize Spring Boot 3 application on AWS Lambda 43
  • 38. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ Re-use Spring Cloud Function ▪ Use Base Java 21 Docker Image from Amazon ECR public.ecr.aws/lambda/java:21 ▪ Docker build image ▪ Docker tag image ▪ Create Amazon ECR repository (if not exists) ▪ Docker push image to Amazon ECR repository Docker Container Image with Spring Cloud Function How to develop, run and optimize Spring Boot 3 application on AWS Lambda 44 https://github.com/Vadym79/AWSLambdaJavaDockerImage/
  • 39. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Dockerfile FROM public.ecr.aws/lambda/java:21 # Copy function code and runtime dependencies from Maven layout COPY target/classes ${LAMBDA_TASK_ROOT} COPY target/dependency/* ${LAMBDA_TASK_ROOT}/lib/ Docker Container Image with Spring Cloud Function How to develop, run and optimize Spring Boot 3 application on AWS Lambda 45 https://github.com/Vadym79/AWSLambdaJavaDockerImage/
  • 40. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS SAM Template (template.yaml) GetProductByIdFunction: Type: AWS::Serverless::Function Properties: PackageType: Image ImageUri: !Sub ${AWS::AccountId}.dkr.ecr.eu-central-1.amazonaws.com/aws-spring-boot- 3.2-java21-custom-docker-image:v1 ImageConfig: Command: ["org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest"] MemorySize: 1024 Environment: Variables: MAIN_CLASS: software.amazonaws.Application SPRING_CLOUD_FUNCTION_DEFINITION: getProductByIdHandler Docker Container Image with Spring Cloud Function How to develop, run and optimize Spring Boot 3 application on AWS Lambda 46 https://github.com/Vadym79/AWSLambdaJavaDockerImage/
  • 41. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Demo Application How to develop, run and optimize Spring Boot 3 application on AWS Lambda 47 https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot ▪ Used Spring 3.2 with Amazon Corretto Java 21 ▪ Lambda has 1024 MB memory setting ▪ Lambda uses x86 architecture ▪ 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
  • 42. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Lambda function lifecycle – a full cold start How to develop, run and optimize Spring Boot 3 application on AWS Lambda 48 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
  • 43. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ 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 How to develop, run and optimize Spring Boot 3 application on AWS Lambda 49 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
  • 44. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 1000 2000 3000 4000 5000 6000 7000 8000 AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS Docker Image based on Spring Cloud Function for AWS p50 p75 p90 p99 p99.9 max https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k ms
  • 45. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 1000 2000 3000 4000 5000 6000 7000 8000 AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS Docker Image based on Spring Cloud Function for AWS Pure Java w/o framework usage p50 p75 p90 p99 p99.9 max https://dev.to/aws-builders/aws-snapstart-part-13-measuring-warm-starts-with-java-21-using-different-lambda-memory-settings-160k ms
  • 46. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0,00 5,00 10,00 15,00 20,00 25,00 30,00 AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS Docker Image based on Spring Cloud Function for AWS p50 p75 p90 p99 ms
  • 47. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0,00 5,00 10,00 15,00 20,00 25,00 30,00 AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS Docker Image based on Spring Cloud Function for AWS Pure Java w/o framework usage p50 p75 p90 p99 ms
  • 48. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 500 1000 1500 2000 2500 AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS Docker Image based on Spring Cloud Function for AWS p99.9 max ms
  • 49. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 500 1000 1500 2000 2500 AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS Docker Image based on Spring Cloud Function for AWS Pure Java w/o framework usage p99.9 max ms
  • 50. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS Lambda SnapStart 59 How to develop, run and optimize Spring Boot 3 application on AWS Lambda
  • 51. Vadym Kazulkin | @VKazulkin |ip.labs GmbH „AWS Lambda SnapStart „ series How to develop, run and optimize Spring Boot 3 application on AWS Lambda 60 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 SnapStart and 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
  • 52. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ Lambda SnapStart for Java can improve startup performance for latency- sensitive applications ▪ SnapStart is fully managed AWS Lambda SnapStart How to develop, run and optimize Spring Boot 3 application on AWS Lambda 61 https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html
  • 53. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS SnapStart Deployment & Invocation How to develop, run and optimize Spring Boot 3 application on AWS Lambda 62 https://dev.to/vkazulkin/measuring-java-11-lambda-cold-starts-with-snapstart-part-1-first-impressions-30a4
  • 54. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ 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 GraalVM Native Image) AWS Lambda SnapStart How to develop, run and optimize Spring Boot 3 application on AWS Lambda 63 https://github.com/Vadym79/AWSLambdaJavaDockerImage/
  • 55. Vadym Kazulkin | @VKazulkin |ip.labs GmbH available for: ▪ AWS Serverless Java Container ▪ AWS Lambda Web Adapter ▪ Spring Cloud Function (on AWS Lambda) ▪ Custom (Docker) Container Image AWS Lambda SnapStart optimizations How to develop, run and optimize Spring Boot 3 application on AWS Lambda 64
  • 56. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS SnapStart Deployment & Invocation How to develop, run and optimize Spring Boot 3 application on AWS Lambda 66 https://aws.amazon.com/de/blogs/compute/reducing-java-cold-starts-on-aws-lambda-functions-with-snapstart/ Vadym Kazulkin @VKazulkin , ip.labs GmbH C Create Snapshot Firecracker microVM create & restore snapshot
  • 57. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS SnapStart Deployment & Invocation How to develop, run and optimize Spring Boot 3 application on AWS Lambda 67 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
  • 58. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ Linux CRIU available since 2012 allows a running application to be paused and restarted at some point later in time, potentially on a different machine. ▪ The overall goal of the project is to support the migration of containers. ▪ When performing a checkpoint, essentially, the full context of the process is saved: program counter, registers, stacks, memory-mapped and shared memory ▪ To restore the application, all this data can be reloaded and (theoretically) it continues from the same point. ▪ Challenges ▪ open files ▪ network connections ▪ sudden change in the value of the system clock ▪ time-based caches CRIU (Checkpoint/Restore in Userspace) How to develop, run and optimize Spring Boot 3 application on AWS Lambda 68 https://criu.org/Main_Page
  • 59. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ Compared to CRIU and Docker checkpoint and restore, Firecracker snapshotting is different in the sense that it does not only saves a single process or container but a whole running OS. ▪ Advantages of the Firecracker snapshot : we don't have to care about file handles because they will be still valid after resume. ▪ Drawbacks the Firecracker snapshot : the need to reseed /dev/random and to sync the system clock. Snapshot /checkpointing and restore Firecracker microVM vs CRIU How to develop, run and optimize Spring Boot 3 application on AWS Lambda 69 https://mail.openjdk.org/pipermail/discuss/2021-July/005864.html
  • 60. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS SnapStart Deployment & Invocation How to develop, run and optimize Spring Boot 3 application on AWS Lambda 72 https://aws.amazon.com/de/blogs/compute/reducing-java-cold-starts-on-aws-lambda-functions-with-snapstart/ Vadym Kazulkin @VKazulkin , ip.labs GmbH Lambda uses the CRaC APIs for runtime hooks for Priming C Create Snapshot Firecracker microVM create & restore snapshot
  • 61. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ CRaC is a JDK project that allows you to start Java programs with a shorter time to first transaction, combined with less time and resources to achieve full code speed. ▪ CRaC effectively takes a snapshot of the Java process (checkpoint) when it is fully warmed up, then uses that snapshot to launch any number of JVMs from this captured state. ▪ CRaC is based on CRIU Ideas behind CRaC (Coordinated Restore at Checkpoint) How to develop, run and optimize Spring Boot 3 application on AWS Lambda 74 https://www.azul.com/blog/superfast-application-startup-java-on-crac/ https://github.com/CRaC/docs
  • 62. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS Lambda SnapStart with Priming How to develop, run and optimize Spring Boot 3 application on AWS Lambda 75
  • 63. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS Lambda SnapStart with Priming Approach 1 Priming of DynamoDB request How to develop, run and optimize Spring Boot 3 application on AWS Lambda 77
  • 64. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ Prime dependencies 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: DynamoDbClient client = 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()); …… Priming How to develop, run and optimize Spring Boot 3 application on AWS Lambda 78 invocation forces HTTP Client and Jackson Marshallers to pre-initialize getProductById (int id) method
  • 65. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @Configuration public class Priming implements Resource { @Autowired private DynamoProductDao productDao; public Priming() { Core.getGlobalContext().register(this); } @Override public void beforeCheckpoint(Context<? extends Resource> context) throws Exception { productDao.getProduct("0"); } @Override public void afterRestore(Context<? extends Resource> context) throws Exception { } } AWS Serverless Java Container (Priming of DynamoDB request) How to develop, run and optimize Spring Boot 3 application on AWS Lambda 79
  • 66. Vadym Kazulkin | @VKazulkin |ip.labs GmbH public class StreamLambdaHandler implements RequestStreamHandler, Resource { SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class); @Autowired private DynamoProductDao productDao; public StreamLambdaHandler() { Core.getGlobalContext().register(this); } @Override public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) { handler.proxyStream(inputStream, outputStream, context); } How to develop, run and optimize Spring Boot 3 application on AWS Lambda 80 AWS Serverless Java Container (Priming of DynamoDB request) Another alternative to perform priming (within custom Lambda function)
  • 67. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @Override public void beforeCheckpoint(Context<? extends Resource> context) throws Exception { productDao.getProduct("0"); } @Override public void afterRestore(Context<? extends Resource> context) throws Exception { } } How to develop, run and optimize Spring Boot 3 application on AWS Lambda 81 AWS Serverless Java Container (Priming of DynamoDB request)
  • 68. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @Configuration public class Priming implements Resource { @Autowired private DynamoProductDao productDao; public Priming() { Core.getGlobalContext().register(this); } @Override public void beforeCheckpoint(Context<? extends Resource> context) throws Exception { productDao.getProduct("0"); } @Override public void afterRestore(Context<? extends Resource> context) throws Exception { } } AWS Lambda Web Adapter (Priming of DynamoDB request) How to develop, run and optimize Spring Boot 3 application on AWS Lambda 82 Looks the same as with AWS Serverless Java Container
  • 69. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @Component public class GetProductByIdHandler implements Function<APIGatewayProxyRequestEvent, Product>, Resource { @Autowired private DynamoProductDao productDao; public GetProductByIdWithDynamoDBRequestPrimingHandler () { Core.getGlobalContext().register(this); } ... public Product apply(APIGatewayProxyRequestEvent requestEvent) { String id = requestEvent.getPathParameters().get("id"); return productDao.getProduct(id); } How to develop, run and optimize Spring Boot 3 application on AWS Lambda 83 Spring Cloud Function for AWS (Priming of DynamoDB request)
  • 70. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @Override public void beforeCheckpoint(Context<? extends Resource> context) throws Exception { productDao.getProduct("0"); } @Override public void afterRestore(Context<? extends Resource> context) throws Exception { } } How to develop, run and optimize Spring Boot 3 application on AWS Lambda 84 Spring Cloud Function for AWS (Priming of DynamoDB request)
  • 71. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS Lambda SnapStart with Priming Approach 2 Priming/Proxying the whole web (API Gateway) request without going via network How to develop, run and optimize Spring Boot 3 application on AWS Lambda 85
  • 72. Vadym Kazulkin | @VKazulkin |ip.labs GmbH public class StreamLambdaHandler implements RequestStreamHandler, Resource { SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class); @Autowired private DynamoProductDao productDao; public StreamLambdaHandler() { Core.getGlobalContext().register(this); } @Override public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) { handler.proxyStream(inputStream, outputStream, context); } How to develop, run and optimize Spring Boot 3 application on AWS Lambda 86 AWS Serverless Java Container (Priming of Web request)
  • 73. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @Override public void beforeCheckpoint(Context<? extends Resource> context) throws Exception { handler.proxy(getAwsProxyRequest(), new MockLambdaContext()); } @Override public void afterRestore(Context<? extends Resource> context) throws Exception { } How to develop, run and optimize Spring Boot 3 application on AWS Lambda 87 AWS Serverless Java Container (Priming of Web request)
  • 74. Vadym Kazulkin | @VKazulkin |ip.labs GmbH private static AwsProxyRequest getAwsProxyRequest () { final AwsProxyRequest awsProxyRequest = new AwsProxyRequest (); awsProxyRequest.setHttpMethod("GET"); awsProxyRequest.setPath("/products/0"); awsProxyRequest.setResource("/products/{id}"); awsProxyRequest.setPathParameters(Map.of("id","0")); final AwsProxyRequestContext awsProxyRequestContext = new AwsProxyRequestContext(); final ApiGatewayRequestIdentity apiGatewayRequestIdentity= new ApiGatewayRequestIdentity(); apiGatewayRequestIdentity.setApiKey("blabla"); awsProxyRequestContext.setIdentity(apiGatewayRequestIdentity); awsProxyRequest.setRequestContext(awsProxyRequestContext); return awsProxyRequest; } } How to develop, run and optimize Spring Boot 3 application on AWS Lambda 88 AWS Serverless Java Container (Priming of Web request) Simulate ApiGatewayProxyRequest
  • 75. Vadym Kazulkin | @VKazulkin |ip.labs GmbH This type of priming doesn’t work for AWS Web Adapter as doesn’t offer low-level API to stream/proxy the web request How to develop, run and optimize Spring Boot 3 application on AWS Lambda 89
  • 76. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @Component public class GetProductByIdHandler implements Function<APIGatewayProxyRequestEvent, Product>, Resource { @Autowired private DynamoProductDao productDao; public GetProductByIdWithDynamoDBRequestPrimingHandler () { Core.getGlobalContext().register(this); } ... public Product apply(APIGatewayProxyRequestEvent requestEvent) { String id = requestEvent.getPathParameters().get("id"); return productDao.getProduct(id); } How to develop, run and optimize Spring Boot 3 application on AWS Lambda 90 Spring Cloud Function for AWS (Priming of Web request)
  • 77. Vadym Kazulkin | @VKazulkin |ip.labs GmbH @Override public void beforeCheckpoint(Context<? extends Resource> context) throws Exception { new FunctionInvoker().handleRequest(new ByteArrayInputStream(getAPIGatewayRequestMultiLine().getBytes(StandardCharsets.UTF_8)), new ByteArrayOutputStream(), new MockLambdaContext()); } @Override public void afterRestore(Context<? extends Resource> context) throws Exception { } } How to develop, run and optimize Spring Boot 3 application on AWS Lambda 91 Spring Cloud Function for AWS (Priming of Web request)
  • 78. Vadym Kazulkin | @VKazulkin |ip.labs GmbH private static String getAPIGatewayRequestMultiLine () { return “”” { "resource": "/products/{id}", "path": "/products/0", "httpMethod": "GET", "pathParameters": { "id": "0" }, "requestContext": { "identity": { "apiKey": "blabla" } } } “””; } } How to develop, run and optimize Spring Boot 3 application on AWS Lambda 92 Spring Cloud Function for AWS (Priming of Web request) Simulate ApiGatewayProxyRequest
  • 79. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Lambda SnapStart Priming Guide How to develop, run and optimize Spring Boot 3 application on AWS Lambda 93 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
  • 80. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 1000 2000 3000 4000 5000 6000 7000 AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS w/o SnapStart w SnapStart no Priming w SnapStart & DynamoDB request priming w SnapStart and Web request priming p90 ms
  • 81. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 1000 2000 3000 4000 5000 6000 7000 AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS Pure Java w/o framework usage w/o SnapStart w SnapStart no Priming w SnapStart & DynamoDB request priming w SnapStart and Web request priming p90 ms
  • 82. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 500 1000 1500 2000 2500 AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS w/o SnapStart w SnapStart no Priming w SnapStart & DynamoDB request priming w SnapStart and Web request priming max value ms
  • 83. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 500 1000 1500 2000 2500 AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS Pure Java w/o framework usage w/o SnapStart w SnapStart no Priming w SnapStart & DynamoDB request priming w SnapStart and Web request priming max value ms
  • 84. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Other optimizations High performance Serverless Java on AWS 100 • Measure the cold and warm start of Lambda with SnapStart enabled (and with priming) for arm64 architecture and compare it to x86 • Measure the impact of the different Java compilation options • Measure the impact of setting the different synchronous HTTP clients DynamoDbClient client = DynamoDbClient.builder().region(Region.EU_CENTRAL_1) . httpClient(ApacheHttpClient.create()) //.httpClient(UrlConnectionHttpClient.create()) //.httpClient(AwsCrtHttpClient.create()) .build();
  • 85. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS SnapStart Deployment & Invocation How to develop, run and optimize Spring Boot 3 application on AWS Lambda 101 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
  • 86. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Demo Application How to develop, run and optimize Spring Boot 3 application on AWS Lambda 102 https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot ▪ Used Spring 3.2 with Amazon Corretto Java 21 ▪ Lambda has 1024 MB memory setting ▪ Lambda uses x86 architecture ▪ 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
  • 87. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS SnapStart tiered cache How to develop, run and optimize Spring Boot 3 application on AWS Lambda 103 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
  • 88. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AWS Lambda under the Hood How to develop, run and optimize Spring Boot 3 application on AWS Lambda 104 https://www.infoq.com/articles/aws-lambda-under-the-hood/ https://www.infoq.com/presentations/aws-lambda-arch/
  • 89. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ SnapStart supports the Java 11, 17 and 21 (Corretto) managed runtime only ▪ Deployment with SnapStart enabled takes more than 2-2,5 minutes additionally ▪ Snapshot is deleted from cache if Lambda function is not invoked for 14 days ▪ SnapStart currently 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 How to develop, run and optimize Spring Boot 3 application on AWS Lambda 105 https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html
  • 90. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ AWS Serverless Java Container and AWS Lambda Web Adapter offer similar functionality ▪ AWS Serverless Java Container a bit lower warm start times ▪ AWS Serverless Java Container offers more flexibility to apply priming techniques to the individual Lambda functions in the deployment artifact through its API ▪ AWS Lambda Web Adapter offers lower cold start times (with SnapStart and priming) ▪ AWS Lambda Web Adapter offers more event triggers (non HTTP events) ▪ Spring Cloud Function makes sense if already in use ▪ AWS Serverless Java Container (since 2.0.2) and Spring Cloud Function both support GraalVM Native Image ▪ Using Docker Image leads to the highest cold start. You can use the recent Java version though Framework Comparison How to develop, run and optimize Spring Boot 3 application on AWS Lambda 107 https://www.azul.com/blog/superfast-application-startup-java-on-crac/ https://github.com/CRaC/docs
  • 91. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 108 How to develop, run and optimize Spring Boot 3 application on AWS Lambda
  • 92. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ Low footprint ahead-of-time mode for JVM-based languages ▪ High performance for all languages ▪ Convenient language interoperability and polyglot tooling GraalVM Goals How to develop, run and optimize Spring Boot 3 application on AWS Lambda 109 Source: „Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger” https://www.youtube.com/watch?v=ANN9rxYo5Hg
  • 93. Vadym Kazulkin | @VKazulkin |ip.labs GmbH GraalVM Architecture How to develop, run and optimize Spring Boot 3 application on AWS Lambda 110
  • 94. Vadym Kazulkin | @VKazulkin |ip.labs GmbH GraalVM Ahead-of-Time Compilation How to develop, run and optimize Spring Boot 3 application on AWS Lambda 111 Source: Oleg Šelajev, Thomas Wuerthinger, Oracle: “Deep dive into using GraalVM for Java and JavaScript” https://www.youtube.com/watch?v=a-XEZobXspo
  • 95. Vadym Kazulkin | @VKazulkin |ip.labs GmbH AOT vs JIT How to develop, run and optimize Spring Boot 3 application on AWS Lambda 112 Source: „Everything you need to know about GraalVM by Oleg Šelajev & Thomas Wuerthinger” https://www.youtube.com/watch?v=ANN9rxYo5Hg
  • 96. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Promise: Java Function compiled into a native executable using GraalVM Native Image significantly reduces ▪ “cold start” times ▪ memory footprint GraalVM Native Image How to develop, run and optimize Spring Boot 3 application on AWS Lambda 113
  • 97. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ 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 How to develop, run and optimize Spring Boot 3 application on AWS Lambda 114
  • 98. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Custom Lambda Runtimes How to develop, run and optimize Spring Boot 3 application on AWS Lambda 115 https://github.com/Vadym79/AWSLambdaGraalVMNativeImage
  • 99. Vadym Kazulkin | @VKazulkin |ip.labs GmbH GraalVM Release Calendar How to develop, run and optimize Spring Boot 3 application on AWS Lambda 116 https://www.graalvm.org/release-calendar/
  • 100. Vadym Kazulkin | @VKazulkin |ip.labs GmbH GraalVM CE is based on OpenJDK How to develop, run and optimize Spring Boot 3 application on AWS Lambda 117 https://www.graalvm.org/2022/openjdk-announcement/ https://blogs.oracle.com/java/post/graalvm-free-license
  • 101. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Spring Boot 3+ provides official support for compiling Spring Boot applications into the GraalVM Native Image Spring Supprt for GraalVM Native Image How to develop, run and optimize Spring Boot 3 application on AWS Lambda 118
  • 102. Vadym Kazulkin | @VKazulkin |ip.labs GmbH pom.xml <profiles> <profile> <id>native</id> <activation> <property> <name>native</name> </property> </activation> .... </profile> </profiles> GraalVM Native Image with Spring Cloud Function How to develop, run and optimize Spring Boot 3 application on AWS Lambda 119 mvn clean package -Pnative https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-graalvm-native-image
  • 103. Vadym Kazulkin | @VKazulkin |ip.labs GmbH pom.xml <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <id>process-aot</id> <goals> <goal>process-aot</goal> </goals> </execution> </executions> </plugin> GraalVM Native Image with Spring Cloud Function How to develop, run and optimize Spring Boot 3 application on AWS Lambda 120 https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-graalvm-native-image
  • 104. Vadym Kazulkin | @VKazulkin |ip.labs GmbH pom.xml <plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <configuration> <mainClass>software.amazonaws.Application</mainClass> </configuration> <executions> <execution> <id>build-native</id> <goals> <goal>compile-no-fork</goal> </goals> GraalVM Native Image with Spring Cloud Function How to develop, run and optimize Spring Boot 3 application on AWS Lambda 121 https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-graalvm-native-image <phase>package</phase> </execution> </executions> </plugin>
  • 105. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ You can run into errors when application is running GraalVM Native Image High performance Serverless Java on AWS 122 https://github.com/Vadym79/AWSLambdaGraalVMNativeImage/blob/master/pure-lambda-graalvm-jdk-21-native-image/src/main/reflect.json
  • 106. Vadym Kazulkin | @VKazulkin |ip.labs GmbH import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHintsRegistrar; import org.springframework.aot.hint.annotation.RegisterReflection ForBinding; @Configuration @RegisterReflectionForBinding({DateTime.class, APIGatewayProxyRequestEvent.class, HashSet.class, APIGatewayProxyRequestEvent.ProxyRequestContext.class, APIGatewayProxyRequestEvent.RequestIdentity.class, Product.class, Products.class}) @ImportRuntimeHints(ApplicationConfiguration.Application RuntimeHintsRegistrar.class) GraalVM Native Image with Spring Cloud Function How to develop, run and optimize Spring Boot 3 application on AWS Lambda 123 public class ApplicationConfiguration { public static class ApplicationRuntimeHintsRegistrar implements RuntimeHintsRegistrar { @Override public void registerHints(RuntimeHints hints, ClassLoader classLoader) { hints.reflection() .registerType(Product.class, PUBLIC_FIELDS, INVOKE_PUBLIC_METHODS, INVOKE_PUBLIC_CONSTRUCTORS ; } } } https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-graalvm-native-image
  • 107. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 950 1000 1050 1100 1150 1200 1250 GraalVM 21 Native Image with Spring Cloud Function for AWS p50 p75 p90 p99 p99.9 max 1024 MB memory ms
  • 108. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 50 100 150 200 250 GraalVM 21 Native Image with Spring Cloud Function for AWS p50 p75 p90 p99 p99.9 max 1024 MB memory ms
  • 109. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 1000 2000 3000 4000 5000 6000 7000 AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS Graal VM 21 Native Image with Spring Cloud Function for AWS w/o SnapStart w SnapStart no Priming w SnapStart & DynamoDB priming w SnapStart and Web priming p90
  • 110. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 500 1000 1500 2000 2500 AWS Serverless Java Container AWS Lambda Web Adapter Spring Cloud Function for AWS Graal VM 21 Native Image with Spring Cloud Function for AWS w/o SnapStart w SnapStart no Priming w SnapStart & DynamoDB priming w SnapStart and Web priming max value ms
  • 111. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 200 400 600 800 1000 1200 1400 GraalVM 21 Native Image with Spring Cloud Function for AWS GraalVM 21 Native Image with Pure Java w/o framework usage p50 p75 p90 p99 p99.9 max 1024 MB memory https://github.com/Vadym79/AWSLambdaJavaWithSpringBoot/tree/master/spring-boot-3.2-with-graalvm-native-image https://github.com/Vadym79/AWSLambdaGraalVMNativeImage/tree/master/pure-lambda-graalvm-jdk-21-native-image ms
  • 112. Vadym Kazulkin | @VKazulkin |ip.labs GmbH GraalVM 21 Native Image for Spring Cloud Function for AWS How to develop, run and optimize Spring Boot 3 application on AWS Lambda 129 ONE SECOND ONE GB
  • 113. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 200 400 600 800 1000 1200 1400 1600 1800 2000 256 MB 512 MB 768 MB 1024 MB 1280 MB 1536 MB p50 p75 p90 p99 p99.9 max GraalVM 21 Native Image with Spring Cloud Function for AWS ms
  • 114. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 0 100 200 300 400 500 600 700 800 900 1000 256 MB 512 MB 768 MB 1024 MB 1280 MB 1536 MB p50 p75 p90 p99 p99.9 max GraalVM 21 Native Image with Spring Cloud Function for AWS ms
  • 115. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ GraalVM is really powerful and has a lot of potential ▪ GraalVM Native Image improves cold starts and memory footprint significantly ▪ GraalVM Native 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 ▪ 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 How to develop, run and optimize Spring Boot 3 application on AWS Lambda 132
  • 116. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Frameworks and libraries Ready for GraalVM Native Image How to develop, run and optimize Spring Boot 3 application on AWS Lambda 133 https://www.graalvm.org/native-image/libraries-and-frameworks/
  • 117. Vadym Kazulkin | @VKazulkin |ip.labs GmbH “Spring Boot 3 application on AWS Lambda” series How to develop, run and optimize Spring Boot 3 application on AWS Lambda 134 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 ▪ GraalVM Native 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
  • 118. Vadym Kazulkin | @VKazulkin |ip.labs GmbH “Data API for Amazon Aurora Serverless v2 with AWS SDK for Java” series How to develop, run and optimize Spring Boot 3 application on AWS Lambda 135 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
  • 119. Vadym Kazulkin | @VKazulkin |ip.labs GmbH Word of caution How to develop, run and optimize Spring Boot 3 application on AWS Lambda 136 Re-measure for your use case! Even with my examples measurements might already produce different results due to: ▪ Lambda Amazon Corretto Java 21 managed runtime minor version changes ▪ Spring Boot 3 version updates (already 3.3. released, tested with 3.2.) ▪ Lambda SnapStart snapshot create and restore improvements ▪ Firecracker VM improvements ▪ GraalVM and Native Image improvements (already version 22 released, tested with 21) ▪ There are still servers behind Lambda ▪ Java Memory Model impact (L or RAM caches hits and misses)
  • 120. Vadym Kazulkin | @VKazulkin |ip.labs GmbH ▪ Spring Boot natively supports CRaC ▪ -Dspring.context.checkpoint=onRefresh -XX:CRaCCheckpointTo={path} ▪ Amazon Corretto doesn’t support CRaC ▪ Azul Zulu OpenJDK supports CRaC ▪ Can be deployed on Lambda with Docker Container Image ▪ Firecracker microVM throws error when performing CRaC snapshot/checkpoint and it seems that kernel not to support CRIU/CRaC Spring Boot Native CRaC Support How to develop, run and optimize Spring Boot 3 application on AWS Lambda 137 https://github.com/CRaC/example-lambda/issues/7
  • 121. Vadym Kazulkin | @VKazulkin |ip.labs GmbH FAQ Ask me Anything How to develop, run and optimize Spring Boot 3 application on AWS Lambda 138
  • 122. Vadym Kazulkin | @VKazulkin |ip.labs GmbH 139 How to develop, run and optimize Spring Boot 3 application on AWS Lambda Thank you