Slide 1

Slide 1 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Serverless Java with Spring Maximilian Schellhorn Senior Solutions Architect AWS Dennis Kieselhorst Principal Solutions Architect AWS

Slide 2

Slide 2 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Application Load Balancer Container Typical server-based application HTTP server Spring Framework Application code Base Image Runtime Cloud Networking (VPC) Compute Node Container Runtime Orchestration Running application Initialization

Slide 3

Slide 3 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Server-based container management • Familiar environment and broad choice of open source projects • Full control and debugging capabilities • Flexibility of infrastructure (CPU, GPU) • Long running & coarse granular scaling (Container & Node) • Operations: Responsible for Cluster upgrades, CVE Patching etc. Container Base Image Runtime App Networking Compute Cloud Auto Scaling

Slide 4

Slide 4 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 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 5

Slide 5 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 5

Slide 6

Slide 6 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Serverless Containers Container Base Image Runtime App Networking Cloud Container Base Image Runtime App Networking Compute Cloud Auto Scaling Container Base Image Runtime App Cloud

Slide 7

Slide 7 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Slide 8

Slide 8 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Serverless containers to functions Container Base Image Runtime App Cloud Serverless Function Service App Cloud

Slide 9

Slide 9 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Anatomy of a Serverless function Handler function • Function written in Java, JS, Python etc. • Input params with req. information Configuration and Deployment • Runtime Version: Java 11, Java 17, Java 21 • Memory setting, CPU Architecture (x86, ARM), Timeout up to 15 minutes • Package as an uber jar and upload via UI, CLI or infrastructure-as-code 10240 MB 128 MB

Slide 10

Slide 10 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Serverless Functions Event Messages from a queue API request to endpoints Changes in resource state (Spring) Framework Application code Serverless Function

Slide 11

Slide 11 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda invocation AWS Lambda Service GetRequestId Function { "some": "json" } { "result": "json" } API request to endpoints Lambda Invoke-API Invoke

Slide 12

Slide 12 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda Event integration Messages from a queue or stream Reads ProcessMessage Function Invoke AWS Lambda Service (Event Source Mapping)

Slide 13

Slide 13 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda request handling 13 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

Slide 14

Slide 14 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Java with AWS Lambda • 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 14 1 Initialization Execution Execution 2 Execution 3 Initialization 4 Execution Env #1 Env #2

Slide 15

Slide 15 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Event handling with Spring 15

Slide 16

Slide 16 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Functions are invoked by events 16 AWS Lambda Function 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-..", ... } } API request to endpoints • AWS Lambda is invoked via events (API Event, Kafka Event, SQS Event ..) • Events follow a certain structure • This is different from the raw format (HTTP, Kafka Record etc.)

Slide 17

Slide 17 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Functions are invoked by events • AWS Lambda is invoked via events (API Event, Kafka Event, SQS Event ..) • Events follow a certain structure • This is different from the raw format (HTTP, Kafka Record etc.) 17 { "eventSource":"aws:kafka",", "bootstrapServers":"b-2.demo-cluster", "records":{ "mytopic-0":[ { "topic":"mytopic", "partition":0, "offset":15, "timestamp":1545084650987, "key":"abcDEFghiJKLmnostuVW", "value":“my-value", "headers":[] }] } } AWS Lambda Service AWS Lambda Function

Slide 18

Slide 18 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Method 1: Handling via functions 18 AWS Lambda Service GetRequestId Function UnicornRequestIdHandler:: handleRequest API Request

Slide 19

Slide 19 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Spring Cloud Function § Promotes the use of Java Functions to implement business requirements § Additional routing and mapping functionality § Specific adapters (spring-cloud-function-adapter-aws) 19 com.example.FunctionConfiguration::uppercase

Slide 20

Slide 20 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Method 1: Handling via Spring Cloud Function 20 org.springframework.cloud.function. adapter.aws.FunctionInvoker Context agnostic with Message<> Type Mapping to your POJO Plain Java Functions Leverage full Spring ecosystem (DI, Spring Data, Testing) Platform specific entrypoint that allows function routing

Slide 21

Slide 21 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Method 1: Handling via Spring Cloud Function 21 SCF allows you to expose functions locally (localhost:8080/hello & /helloSpring) Familiar Spring Boot Integration testing

Slide 22

