Have you ever tried Java on AWS Lambda but found that the cold-start latency and memory usage were far too high? In this session, we will show how we optimized Java for serverless applications by leveraging GraalVM with Quarkus to provide both supersonic startup speed and a subatomic memory footprint.
1. With AWS Lambda and Azure functions
Quarkus serverless
Bill Burke
1
2. AGENDA
2
● Intro: What is Serverless?
● Quarkus Serverless Story
● DEMOS
○ Improve Lambda cold start with Quarkus and Graal
○ Use Quarkus with Spring DI within a Lambda class
○ Use Quarkus with Spring MVC with Lambda
○ If time, show Quarkus with Spring MVC with Azure Functions
What we’ll be
discussing today
3. What is Serverless?
3
● Scale to Zero
○ Event triggered service spinup
○ Event triggered autoscaling
○ CGI 2.0 - only sort of joking…
● Direct code deployments
○ No containers
● New function development models
○ Event driven
○ Fine grain
INTRO
4. Why Serverless?
4
● Reduce Costs
○ $$$ in the public cloud
○ Density/resources in the private cloud
● Deployment simpler and faster
○ Remove the “ops” from “dev-ops”
● Inherently scalable
● Finer grain deployments
○ Is that actually better?
INTRO
5. Serverless disadvantages
5
● Cold-start latency
○ Hard boot
○ AWS Provisioned Concurrency is PAY-HOURLY rather than
PAY-PER_USE!
● Too fine-grained
○ Hard to debug and reproduce problems
● Vendor lock in
○ Vendor specific APIs
INTRO
6. Java Serverless disadvantages
6
● Java optimized for long-running server applications
● Performance at the expense of memory
○ Massive memory footprints
○ Amazon Lambda also bills by memory consumption
● Traditional Java framework ecosystem slow to boot
○ Spring Boot, Jave EE, etc...
○ Measured in seconds
INTRO
9. Quarkus serverless vision
QUARKUS SERVERLESS STORY
● Reduce Java footprint and cold start latency
○ Quarkus doesn’t assume “memory is abundant and cheap”
● Bring Java Ecosystem to AWS Lambda and Azure Functions
○ Spring and CDI Injection
○ Hibernate and Spring Data
● Portability
○ JAX-RS and Spring MVC
○ Quarkus Funqy FaaS Framework
● Leverage Graal VM in Lambda
○ Reduce memory and cold start even more!
11. Quarkus Lambda extension demo
QUARKUS SERVERLESS STORY
● Reduce memory footprint and cold start with Graal
● Bring Spring and CDI injection to a Lambda Function
12. Setup POM
QUARKUS SERVERLESS STORY
● Quarkize your pom (quarkus plugin). Same as any other Quarkus project
● quarkus-amazon-lambda dependency (lambda extension)
● Uber-jar
● If native build, rename to bootstrap and zip it
13. How does Quarkus + Lambda work?
QUARKUS SERVERLESS STORY
● JVM Mode
○ Thin Lambda StreamHandler that wraps your Lambda
○ Bootstraps Quarkus if needed
● Custom Runtime (Native) Mode
○ Custom runtime event loop
○ Unmarshal event, invoke lambda, marshal response
● Automatic
○ mvn clean install
■ builds JVM, .jar deployment
○ mvn clean install -Dnative
■ pulls in event loop code and builds native executable
16. Quarkus + Lambda + REST
QUARKUS SERVERLESS STORY
● Write portable REST microservices and functions
● Deployable to Lambda
● Not limited to one function per deployment
● Use favorite REST framework
○ Spring MVC
○ JAX-RS
○ Servlet
○ Vert.x Web
● JVM and Native Mode
17. Setup for Quarkus + REST + Lambda
QUARKUS SERVERLESS STORY
● AWS API Gateway
● Install SAM CLI
○ Allows easy integration with AWS API Gateway
● Create an S3 bucket for packaging
● sam package
● sam deploy
18. How does Lambda + REST work?
QUARKUS SERVERLESS STORY
● Leverage AWS API Gateway
○ Map HTTP endpoint to a Lambda
● Resteasy can consume Spring MVC annotations
● Quarkus Consolidated under Vert.x Web and Netty
○ Undertow and Resteasy and Spring MVC run under Vert.x Web
● API Gateway request mapped to virtual Netty HTTP Request
● Netty request is dropped on Netty event loop
● Vert.x Web picks up netty request and dispatches appropriately
20. Quarkus + Azure + REST
QUARKUS SERVERLESS STORY
● Write portable REST microservices and functions
● Deployable to Azure Functions
● Use favorite REST framework
○ Spring MVC
○ JAX-RS
○ Servlet
○ Vert.x Web
● JVM mode only
21. Setup for Quarkus + REST + Azure functions
QUARKUS SERVERLESS STORY
● Install Azure CLI
● Modify POM
○ Azure plugin
○ Zip structure
● $ az login
● $ mvn clean install azure-functions:deploy
22. How does Azure + REST work?
QUARKUS SERVERLESS STORY
● Thin @HttpTrigger wrapper function
○ Bootstraps Quarkus once
○ Azure http request mapped to Netty HTTP request
○ Netty request is dropped on Netty event loop
● Vert.x Web picks up netty request and dispatches appropriately
24. Portable FAAS framework
QUARKUS SERVERLESS STORY
● Not all FaaS frameworks support REST
● For AWS at least, extra things are involved to use REST (API Gateway)
● Quarkus Funqy Services
○ AWS Lambda
○ Azure Functions
○ Knative
○ etc...
25. Quarkus Funqy Services
QUARKUS SERVERLESS STORY
public MyFunction {
@Funq
public OutputObject myfunc(InputObject input) {
…
}
}