SlideShare une entreprise Scribd 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

Contenu connexe

Similaire à 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
 

Similaire à 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...
 

Dernier

Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
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
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
Pravash Chandra Das
 
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStrDeep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
saastr
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
Dinusha Kumarasiri
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 

Dernier (20)

Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
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
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
 
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStrDeep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 

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