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

Serverless Java with Spring (JFokus 2025)

Serverless Java with Spring (JFokus 2025)

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

February 05, 2025
Tweet

More Decks by Dennis Kieselhorst

Other Decks in Programming

Transcript

  1. © 2025, 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. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon EKS, Amazon ECS container orchestration 2 Run on compute instances You have to manage compute capacity Customer account Instance Server-based containers 1 Pod Networking Application Load Balancer NAT gateway Pod definition Pod Pod Pod Pod Pod Pod
  3. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Run on compute instances You have to manage compute capacity Customer account Instance Server-based containers (continued) Cluster auto scaling, Karpenter Instance 2 Networking Application Load Balancer NAT gateway Pod definition Pod Pod Pod Pod Amazon EKS, Amazon ECS container orchestration 1 Platform engineering Auth State Abstraction Event bus Policy Automation API OPA ArgoCD Crossplane
  4. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Networking You don’t have to manage capacity Customer account Serverless containers AWS Fargate 2 Pod Application Load Balancer NAT gateway Pod definition Amazon EKS, Amazon ECS container orchestration 1
  5. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. From full control to fully managed Container Base Image Runtime App Networking Cloud Container Base Image Runtime App Networking Compute Cloud Auto Scaling Container Base Image Runtime App Cloud
  6. © 2025, Amazon Web Services, Inc. or its affiliates. All

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

    rights reserved. Example: AWS Lambda invocation AWS Lambda Service GetRequestId Function API request to endpoints Lambda Invoke-API Invoke UnicornRequestIdHandler::handleRequest
  9. © 2025, 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) SQSHandler::handleRequest
  10. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Functions are invoked by events 12 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-..", ... } } • 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.) API Gateway
  11. © 2025, 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.) 13 { "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 (Event Source Mapping) AWS Lambda Function
  12. © 2025, 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) 14
  13. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Spring Cloud Function advantages 15 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 for routing and abstraction
  14. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Spring AWS adapter FunctionInvoker 16 { "body": "{"name": "MyPet"}" , "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 Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker
  15. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Spring Cloud Function Routing 17 FunctionInvoker needs routing instructions for multiple functional beans Static configuration via application.properties / environment variables Dynamic configuration via Message headers & MessageRoutingCallback
  16. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Static routing for multiple Lambda functions 18 Use cases: Different permission or scaling requirements per function PostPetFunction DeletePetFunction
  17. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. PostPet Function DeletePet Function POST DELETE org.springframework.cloud.function. adapter.aws.FunctionInvoker /unicorns Static routing for multiple Lambda functions Amazon API Gateway SPRING_CLOUD_FUNCTION_DEFINITION: handlePetPostFunction SPRING_CLOUD_FUNCTION_DEFINITION: handlePetDeleteFunction
  18. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Function granularity Will I end up with hundreds of functions? API Gateway GET /unicorns POST PUT DELETE HEAD OPTIONS /orders GET POST PUT DELETE HEAD
  19. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. API Gateway GET /unicorns POST PUT DELETE HEAD OPTIONS /orders GET POST PUT DELETE HEAD API Gateway /orders /unicorns Many functions vs. few functions /orders/{id}/images
  20. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Dynamic routing for a single Lambda function 22 Use cases: Same permission set (CRUD), less cold-starts, simple deployment PetFunction
  21. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Dynamic routing for a single Lambda function 23 PetFunction Amazon API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker
  22. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Dynamic routing for a single Lambda function 24 PetFunction Amazon API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker HTTP Header : spring.cloud.function.definition: handlePetPostFunction
  23. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Dynamic routing for a single Lambda function 25 handlePetPostFunction PetFunction Amazon API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker HTTP Header : spring.cloud.function.definition: handlePetPostFunction
  24. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Dynamic routing for a single Lambda function 26 PetFunction Amazon API Gateway org.springframework.cloud.function. adapter.aws.FunctionInvoker HTTP Header : spring.cloud.function.definition: handlePetDeleteFunction handlePetDeleteFunction
  25. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Dynamic routing with custom logic 27 Implement your own dynamic routing logic
  26. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Recap on Spring Cloud Function • Standard way for asynchronous functions triggered by Kafka, RabbitMQ, Amazon SQS etc. • Good for small to medium sized synchronous APIs (CRUD /pet) • Additional benefit for synchronous functions: No need for embedded HTTP servers (Tomcat or Netty) • Portability and testing experience 28 1 Lambda function ≠ 1 Spring Cloud Function
  27. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 31 What about RestControllers?
  28. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. HTTP adapter 32 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
  29. © 2025, Amazon Web Services, Inc. or its affiliates. All

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

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

    rights reserved. Traditional Servlet Container vs. SJC 35 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
  32. © 2025, Amazon Web Services, Inc. or its affiliates. All

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

    rights reserved. Steps to implement 1. Add dependency to pom.xml (or Gradle buildfile accordingly) 2. Configure the handler class 3. Package and deploy 37 <dependency> <groupId>com.amazonaws.serverless</groupId> <artifactId>aws-serverless-java-container-springboot3</artifactId> <version>2.1.2</version> </dependency> com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler
  34. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 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 38 com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler
  35. © 2025, 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 39 Handling via functions Using an HTTP adapter
  36. © 2025, Amazon Web Services, Inc. or its affiliates. All

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

    rights reserved. Example: AWS Lambda scaling • 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 42 1 Initialization Execution Execution 2 Execution 3 Initialization 4 Execution Env #1 Env #2
  38. © 2025, 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 43
  39. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 44 AWS Lambda SnapStart Up to 10x faster start-up performance
  40. © 2025, 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
  41. © 2025, Amazon Web Services, Inc. or its affiliates. All

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

    rights reserved. SnapStart benefits • No additional cost • Minimal to no code changes • Customization via Runtime Hooks 47 Without SnapStart With SnapStart 6.725 - 80 % 1.15 Full Duration Results will vary based on your application
  43. © 2025, 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!
  44. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Custom runtime/ GraalVM native image 49
  45. © 2025, 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 50 bootstrap 2023
  46. © 2025, 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 51
  47. © 2025, Amazon Web Services, Inc. or its affiliates. All

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

    rights reserved. Thank you! Maximilian Schellhorn @maschnetwork Dennis Kieselhorst @dekies.de 57 Please complete the session survey using the website https://www.jfokus.se/rate/2247