Designing and delivering cloud - optimized Java applications Java – 25 years and going Apache Software Lines of code 1996-2020. Source: The Apache Software Foundation - Annual Report - FY2020 Agenda Java ecosystem overview Spring journey to AWS Lambda Accelerate with modern frameworks Other considerations Summary Motivation Java plays an essential role when building reliable software architectures Spring Boot is the most popular Java Application Framework with +60%* of Java developers reportedly using it Broad ecosystem (libraries, tools and resources) & Developer experience Over the past years companies invested in these technologies New feature development: Spring 3.0 with GraalVM Support, Project Leyden, Java release cycle * Source: https://www.jrebel.com/blog/2021 - java - technology - report Challenges with Java & Spring in Serverless Java initially has been designed around factors that differ from modern cloud - native requirements There is a paradigm shift from always - on serverful applications to short - lived serverless functions Large enterprise frameworks often perform expensive operations during initial JVM startup up time Gathering metrics from JVM based usage – combining them across service traces Spring Boot on a server • Initialize the environment once • Handle multiple request with the same instance (concurrently) • Instance keeps running after request processing • Scaling based on metrics or manually 1 2 Request Request Initialize full Spring Context Running application 3 Request ... Initialization Compute options on AWS Amazon EC2 • Virtual Machines (Linux/Windows) AWS Lambda • Execute code without provisioning or managing servers • Synchronous (request/response) or Asynchronous execution models Containers • Broad range of fully managed, do - it - yourself and serverless container options H/W OS Runtime Level of abstraction VM Task Function Unit of Consumption Containers c ommon i ssues • Slow to start up • High CPU consumption • OOM killed due to high memory usage Containers r ecommendations • Check your container logs • Use at least Java 8u191 with UseContainerSupport flag, or better use Java 10 • Kubernetes probes ( livenessProbe , readinessProbe , startupProbe ) • HPA (Horizontal Pod Autoscaler ) and VPA (Vertical Pod Autoscaler ) for scaling Optimizing your containers • Optimize for smaller size • Use a minimalist operating system • Not all runtimes are equal! REPOSITORY SIZE node:latest 674MB java:latest 643MB node:slim 184MB ubuntu:latest 85.8MB alpine:latest 4.41MB busybox:latest 1.15MB AWS Lambda Event Databases AWS Services Etc. Lambda Function API Request State change Spring Boot on 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 • Scales to zero if there is no request – Managed auto scaling 1 Initialization Execution Execution 2 Execution 3 Initialization 4 Execution Initialize full Spring Context Initialize full Spring Context Why optimize Java on AWS Lambda You are charged based on execution duration, rounded up to the nearest 100ms . The price depends on the amount of memory you allocate to your function. Each % in performance optimization can make your customers happier and save money ! A Spring journey to AWS Lambda 1. Migrate 2. Optimize 3. Refactor 4. Accelerate Migrate HTTP request AWS Lambda Event Amazon API Gateway AWS Serverless Java Container • Transforms events so that Spring 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 Optimize • Right size your applications memory setting with AWS Lambda Power Tuning • Use Tiered Compilation to reduce cold - starts • Use AWS SDK & Initialization best practices • Pre - warm execution environments with Provisioned Concurrency AWS Lambda Power Tuning Tiered compilation • Use optimized C1 compiler for faster application start up • Add an environment variable to change the compiler level Without Tiered With Tiered 11.378 - 40 % 6.725 Init Duration Results will vary based on your application