Slide 1

Slide 1 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. © 2024, Amazon Web Services, Inc. or its affiliates. Dennis Kieselhorst Principal Solutions Architect From Serverful to Serverless Java J F O K U S 0 6 . 0 2 . 2 0 2 4 Maximilian Schellhorn Senior Solutions Architect

Slide 2

Slide 2 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Serverful Java overview 2 Application Load Balancer Web app Auto Scaling Group HTTP server Framework Application code Java application

Slide 3

Slide 3 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Why serverless? Get to market faster, deliver features Reduced operational overhead Automatic scaling by unit of consumption High performance and scalability Pay for value Pay for value vs. pay for instance Security, Integration & High availability Deep integration into the ecosystem

Slide 4

Slide 4 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 4 Event Changes in data state Requests to endpoints Changes in resource state Serverless Architecture Application code AWS Lambda function Framework

Slide 5

Slide 5 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Anatomy of AWS Lambda functions 5 Handler function • Function executed on invocation • Java 8, 11, 17 and 21 supported Event • Invocation data sent to function • JSON event payload differs by source (API, State change, Queue) Context • Additional information from the service • Example: Request ID

Slide 6

Slide 6 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. AWS Lambda Java libraries aws-lambda-java-core • Handler interfaces • Context object aws-lambda-java-events • Object types of AWS native integrations aws-lambda-java-tests • Junit extensions to simplify testing 6

Slide 7

Slide 7 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. AWS Lambda Memory configuration AWS Lambda exposes a memory control CPU and network capacity are allocated proportionally 10240 MB 128 MB Source: https://github.com/alexcasalboni/aws-lambda-power-tuning

Slide 8

Slide 8 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Package & Deploy Java applications to AWS Lambda 8 Code Maven Shade Plugin Maven Assembly Plugin Gradle zip task Gradle shadow plugin ZIP or JAR Archive Uber Jar with Maven Shade Plugin Zip Archive with Gradle Task Build Uber Jars or Zip Archives with familiar build tooling Upload via AWS Console, AWS CLI, Amazon S3 or framework automation

Slide 9

Slide 9 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Lambda Execution Model 9 reqs changes Lambda function Lambda function Synchronous Asynchronous Poll API Gateway or Function URL Events (e.g. file uploaded) Queue or Stream Lambda function

Slide 10

Slide 10 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Example: Simple Function URL 10 AWS Lambda User Function Url

Slide 11

Slide 11 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Example: REST API 11 AWS Lambda Amazon API Gateway AWS Lambda GET POST com.unicorn.store.GetHandler /unicorns com.unicorn.store.PostHandler

Slide 12

Slide 12 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Example: Queue processor Amazon SQS Queue Queue Processor Lambda function Lambda function Lambda function

Slide 13

Slide 13 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. © 2024, Amazon Web Services, Inc. or its affiliates. Event handling 14

Slide 14

Slide 14 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 15 Event Databases AWS Services Etc. Lambda Function API Request State change

Slide 15

