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

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

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

    View Slide

  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

    View Slide

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

    View Slide

  4. Fundamentals

    View Slide

  5. View Slide

  6. 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

    View Slide

  7. 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!')
    }

    View Slide

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

    View Slide

  9. Lambda + VPC =
    ß Before: 14.8 sec duration

    View Slide

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

    View Slide

  11. Optimization best practices

    View Slide

  12. 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

    View Slide

  13. 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.)

    View Slide

  14. 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)

    View Slide

  15. 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)

    View Slide

  16. Resources allocation
    Memory Power

    View Slide

  17. 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

    View Slide

  18. 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

    View Slide

  19. CPU-bound example

    View Slide

  20. 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

    View Slide

  21. Idea: data-driven approach

    View Slide

  22. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  27. Real-world examples
    and patterns

    View Slide

  28. No-Op (trivial data manipulation <100ms)

    View Slide

  29. CPU-bound (numpy: inverting 1500x1500 matrix)

    View Slide

  30. CPU-bound (prime numbers)

    View Slide

  31. CPU-bound (prime numbers – more granularity)

    View Slide

  32. Network-bound (third-party API call)

    View Slide

  33. Network-bound (3x DDB queries)

    View Slide

  34. Network-bound (S3 download – 150MB)

    View Slide

  35. Network-bound (S3 download multithread – 150MB)

    View Slide

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

    View Slide

  37. Takeaways

    View Slide

  38. 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

    View Slide

  39. Go Build!
    Here to help you build

    View Slide

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

    View Slide