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

From Serverful to Serverless Java (JFokus 2024)

From Serverful to Serverless Java (JFokus 2024)

For more than 25 years, Java played an essential role when building sustainable software architectures and applications. The traditional approach to develop & run Java applications included setting up applications servers, complex build & dependency management and often relied on large enterprise frameworks. In contrast, Serverless functions are ideally short-lived, single-purposed and do not rely on extensive infrastructure configuration. This paradigm shift encouraged new frameworks, concepts and techniques to evolve.

In this session we will explain how to run your traditional Java Spring application in a Serverless way (on AWS Lambda) with minimal effort. We’ll also cover how to iteratively apply optimizations to get the best out of your Serverless Java experience. You will get an overview of best practices such as GraalVM native images and SnapStart (which leverages elements of the CRaC project), performance trade-offs and the design considerations for each step to be able to make well-informed decisions when bringing enterprise Java applications to AWS Lambda.

Dennis Kieselhorst

February 06, 2024
Tweet

More Decks by Dennis Kieselhorst

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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-..", ... } }
  16. 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
  17. 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
  18. 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
  19. 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
  20. JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024,

    Amazon Web Services, Inc. or its affiliates. 22 What about RestControllers?
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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 <dependency> <groupId>com.amazonaws.serverless</groupId> <artifactId>aws-serverless-java-container-springboot3</artifactId> <version>2.0.0</version> </dependency> com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. JFOKUS 2024 – FROM SERVERFUL TO SERVERLESS JAVA © 2024,

    Amazon Web Services, Inc. or its affiliates. Serverless Java Replatforming Guide 58
  47. 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!