Slide 15 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Functions are invoked by events AWS Lambda is invoked via events (API Event, S3 Object Event, SQS Event ..) Events follow a certain structure This is different from accepting a HTTP connection 16 Amazon API Gateway AWS Lambda POST /v1/pets HTTP/2 Host: x.execute-api.eu-west-1.. User-Agent: curl/7.64.1 Accept: */* Content-Type: application/json Content-Length: 39 Body: {“data:“ : “test“} { "body": “{“data“: “test“}“ , "resource": "/{proxy+}", "path": "/path/to/resource", "httpMethod": "POST", "isBase64Encoded": true, "headers": { "Accept-Encoding": "gzip", "Accept-Language": "en-US,en;q=0.8" }, }, "requestContext": { "accountId": "123456789012", "requestId": "c6af9ac6-7b61-..", ... } }

Slide 16

Slide 16 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Method 1: Handling via functions 17 AWS Lambda Amazon API Gateway Amazon SQS Queue AWS Lambda AWS Lambda Amazon S3

Slide 17

Slide 17 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Method 1: Handling via functions No need for embedded HTTP servers (API Gateway or Function URL take care of this) Handle Requests via aws-lambda-java-events or raw via Map or Input-Outputstream Framework implementations • Spring Cloud Functions • Micronaut Serverless Function (Application Type) • Quarkus aws-lambda extension or Funqy 18

Slide 18

Slide 18 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Spring Cloud Function Promotes the use of Java Functions to implement business requirements Additional routing and mapping functionality Abstraction via Java Functional Interface 19 com.example.FunctionConfiguration::uppercase

Slide 19

Slide 19 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 20 Spring Cloud Function Application code Web app function Amazon API Gateway Method 1: Handling via functions Framework provides abstractions and enrichments

Slide 20

Slide 20 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 22 What about RestControllers?

Slide 21

Slide 21 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 23 Framework Application code Adapter logic Web app function Invocation event mapped to framework request Function result mapped from framework response Web application wrapped in adapter logic. Amazon API Gateway Method 2: HTTP adapter

Slide 22

Slide 22 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 24 Method 2: Example with AWS Serverless Java Container Transforms events so that frameworks can handle them as if it was an HTTP Request Routing via RestController, POJO serialization, HTTP status codes Add the library to your code and provide a configuration class

Slide 23

Slide 23 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 25 AWS Serverless Java Container 2.0.0 Support for Spring Framework 6.x, Spring Boot 3.x and JAX-RS/ Jersey 3.x including native image support (GraalVM) NEW

Slide 24

Slide 24 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 26 Spring Framework Application code (@SpringBootApplication, @Controller) AWS Serverless Java Container Web app function Invocation event mapped to framework request Function result mapped from framework response Amazon API Gateway AWS Serverless Java Container & Spring Cloud Function Spring Cloud Function Spring Boot

Slide 25

Slide 25 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Method 2: Steps to implement 1. Add dependency to pom.xml (or Gradle buildfile accordingly) 2. Configure the handler class 3. Package and deploy 27 com.amazonaws.serverless aws-serverless-java-container-springboot3 2.0.0 com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler

Slide 26

Slide 26 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 28 Method 2: Advantages of HTTP Adapter Use the familiar @Controller programming style Easily port your existing applications to AWS Lambda Framework implementations • Spring Boot via Serverless Java Container • Micronaut via micronaut-function-aws-api-proxy • Quarkus via AWS Lambda HTTP extension com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler

Slide 27

Slide 27 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Summary: Two main ways of handling events in Java • Deserialize JSON payload to a Java object, no HTTP request/ response classes involved • Consume and process the content in event specific code • Standard way of implementing for Non-HTTP usecases • More difficult to replatform existing applications and reuse frameworks • Adapter transforms events to HTTP requests/ responses (e.g. to Jakarta Servlet API) • Allows to keep existing code and reuse existing frameworks as is • Some overhead due to additional transformation 29 Method 1: Handling via functions Method 2: Using an HTTP adapter

Slide 28

Slide 28 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. © 2024, Amazon Web Services, Inc. or its affiliates. Performance & Scaling 31

Slide 29

Slide 29 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 32 1 2 Request Request Initialize application Running application 3 Request … • Initialize the environment once • Handle multiple request with the same instance (concurrently) • Instance keeps running after request processing • Scaling based on metrics or manually Java in a container or VM Initialization

Slide 30

Slide 30 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 33 Execution Environment ready? Download Code Start Runtime Initialize Function Code Code execution Request Code execution No Yes Create Execution Environment Execution Environment Cold start Warm start AWS Lambda request handling

Slide 31

Slide 31 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. • Initialize execution environment on first invoke • An execution environment handles a single request at a time • If an execution environment is busy with a request – another environment is started – simpler programming model • Scales to zero if there is no request – Managed auto scaling 34 1 Initialization Execution Execution 2 Execution 3 Initialization 4 Execution Java with AWS Lambda Env #1 Env #2

Slide 32

Slide 32 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 0 2 4 6 8 10 12 14 0 1 2 3 4 5 6 7 8 9 Tiered Compilation Compile Time frameworks No framework No optimization Lightw. deps Function handler GraalVM Effort to modernize Cold-start (seconds) Provisioned Concurrency AWS Lambda performance optimizations SnapStart Default with Java 17 & 21 on AWS Lambda 35

Slide 33

Slide 33 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. AWS Lambda Provisioned Concurrency Sets floor on minimum number of execution environments Pre-warm execution environments to reduce cold-start impact Can save costs in certain situations

Slide 34

Slide 34 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 40 AWS Lambda SnapStart Up to 10x faster start-up performance

Slide 35

Slide 35 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. AWS Lambda SnapStart microVM snapshot technology Takes a snapshot of the memory and disk state Snapshot encryption & caching for low-latency access Creates new Lambda environments from cached snapshot Fully Managed

Slide 36

Slide 36 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 42 Code execution Invoke Resume Resume Snapshot Post Snapshot Hook (optional) Pre Snapshot Hook (optional) Create Execution Environment Init during deployment Download Code Start Runtime Initialize Function Code Create Snapshot first request How SnapStart works

Slide 37

Slide 37 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. SnapStart benefits No additional cost Minimal to no code changes Customization via Runtime Hooks 45 Without SnapStart With SnapStart 6.725 - 80 % 1.15 Full Duration Results will vary based on your application

Slide 38

Slide 38 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. CRaC interface for post- and pre-hooks examples - beforeCheckpoint - priming - class loading - dependency injection - afterRestore - unique ID for execution environment - re-establish connections

Slide 39

Slide 39 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. © 2024, Amazon Web Services, Inc. or its affiliates. Custom runtime/ GraalVM native image 47

Slide 40

Slide 40 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Handling requests (managed runtime) 48 Execution Environment Runtime Lambda function code Lambda Extensions Layers (Optional) Runtime API

Slide 41

Slide 41 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Handling requests (managed runtime) 49 Execution Environment Runtime public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, …) { … return } Lambda Extensions Layers (Optional) Runtime API

Slide 42

Slide 42 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Handling requests (custom runtime) 50 Execution Environment Custom Runtime and Code Lambda Extensions Layers (Optional) Runtime API

Slide 43

Slide 43 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. GraalVM native images on AWS Lambda Create a native binary and deploy as AWS Lambda custom runtime Sub-second application startup and low memory footprint Frameworks provide built-in support (Micronaut, Quarkus, Spring) 54 bootstrap 2023

Slide 44

Slide 44 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. 0 2 4 6 8 10 12 14 0 1 2 3 4 5 6 7 8 9 Tiered Compilation Compile Time frameworks No framework No optimization Lightw. deps Function handler GraalVM Effort to modernize Cold-start (seconds) Provisioned Concurrency AWS Lambda performance optimizations SnapStart Default with Java 17 & 21 on AWS Lambda 55

Slide 45

Slide 45 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Deep Dive: Java on AWS Lambda Workshop 57 https://catalog.workshops.aws/java-on-aws-lambda

Slide 46

Slide 46 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. Serverless Java Replatforming Guide 58

Slide 47

Slide 47 text

JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024, Amazon Web Services, Inc. or its affiliates. © 2024, Amazon Web Services, Inc. or its affiliates. Thank you!