Slide 22 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Method 1: Multiple functions 22 mySpringFunction1 AWS Lambda Amazon API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker HTTP Header | Env Var: mySpringFunction1

Slide 23

Slide 23 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Method 1: Handling via functions • Standard way for asynchronous functions triggered by Kafka, RabbitMQ, SQS etc. • For synchronous functions there is no need for embedded HTTP servers (API Gateway or Function URL take care of this) 23

Slide 24

Slide 24 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 24 What about RestControllers?

Slide 25

Slide 25 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Method 2: HTTP adapter 25 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

Slide 26

Slide 26 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 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 26

Slide 27

Slide 27 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 27 AWS Serverless Java Container 2.0.2 Support for Spring Framework 6.x, Spring Boot 3.x and JAX-RS/ Jersey 3.x including native image support (GraalVM) NEW

Slide 28

Slide 28 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Traditional Servlet Container vs. SJC 28 Embedded Apache Tomcat HTTP Connector Servlet Container Expose port Spring Boot (Web) HttpServlet POST /orders -> doPost GET / orders -> doGet Serverless Java Container Spring Boot (Web) HttpServlet POST /orders -> doPost GET / orders -> doGet API Gateway JSON Your Java Code Your Java Code

Slide 29

Slide 29 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Serverless Java Container & Spring Cloud Function 29 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 Spring Cloud Function Spring Boot

Slide 30

Slide 30 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Method 2: Steps to implement 1. Add dependency to pom.xml (or Gradle buildfile accordingly) 2. Configure the handler class 3. Package and deploy 30 com.amazonaws.serverless aws-serverless-java-container-springboot3 2.0.2 com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler

Slide 31

Slide 31 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Method 2: Advantages of HTTP Adapter • Use the familiar @Controller programming style • Easily port your existing applications to AWS Lambda • Sample implementations in Spring Boot via Serverless Java Container 31 com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler

Slide 32

Slide 32 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 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 32 Method 1: Handling via functions Method 2: Using an HTTP adapter

Slide 33

Slide 33 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Performance 33

Slide 34

Slide 34 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 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 for a typical Spring application SnapStart Default with Java 17 & 21 on AWS Lambda 34

Slide 35

Slide 35 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 35 AWS Lambda SnapStart Up to 10x faster start-up performance

Slide 36

Slide 36 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda SnapStart • 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 microVM snapshot technology

Slide 37

Slide 37 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. How SnapStart works 37 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

Slide 38

Slide 38 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. SnapStart benefits • No additional cost • Minimal to no code changes • Customization via Runtime Hooks 38 Without SnapStart With SnapStart 6.725 - 80 % 1.15 Full Duration Results will vary based on your application

Slide 39

Slide 39 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. - beforeCheckpoint - priming - class loading - dependency injection - afterRestore - unique ID for execution environment - re-establish connections CRaC interface for post- and pre-hooks examples Spring Boot >3.2 implements useful hooks for you!

Slide 40

Slide 40 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Custom runtime/ GraalVM native image 40

Slide 41

Slide 41 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 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 § Spring Boot provides built-in support 41 bootstrap 2023

Slide 42

Slide 42 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 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 for a typical Spring application SnapStart Default with Java 17 & 21 on AWS Lambda 42

Slide 43

Slide 43 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Additional considerations 43

Slide 44

Slide 44 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 44 Database connection pooling • Database connection pools (e.g. HikariCP) initialize multiple database connections during startup • Since an AWS Lambda environment handles a single request at a time you only need 1 connection per environment • Ensure to stay within database connection pool limits • Solution: RDS Proxy or Reserved Concurrency (Limit connections) … Container

Slide 45

Slide 45 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Observability considerations • AWS Lambda does not expose traditional HTTP endpoints • Implement a push model, leverage AWS Distro for OpenTelemetry or get insights directly via Amazon CloudWatch/ AWS X-Ray Function, layer code Lambda runtime Prometheus Metrics Scraping Endpoint? Spring Actuator Health Checks? Firecracker MicroVM 45 METRICS TRACES LOGS

Slide 46

Slide 46 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Deep Dive: Java on AWS Lambda Workshop 46 https://catalog.workshops.aws/java-on-aws-lambda

Slide 47

Slide 47 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Serverless Java Replatforming Guide 47

Slide 48

Slide 48 text

© 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Thank you! 48 Please complete the session survey in the mobile app