This session will explore how SnapStart revolutionizes the startup time of Java-based Lambda functions, significantly reducing cold start delays and improving overall execution efficiency. It'll uncover the technical details behind SnapStart, and provide practical insights and best practices for leveraging SnapStart effectively in your own serverless projects. Furthermore, this presentation will offer valuable knowledge and actionable takeaways for optimizing Java-based AWS Lambda functions and discovering how it can supercharge your cloud applications and workflows, especially when working with Java.
1. Accelerating Serverless Performance with
AWS Lambda SnapStart
Wellington AWS User Group - April 2023
Geethika Guruge
Lead Consultant @ Mantel
APN Ambassador | AWS Community Builder
2. 850+ team members
Offices across Australia and New Zealand
Voted Best Place to Work in 2021 and 2022
Operating at over 150 clients
3. Changing how the world works for the better.
Agenda
● A trip down the memory lane
● Anatomy of AWS Lambda
● Lambda Execution Lifecycle
● Let's Talk about Java
● Writing Lambdas in Java - is it as crazy as it sounds
● Lambda Snapstart
● Snapstart Pitfalls
● A bonus feature!!!!
● Demo
4. Changing how the world works for the better.
How Did We End Up Here
5. Changing how the world works for the better.
Anatomy of AWS Lambda
● The invocation request is passed by Load Balancer to a selected Frontend Invoker
● Frontend Invoker checks the request, and asks the Worker Manager for a sandboxed
function that will handle the invocation.
● Worker Manager either finds a proper Worker and a sandbox, or creates one.
● Once it's ready, the code is executed by a Worker
6. Changing how the world works for the better.
Anatomy of AWS Lambda
● Firecracker is an open source project that allows AWS to span hundreds and thousands
lightweight sandboxes on a single Worker.
7. Changing how the world works for the better.
Anatomy of AWS Lambda
● Note that we have the handler function and some code outside of it
8. Changing how the world works for the better.
Lambda Execution Environment Lifecycle (ColdStart)
● The code outside the handler function (init) is executed only during the cold start
9. Changing how the world works for the better.
Lambda Execution Environment Lifecycle (WarmStart)
10. Changing how the world works for the better.
When Would a Cold Strat Occur
● The first execution
● After 5-7 mins since last execution
● When lambda is horizontally scaling
12. Changing how the world works for the better.
The Problems Features of Java
● It's a Compiled language
● Uses JIT compiler at runtime.
○ Initial compilation is minimal.
○ Compiles additional classes on-demand.
● Allows use of reflection.
○ JVM cannot optimize the code on compilation time
● Slower on first run.
● Faster on subsequent runs.
13. Changing how the world works for the better.
So Why Do People Still Use Java
● Been there since 1990s
● Lots of small & big enterprises leverage Java
● Its way too complicated to rewrite decades old Applications
● Existing Team skill set
● Has a still growing community
14. Changing how the world works for the better.
So Why Would You Use Java in Lambdas
● No-one writes Java anymore - Well not really…..
○ There are probably between 5 and 10 million Java developers in the world,
& Java is still in the top 5 most-frequently-used programming languages
○ AWS themselves are huge users of Java
● Java is far too verbose for small Lambda functions
○ You can do a lot in 15 minutes with 6 CPU cores and 10GB memory.
● Cold Starts are terrible
○ That's why we’re here…..
15. Changing how the world works for the better.
When To Use Java in Lambdas
● When a team is already writing Java
● When throughput performance is important
● When writing applications and not “glue” scripts
17. Changing how the world works for the better.
Lambda Execution Environment Lifecycle (SnapStart)
18. Changing how the world works for the better.
Things to Consider….Pitfalls to Avoid
● Uniqueness
○ Random number generation (i.e DB keys, correlation IDs,
Cryptography)
○ Some commonly used libraries are “snap safe”
■ java.security.SecureRandom
■ Amazon Linux’s /dev/random and /dev/urandom
● Network Connections
○ Write resilient code to re-establish connections if necessary
● Ephemeral Data
○ Externally loaded data, may need to be refreshed.
19. Changing how the world works for the better.
Hang on….There's more