Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Data-driven & cost-aware performance tuning for...

Data-driven & cost-aware performance tuning for AWS Lambda

How can we combine cost optimization and performance tuning into a single data-driven process, and even integrate it into our CI/CD pipelines? This talk will showcase an open-source tool to optimize Lambda functions resource allocation without requiring complex refactors or architectural changes, while showcasing charts and cost/performance patterns of real-world use cases.

Alex Casalboni

February 21, 2020
Tweet

More Decks by Alex Casalboni

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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!') }
  5. The request lifecycle Bootstrap the runtime Start your code Cold

    start Warm start Download your code Start new micro vm AWS optimization Your optimization
  6. 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
  7. 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.)
  8. 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)
  9. 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)
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. Alex Casalboni Sr. Developer Advocate, AWS @alex_casalboni @ 2019, Amazon

    Web Services, Inc. or its Affiliates. All rights reserved Thank you!