SlideShare a Scribd company logo
How to avoid common mistakes
and misconceptions when working
with Java on AWS Lambda
Andrzej Dębski
Technicalities
• Disclaimer
• https://github.com/Adebski/LambdaCommonMisconception
sExamples
About me
1. A decade of experience with JVM languages, mostly Java
and Scala
2. Working (mostly) on backend services
3. Pratical experience with AWS Lambda since
February 2021
• Maintenance and development of 2 existing services
• Development of additional 2 services/products on
AWS Lambda from scratch
Agenda
1. Cold starts (generic and those specific to JVM)
2. AWS Lambda Pricing
3. In-memory caching on Lambda
Cold starts
Lambda instance lifecycle
Cold start type 1 (container recycle)
Time
Duration
(ms)
Init Duration
(ms)
18:16:32 85.24 1821.08
18:18:50 1.95 N/A
18:24:05 1.74 N/A
18:30:40 72.91 1870.40
Cold start type 2 (JVM JIT and lazy load)
Code path # Duration (ms) Notes
Code path 1 731.32 First request, cold
start + JVM JIT
Code path 1 19.86 Code is already
loaded by JVM, no
cold start
Code path 1 4.31 Further optimizations
Code path 2 1261.40 JVM executes new
branch for the first
time
Code path 2 2.12 No JVM overhead
Reducing cold starts
1. Provisioned concurrency (best but costly)
• Keeps a set of instances ready to respond to requests
2. "Background" traffic (best effort)
3. Use a different language
like Go or use AOT compilation through GraalVM
• https://shinesolutions.com/2021/08/30/improving-cold-
start-times-of-java-aws-lambda-functions-using-
graalvm-and-native-images/
Cold start for provisioned concurrency
Code path # Duration (ms) Notes
Code path 1 33.12 Small overhead, first
request to Lambda
Code path 1 2.09
Code path 1 2.34
Code path 2 2.03 New request type
handled for the first
time
Code path 2 2.4
Provisioned concurrency best practices
1. Make sure the function is invoked using the alias.
2. Monitor (and alarm) on metrics
• ProvisionedConcurrencySpilloverInvocations
• ProvisionedConcurrencyUtilization
3. Pre-warm the code paths in the constructor
4. Use code deploy policies to gradually deploy new
function revisions.
Recap
1. Cold starts are real and they affect the
JVM Lambdas even more.
2. Either accept them
or pay for provisioned concurrency (and use it well).
3. Monitor and instrument your functions
to understand the bottlenecks in your code.
$$$
Lambda pricing
1. Lambda price (https://aws.amazon.com/lambda/pricing/):
• On demand: billed for every GB-second (sum(duration ) *
allocated memory) + number of invocations
• Provisioned concurrency is more complicated. We pay for
keeping the containers "warm"
2. Free-tier
How to calculate AWS costs
1. AWS pricing calculator
• https://calculator.aws/#/
2. Cost estimates for the following examples:
• https://tinyurl.com/yk7v3wek
Assumptions
1. "Average" request time: 200 ms
2. Monthly costs
3. Region: us-east-1
4. Focus on compute cost, ignore everything else
Scenarios considered
1. "Hello world": on-demand vs provisioned capacity
2. Smallest Fargate vs Lambda
3. StackExchange on Lambda vs Fargate
"Hello world", 1 request per second
256 MB RAM
On demand Provisioned
capacity of 1
0.33 $ 4.55 $
Fargate, 1 request per second
1769 MB RAM 1vCPU
2GB RAM
On demand Provisioned
capacity of 1
Fargate
8.8 $ 28.28 $ 36.04 $
Simulating Stack Exchange
1. https://stackexchange.com/performance
• 1.3* 10^9 monthly page views means ~495 requests per
second
• For Fargate we assume 9 tasks to protect from AZ outage, 82.5
RPS per task.
• For Lambda provisioned concurrency try to handle every
request with prov capacity.
• 495 (RPS) / 5 (requests per second per container) = 99
SE, 495 requests per second
1769 MB RAM 1vCPU
2GB RAM
On demand Provisioned
capacity of 99
Fargate,
9 tasks
7,744.27 $ 6,502.63 $ 324.36 $
Recap/tips&tricks
1. Free-tier
2. Use the AWS Pricing Calculator
3. With more traffic Lambda gets really costly
4. Take advantage of the "free" compute during init phase
for on-demand lambdas
5. For consistent workloads provisioned concurrency may
be cheaper
6. Always consider factors other than $$$ when evaluating
technologies
https://hichaelmart.medium.com/shave-99-93-off-your-
lambda-bill-with-this-one-weird-trick-33c0acebb2ea
Caching
Caching in Lambda - challenges
1. No hard guarantees when the container will be recycled
2. Best effort async updates
3. No control over the routing algorithm
(no sticky sessions)
Caching in Lambda - approaches
1. (Mostly) rely on out of process cache
2. Use different compute platform if L1 cache is critical
3. Use Lambda extensions
• That's how AWS AppConfig does it
Caching in Lambda - extensions
1. Code that can execute alongside your Lambda
2. Extension continues to execute AFTER the Lambda
handler returned a response.
3. Implementing cache in extension
• Save the data on Lambda FS
• Expose local HTTP server and serve the data
Lambda extension overview
Extensions for caching
1. (+) "Asynchronous" cache updates
2. (-) Overhead (in ms):
• Avg (no ext/ext): 1.88 ms/14.1 ms
• Tm99.9 (no ext/ext): 1.88 ms/14.2 ms
3. (-) Lambda instance can't handle new requests until
extension is done
4. (-) Extension instances are not shared between
containers
Recap
1. In memory caching in Lambda is not straightforward
• Small window of time where the cache is "warm"
• Asynchronous updates are best effort only
2. Extensions improve the situation but have their own
downsides
• Runtime overhead
• Complicate the flow
3. L2 cache or different compute platform
How to avoid common mistakes
and misconceptions when working
with Java on AWS Lambda
Andrzej Dębski

More Related Content

Similar to JDD2022: How to avoid common mistakes and misconceptions when working with Java on AWS Lambda

AWS Serverless patterns & best-practices in AWS
AWS Serverless  patterns & best-practices in AWSAWS Serverless  patterns & best-practices in AWS
AWS Serverless patterns & best-practices in AWS
Dima Pasko
 
Serverless Computing
Serverless ComputingServerless Computing
Serverless Computing
Anand Gupta
 
Batch processing using AWS LAMBDA.pdf
Batch processing using AWS LAMBDA.pdfBatch processing using AWS LAMBDA.pdf
Batch processing using AWS LAMBDA.pdf
hayhadiabbas
 
AWS re:Invent 2016: Monitoring, Hold the Infrastructure: Getting the Most fro...
AWS re:Invent 2016: Monitoring, Hold the Infrastructure: Getting the Most fro...AWS re:Invent 2016: Monitoring, Hold the Infrastructure: Getting the Most fro...
AWS re:Invent 2016: Monitoring, Hold the Infrastructure: Getting the Most fro...
Amazon Web Services
 
Webinar: Serverless Architectures with AWS Lambda and MongoDB Atlas
Webinar: Serverless Architectures with AWS Lambda and MongoDB AtlasWebinar: Serverless Architectures with AWS Lambda and MongoDB Atlas
Webinar: Serverless Architectures with AWS Lambda and MongoDB Atlas
MongoDB
 
SoCal NodeJS Meetup 20170215_aws_lambda
SoCal NodeJS Meetup 20170215_aws_lambdaSoCal NodeJS Meetup 20170215_aws_lambda
SoCal NodeJS Meetup 20170215_aws_lambda
Stefan Deusch
 
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02
Derek Ashmore
 
Deep Dive on AWS Lambda - January 2017 AWS Online Tech Talks
Deep Dive on AWS Lambda - January 2017 AWS Online Tech TalksDeep Dive on AWS Lambda - January 2017 AWS Online Tech Talks
Deep Dive on AWS Lambda - January 2017 AWS Online Tech Talks
Amazon Web Services
 
What's New in AWS Serverless and Containers
What's New in AWS Serverless and ContainersWhat's New in AWS Serverless and Containers
What's New in AWS Serverless and Containers
Amazon Web Services
 
Lambda and serverless - DevOps North East Jan 2017
Lambda and serverless - DevOps North East Jan 2017Lambda and serverless - DevOps North East Jan 2017
Lambda and serverless - DevOps North East Jan 2017
Mike Shutlar
 
AWS Lambda and Serverless Cloud
AWS Lambda and Serverless CloudAWS Lambda and Serverless Cloud
AWS Lambda and Serverless Cloud
Amazon Web Services
 
AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019
AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019
AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019
Derek Ashmore
 
AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019
AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019
AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019
Derek Ashmore
 
AWS Lambda for Architects - Chicago Coder Conference -2016-06-07
AWS Lambda for Architects - Chicago Coder Conference -2016-06-07AWS Lambda for Architects - Chicago Coder Conference -2016-06-07
AWS Lambda for Architects - Chicago Coder Conference -2016-06-07
Derek Ashmore
 
DevOps, Microservices and Serverless Architecture
DevOps, Microservices and Serverless ArchitectureDevOps, Microservices and Serverless Architecture
DevOps, Microservices and Serverless Architecture
Mikhail Prudnikov
 
Intro to AWS Lambda
Intro to AWS LambdaIntro to AWS Lambda
Intro to AWS Lambda
Sandra Garcia
 
Should you use container-based Lambdas_.pptx
Should you use container-based Lambdas_.pptxShould you use container-based Lambdas_.pptx
Should you use container-based Lambdas_.pptx
Samuel Durand
 
Real-time Data Processing Using AWS Lambda
Real-time Data Processing Using AWS LambdaReal-time Data Processing Using AWS Lambda
Real-time Data Processing Using AWS Lambda
Amazon Web Services
 
Going serverless with aws
Going serverless with awsGoing serverless with aws
Going serverless with aws
Alex Landa
 
AWS Community Day 2022 Dhiraj Mahapatro_AWS Lambda under the hood _ Best Prac...
AWS Community Day 2022 Dhiraj Mahapatro_AWS Lambda under the hood _ Best Prac...AWS Community Day 2022 Dhiraj Mahapatro_AWS Lambda under the hood _ Best Prac...
AWS Community Day 2022 Dhiraj Mahapatro_AWS Lambda under the hood _ Best Prac...
AWS Chicago
 

Similar to JDD2022: How to avoid common mistakes and misconceptions when working with Java on AWS Lambda (20)

AWS Serverless patterns & best-practices in AWS
AWS Serverless  patterns & best-practices in AWSAWS Serverless  patterns & best-practices in AWS
AWS Serverless patterns & best-practices in AWS
 
Serverless Computing
Serverless ComputingServerless Computing
Serverless Computing
 
Batch processing using AWS LAMBDA.pdf
Batch processing using AWS LAMBDA.pdfBatch processing using AWS LAMBDA.pdf
Batch processing using AWS LAMBDA.pdf
 
AWS re:Invent 2016: Monitoring, Hold the Infrastructure: Getting the Most fro...
AWS re:Invent 2016: Monitoring, Hold the Infrastructure: Getting the Most fro...AWS re:Invent 2016: Monitoring, Hold the Infrastructure: Getting the Most fro...
AWS re:Invent 2016: Monitoring, Hold the Infrastructure: Getting the Most fro...
 
Webinar: Serverless Architectures with AWS Lambda and MongoDB Atlas
Webinar: Serverless Architectures with AWS Lambda and MongoDB AtlasWebinar: Serverless Architectures with AWS Lambda and MongoDB Atlas
Webinar: Serverless Architectures with AWS Lambda and MongoDB Atlas
 
SoCal NodeJS Meetup 20170215_aws_lambda
SoCal NodeJS Meetup 20170215_aws_lambdaSoCal NodeJS Meetup 20170215_aws_lambda
SoCal NodeJS Meetup 20170215_aws_lambda
 
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02
Aws Lambda for Java Architects - Illinois JUG-Northwest -2016-08-02
 
Deep Dive on AWS Lambda - January 2017 AWS Online Tech Talks
Deep Dive on AWS Lambda - January 2017 AWS Online Tech TalksDeep Dive on AWS Lambda - January 2017 AWS Online Tech Talks
Deep Dive on AWS Lambda - January 2017 AWS Online Tech Talks
 
What's New in AWS Serverless and Containers
What's New in AWS Serverless and ContainersWhat's New in AWS Serverless and Containers
What's New in AWS Serverless and Containers
 
Lambda and serverless - DevOps North East Jan 2017
Lambda and serverless - DevOps North East Jan 2017Lambda and serverless - DevOps North East Jan 2017
Lambda and serverless - DevOps North East Jan 2017
 
AWS Lambda and Serverless Cloud
AWS Lambda and Serverless CloudAWS Lambda and Serverless Cloud
AWS Lambda and Serverless Cloud
 
AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019
AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019
AWS Lambda: Best Practices and Common Mistakes - Chicago Cloud Conference 2019
 
AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019
AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019
AWS Lambda: Best Practices and Common Mistakes - DevOps East 2019
 
AWS Lambda for Architects - Chicago Coder Conference -2016-06-07
AWS Lambda for Architects - Chicago Coder Conference -2016-06-07AWS Lambda for Architects - Chicago Coder Conference -2016-06-07
AWS Lambda for Architects - Chicago Coder Conference -2016-06-07
 
DevOps, Microservices and Serverless Architecture
DevOps, Microservices and Serverless ArchitectureDevOps, Microservices and Serverless Architecture
DevOps, Microservices and Serverless Architecture
 
Intro to AWS Lambda
Intro to AWS LambdaIntro to AWS Lambda
Intro to AWS Lambda
 
Should you use container-based Lambdas_.pptx
Should you use container-based Lambdas_.pptxShould you use container-based Lambdas_.pptx
Should you use container-based Lambdas_.pptx
 
Real-time Data Processing Using AWS Lambda
Real-time Data Processing Using AWS LambdaReal-time Data Processing Using AWS Lambda
Real-time Data Processing Using AWS Lambda
 
Going serverless with aws
Going serverless with awsGoing serverless with aws
Going serverless with aws
 
AWS Community Day 2022 Dhiraj Mahapatro_AWS Lambda under the hood _ Best Prac...
AWS Community Day 2022 Dhiraj Mahapatro_AWS Lambda under the hood _ Best Prac...AWS Community Day 2022 Dhiraj Mahapatro_AWS Lambda under the hood _ Best Prac...
AWS Community Day 2022 Dhiraj Mahapatro_AWS Lambda under the hood _ Best Prac...
 

Recently uploaded

Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Zilliz
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
Pixlogix Infotech
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 

Recently uploaded (20)

Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 

JDD2022: How to avoid common mistakes and misconceptions when working with Java on AWS Lambda

  • 1. How to avoid common mistakes and misconceptions when working with Java on AWS Lambda Andrzej Dębski
  • 3. About me 1. A decade of experience with JVM languages, mostly Java and Scala 2. Working (mostly) on backend services 3. Pratical experience with AWS Lambda since February 2021 • Maintenance and development of 2 existing services • Development of additional 2 services/products on AWS Lambda from scratch
  • 4. Agenda 1. Cold starts (generic and those specific to JVM) 2. AWS Lambda Pricing 3. In-memory caching on Lambda
  • 7.
  • 8.
  • 9.
  • 10. Cold start type 1 (container recycle) Time Duration (ms) Init Duration (ms) 18:16:32 85.24 1821.08 18:18:50 1.95 N/A 18:24:05 1.74 N/A 18:30:40 72.91 1870.40
  • 11.
  • 12. Cold start type 2 (JVM JIT and lazy load) Code path # Duration (ms) Notes Code path 1 731.32 First request, cold start + JVM JIT Code path 1 19.86 Code is already loaded by JVM, no cold start Code path 1 4.31 Further optimizations Code path 2 1261.40 JVM executes new branch for the first time Code path 2 2.12 No JVM overhead
  • 13. Reducing cold starts 1. Provisioned concurrency (best but costly) • Keeps a set of instances ready to respond to requests 2. "Background" traffic (best effort) 3. Use a different language like Go or use AOT compilation through GraalVM • https://shinesolutions.com/2021/08/30/improving-cold- start-times-of-java-aws-lambda-functions-using- graalvm-and-native-images/
  • 14.
  • 15. Cold start for provisioned concurrency Code path # Duration (ms) Notes Code path 1 33.12 Small overhead, first request to Lambda Code path 1 2.09 Code path 1 2.34 Code path 2 2.03 New request type handled for the first time Code path 2 2.4
  • 16. Provisioned concurrency best practices 1. Make sure the function is invoked using the alias. 2. Monitor (and alarm) on metrics • ProvisionedConcurrencySpilloverInvocations • ProvisionedConcurrencyUtilization 3. Pre-warm the code paths in the constructor 4. Use code deploy policies to gradually deploy new function revisions.
  • 17. Recap 1. Cold starts are real and they affect the JVM Lambdas even more. 2. Either accept them or pay for provisioned concurrency (and use it well). 3. Monitor and instrument your functions to understand the bottlenecks in your code.
  • 18. $$$
  • 19. Lambda pricing 1. Lambda price (https://aws.amazon.com/lambda/pricing/): • On demand: billed for every GB-second (sum(duration ) * allocated memory) + number of invocations • Provisioned concurrency is more complicated. We pay for keeping the containers "warm" 2. Free-tier
  • 20. How to calculate AWS costs 1. AWS pricing calculator • https://calculator.aws/#/ 2. Cost estimates for the following examples: • https://tinyurl.com/yk7v3wek
  • 21.
  • 22. Assumptions 1. "Average" request time: 200 ms 2. Monthly costs 3. Region: us-east-1 4. Focus on compute cost, ignore everything else
  • 23. Scenarios considered 1. "Hello world": on-demand vs provisioned capacity 2. Smallest Fargate vs Lambda 3. StackExchange on Lambda vs Fargate
  • 24. "Hello world", 1 request per second 256 MB RAM On demand Provisioned capacity of 1 0.33 $ 4.55 $
  • 25. Fargate, 1 request per second 1769 MB RAM 1vCPU 2GB RAM On demand Provisioned capacity of 1 Fargate 8.8 $ 28.28 $ 36.04 $
  • 26.
  • 27. Simulating Stack Exchange 1. https://stackexchange.com/performance • 1.3* 10^9 monthly page views means ~495 requests per second • For Fargate we assume 9 tasks to protect from AZ outage, 82.5 RPS per task. • For Lambda provisioned concurrency try to handle every request with prov capacity. • 495 (RPS) / 5 (requests per second per container) = 99
  • 28. SE, 495 requests per second 1769 MB RAM 1vCPU 2GB RAM On demand Provisioned capacity of 99 Fargate, 9 tasks 7,744.27 $ 6,502.63 $ 324.36 $
  • 29.
  • 30. Recap/tips&tricks 1. Free-tier 2. Use the AWS Pricing Calculator 3. With more traffic Lambda gets really costly 4. Take advantage of the "free" compute during init phase for on-demand lambdas 5. For consistent workloads provisioned concurrency may be cheaper 6. Always consider factors other than $$$ when evaluating technologies
  • 33.
  • 34.
  • 35.
  • 36. Caching in Lambda - challenges 1. No hard guarantees when the container will be recycled 2. Best effort async updates 3. No control over the routing algorithm (no sticky sessions)
  • 37. Caching in Lambda - approaches 1. (Mostly) rely on out of process cache 2. Use different compute platform if L1 cache is critical 3. Use Lambda extensions • That's how AWS AppConfig does it
  • 38. Caching in Lambda - extensions 1. Code that can execute alongside your Lambda 2. Extension continues to execute AFTER the Lambda handler returned a response. 3. Implementing cache in extension • Save the data on Lambda FS • Expose local HTTP server and serve the data
  • 40. Extensions for caching 1. (+) "Asynchronous" cache updates 2. (-) Overhead (in ms): • Avg (no ext/ext): 1.88 ms/14.1 ms • Tm99.9 (no ext/ext): 1.88 ms/14.2 ms 3. (-) Lambda instance can't handle new requests until extension is done 4. (-) Extension instances are not shared between containers
  • 41. Recap 1. In memory caching in Lambda is not straightforward • Small window of time where the cache is "warm" • Asynchronous updates are best effort only 2. Extensions improve the situation but have their own downsides • Runtime overhead • Complicate the flow 3. L2 cache or different compute platform
  • 42. How to avoid common mistakes and misconceptions when working with Java on AWS Lambda Andrzej Dębski