Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Serverless Java with Spring

Serverless Java with Spring

Serverless computing revolutionizes how we build and run applications by abstracting server management, delivering fine granular auto-scaling, and billing only for execution time. This enables developers to focus on code, not infrastructure. Join this talk to understand how you can build and deploy cloud-native and Serverless Spring applications. You will learn how to run already existing Spring Boot applications in a Serverless environment with the AWS Serverless Java container and its integration with the Spring ecosystem. You will also learn how-to develop new event-driven applications with Spring Cloud Function. We will also cover support for GraalVM native images, developer tooling and additional performance optimizations.

Dennis Kieselhorst

May 31, 2024
Tweet

More Decks by Dennis Kieselhorst

Other Decks in Programming

Transcript

  1. © 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
  2. © 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
  3. © 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
  4. © 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
  5. © 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
  6. © 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
  7. © 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
  8. © 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
  9. © 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
  10. © 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)
  11. © 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
  12. © 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
  13. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Event handling with Spring 15
  14. © 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.)
  15. © 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
  16. © 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
  17. © 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
  18. © 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
  19. © 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
  20. © 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
  21. © 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
  22. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 24 What about RestControllers?
  23. © 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
  24. © 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
  25. © 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
  26. © 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
  27. © 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
  28. © 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 <dependency> <groupId>com.amazonaws.serverless</groupId> <artifactId>aws-serverless-java-container-springboot3</artifactId> <version>2.0.2</version> </dependency> com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler
  29. © 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
  30. © 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
  31. © 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
  32. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 35 AWS Lambda SnapStart Up to 10x faster start-up performance
  33. © 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
  34. © 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
  35. © 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
  36. © 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!
  37. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Custom runtime/ GraalVM native image 40
  38. © 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
  39. © 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
  40. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Additional considerations 43
  41. © 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
  42. © 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
  43. © 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
  44. © 2024, Amazon Web Services, Inc. or its affiliates. All

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

    rights reserved. Thank you! 48 Please complete the session survey in the mobile app