Slide 1

Slide 1 text

Alex Casalboni Sr. Developer Advocate, AWS @alex_casalboni Data-driven & cost-aware performance tuning for AWS Lambda @ 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved

Slide 2

Slide 2 text

About me • Software Engineer & Web Developer • Data science background • Worked in a US startup for 4.5 years • ServerlessDays Milan co-organizer • AWS customer since 2013

Slide 3

Slide 3 text

Fundamentals (very quickly!) Optimization best practices Idea: data-driven approach Real-world examples & patterns Agenda

Slide 4

Slide 4 text

Fundamentals

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

AWS operational responsibility models On-premises Cloud Less More Compute Virtual machine Amazon EC2 AWS Elastic Beanstalk AWS Lambda AWS Fargate Databases MySQL MySQL on EC2 Amazon RDS MySQL Amazon Aurora Aurora Serverless Amazon QLDB/DynamoDB Storage Storage Amazon S3 Messaging ESBs Amazon MQ Amazon Kinesis Amazon EventBridge/SNS/SQS Analytics Hadoop Hadoop on EC2 Amazon EMR Amazon Elasticsearch Service Amazon Athena

Slide 7

Slide 7 text

Anatomy of a Lambda function Handler() function Function to be executed upon invocation Event object Data sent during Lambda function Invocation Context object Methods available to interact with runtime information (request ID, log group, more) import json def lambda_handler(event, context): # TODO implement return { 'statusCode': 200, 'body': json.dumps('Hello World!') }

Slide 8

Slide 8 text

The request lifecycle Bootstrap the runtime Start your code Cold start Warm start Download your code Start new micro vm AWS optimization Your optimization

Slide 9

Slide 9 text

Lambda + VPC = ß Before: 14.8 sec duration

Slide 10

Slide 10 text

Lambda + VPC = ß Before: 14.8 sec duration After: 933ms duration à

Slide 11

Slide 11 text

Optimization best practices

Slide 12

Slide 12 text

Avoid «monolithic» functions Reduce deployment package size Micro/Nano services Optimize dependencies (and imports) E.g. up to 120ms faster with Node.js SDK Optimization best practices (cold start) Minify/uglify production code Browserify/Webpack Lazy initialization of shared libs/objs Helps if multiple functions per file

Slide 13

Slide 13 text

Provisioned Concurrency for AWS Lambda New! Managed solution to avoid “cold starts” You set provisioned concurrency for individual functions No code changes required Integrated with AWS Auto Scaling Provisioning rampup (500/min) Wide support (CloudFormation, Terraform, Serverless Framework, Datadog, Epsagon, Lumigo, Thundra, etc.)

Slide 14

Slide 14 text

Externalize orchestration Avoid idle/sleep – delegate to Step Functions Transform, not Transport Minimize data transfer (S3 Select, advanced filtering, etc.) Discard uninteresting events asap Trigger config (S3 prefix, SNS filter, etc.) Fine-tune resources allocation Don’t guesstimate function memory Lambda Destinations Simplified chaining (async) and DLQ Keep in mind retry policies Very powerful (and configurable), but not free Optimization best practices (performance/cost)

Slide 15

Slide 15 text

Externalize orchestration Avoid idle/sleep – delegate to Step Functions Transform, not Transport Minimize data transfer (S3 Select, advanced filtering, etc.) Discard uninteresting events asap Trigger config (S3 prefix, SNS filter, etc.) Fine-tune resources allocation Don’t guesstimate function memory Lambda Destinations Simplified chaining (async) and DLQ Keep in mind retry policies Very powerful (and configurable), but not free Optimization best practices (performance/cost)

Slide 16

Slide 16 text

Resources allocation Memory Power

Slide 17

Slide 17 text

CPU-bound example “Compute 1,000 times all prime numbers <= 1M” 128 MB 11.722 sec $0.024628 256 MB 6.678 sec $0.028035 512 MB 3.194 sec $0.026830 1024 MB 1.465 sec $0.024638

Slide 18

Slide 18 text

CPU-bound example “Compute 1,000 times all prime numbers <= 1M” 128 MB 11.722 sec $0.024628 256 MB 6.678 sec $0.028035 512 MB 3.194 sec $0.026830 1024 MB 1.465 sec $0.024638

Slide 19

Slide 19 text

CPU-bound example

Slide 20

Slide 20 text

Cost-aware performance optimization 310ms 400ms 294ms 300ms 5% performance optimization 25% cost optimization 480ms 500ms 408ms 500ms 15% performance optimization 0% cost optimization A B

Slide 21

Slide 21 text

Idea: data-driven approach

Slide 22

Slide 22 text

Data-driven cost & performance optimization for AWS Lambda Available as a SAR app Easy to integrate with CI/CD Meet AWS Lambda Power Tuning github.com/alexcasalboni/aws-lambda-power-tuning

Slide 23

Slide 23 text

github.com/alexcasalboni/aws-lambda-power-tuning v1 v2 v3

Slide 24

Slide 24 text

AWS Lambda Power Tuning (input) github.com/alexcasalboni/aws-lambda-power-tuning

Slide 25

Slide 25 text

AWS Lambda Power Tuning (output) github.com/alexcasalboni/aws-lambda-power-tuning

Slide 26

Slide 26 text

AWS Lambda Power Tuning (visualization) github.com/alexcasalboni/aws-lambda-power-tuning

Slide 27

Slide 27 text

Real-world examples and patterns

Slide 28

Slide 28 text

No-Op (trivial data manipulation <100ms)

Slide 29

Slide 29 text

CPU-bound (numpy: inverting 1500x1500 matrix)

Slide 30

Slide 30 text

CPU-bound (prime numbers)

Slide 31

Slide 31 text

CPU-bound (prime numbers – more granularity)

Slide 32

Slide 32 text

Network-bound (third-party API call)

Slide 33

Slide 33 text

Network-bound (3x DDB queries)

Slide 34

Slide 34 text

Network-bound (S3 download – 150MB)

Slide 35

Slide 35 text

Network-bound (S3 download multithread – 150MB)

Slide 36

Slide 36 text

Cost/Performance patterns github.com/alexcasalboni/aws-lambda-power-tuning

Slide 37

Slide 37 text

Takeaways

Slide 38

Slide 38 text

Takeaways Optimization best practices (what are you trying to optimize?) Avoid cold starts with Provisioned Concurrency Memory Power Optimal resources allocation can be automated (CI/CD) Think in terms of workload categories and cost/performance patterns Visualize optimal trade-offs with /alexcasalboni/aws-lambda-power-tuning

Slide 39

Slide 39 text

Go Build! Here to help you build

Slide 40

Slide 40 text

Alex Casalboni Sr. Developer Advocate, AWS @alex_casalboni @ 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved Thank you!