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

AWS Lambdaのスロットリング緩和 / How to manage throttling for AWS Lambda

kensh
June 24, 2020

AWS Lambdaのスロットリング緩和 / How to manage throttling for AWS Lambda

AWS Lambdaのスロットリングについて、同時実行数のクオータの考え方や、緩和の方法をご紹介します。

kensh

June 24, 2020
Tweet

More Decks by kensh

Other Decks in Technology

Transcript

  1. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. 24th Jun 2020 Kensuke Shimokawa AWS Lambdaの スロットリング緩和 - How to manage throttling @_kensh
  2. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Who am I? Name Kensuke Shimokawa Company Amazon Web Services Japan K.K. Role Serverless Specialist Solutions Architect @_kensh
  3. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Table of contents • Day1 24th June • How to manage throttling • Day2 1st July • Provisioned Concurrency Dive Deep & Practice
  4. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Scalabilityの基本 Mostly, about AWS Lambda
  5. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. © 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Throttling is not Evil
  6. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttling is not Evil What is Throttling? スロットリングは、リソースとダウンストリームアプリケーションを保護するこ とを目的としています。Lambda は受信トラフィックに合わせて自動的にスケー リングしますが、さまざまな理由で関数がスロットリングされる可能性がありま す。 https://aws.amazon.com/premiumsupport/knowledge-center/lambda-troubleshoot-throttling Lambdaを呼び 出す Lambdaから 呼び出される AWS Lambda アップストリーム ダウンストリーム
  7. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttling is not Evil
  8. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttling is not Evil アカウント、リージョン毎
  9. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttling is not Evil AWS では 160 種類を超えるクラウドサービス について従量制料金(Pay as You go)を適用しています。 Year
  10. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttling is not Evil AWS では 160 種類を超えるクラウドサービス について従量制料金(Pay as You go)を適用しています。 Imagine there's no Throttling It's easy if you try You go You pay Year
  11. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. © 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 関数のスケーリングと同時実行数
  12. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. 関数のスケーリングと同時実行数 Concurrency = 同時実行数
  13. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. 関数のスケーリングと同時実行数 バースト制限 • 3000 – US West (Oregon), US East (N. Virginia), Europe (Ireland) • 1000 – Asia Pacific (Tokyo), Europe (Frankfurt) • 500 – Other Regions Concurrency = 同時実行数
  14. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. 関数のスケーリングと同時実行数 最初のバーストの後、関数の同時実行数は、 1 分ごとにさらに 500 インスタンス増加します。 Concurrency = 同時実行数
  15. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. 関数のスケーリングと同時実行数 バースト制限 3000 – US West (Oregon), US East (N. Virginia), Europe (Ireland) 1000 – Asia Pacific (Tokyo), Europe (Frankfurt) 500 – Other Regions https://docs.aws.amazon.com/lambda/latest/dg/invocation-scaling.html 最初のバーストの後、関数の同時実行数は、1 分ごと にさらに 500 インスタンス増加します。
  16. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. © 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Quota は共有される
  17. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. 同時実行数はアカウント、リージョン単位で共有 • 各Lambda関数が、Quotaで設定された同時実 行数を共有する • 各関数のConcurrencyの合計が 、同時実行数の 制限に達した場合Throttleする。 AWS Cloud Region Quota: 1000(default) Function A Function B Function C
  18. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. 同時実行数はアカウント、リージョン単位で共有 AWS Cloud AWS Cloud AWS Cloud Region Region Region Region Region Region Quota: 1000(default)
  19. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Lambda@Edge 関数について https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cloudfront- limits.html#limits-lambda-at-edge AWS Cloud Region X Quota: 1000 Edge A Replicas Regional B Regional C us-east-1 Edge A (artifact) • Lambda@Edgeは、us- east-1のマスター関数を他 のRegionに複製 • CloudFrontへのアクセスに より、適切なRegionで Lambda関数の複製を実行 • 複製先でのConcurrency QuotaはRegional Lambda と共有 behaviour 設定時に複製 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-edge-how-it- works.html Amazon CloudFront Dev Viewers close to Region X behaviour viewer request Master create invoke AWS Lambda
  20. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. © 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Throttlingの確認
  21. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttling ? になったら 関数の Error メトリクスの増加を確認 Duration メトリクスのスパイクを確認 バーストスケーリングによっての制限か確認 (limit increased) リージョンのバースト同時実行クォータを超えているかどうかを確認 Lambda Throttles Metricsがあるか? ない場合は、Code内APIを調査 CloudWatch LogsにThrottoling が出力されているか確認 https://aws.amazon.com/jp/premiumsupport/knowledge-center/lambda-troubleshoot-throttling
  22. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. どの関数がSpikeしているか確認(per Region) 150 150 150 130 140 210 180 570 300 200 300 280 280 300 350 660 610 1000 730 690 1000 1000 1000 1000 1000 0 200 400 600 800 1000 1200 10:04 10:05 10:06 10:07 10:08 Concurrency func1 func2 func3 sum Quota
  23. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. どの関数がSpikeしているか確認(per Region) 150 150 150 130 140 210 180 570 300 200 300 280 280 300 350 660 610 1000 730 690 1000 1000 1000 1000 1000 0 200 400 600 800 1000 1200 10:04 10:05 10:06 10:07 10:08 Concurrency func1 func2 func3 sum Quota Hit 1000
  24. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. どの関数がSpikeしているか確認(per Region) 150 150 150 130 140 210 180 570 300 200 300 280 280 300 350 660 610 1000 730 690 1000 1000 1000 1000 1000 0 200 400 600 800 1000 1200 10:04 10:05 10:06 10:07 10:08 Concurrency func1 func2 func3 sum Quota Hit 1000 Spike?
  25. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. © 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Throttlingの対処
  26. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. • Duration Metricsに着目 • 関数Errorの急増に着目 • Recursive Callに注意 • Synchronous Callでないといけないか • Throttlingしてはいけない関数か • Service Quota request • Provisioned Concurrency Throttlingの対処
  27. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. • Duration Metricsに着目 • Durationが一時的に伸びている場合、同時実行数を消費することも • 外部リソースへのアクセスが正常かを確認 • 外部リソースへのアクセスに対してExponential Backoffが実装されている場合 • DynamoDBのキャパシティ枯渇時(On-Demandの検討も) • 関数内の呼び出し先On-prem HTTPサーバーのダウン時 (Cacheの検討も) • Lambda Concurrency Hunt • https://github.com/aws-samples/aws-lambda-concurrency-hunt • コードチューニング • AWS Lambda 関数を使用する際のベストプラクティス • https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/best-practices.html Throttlingの対処
  28. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Lambda Concurrency Hunt ( Tips ) 過去7日間のMetricsに対して 同時実行数が最も高い期間を 見つけ、そのSpikeの前の6 分間の情報を出力する。 • 関数の呼び出し回数 • 平均処理時間 • 同時実行数 $ curl https://raw.githubusercontent.com/aws-samples/aws-lambda-concurrency-hunt/master/lambda-con-hunt.py -o lambda-con-hunt.py $ python3 lambda-con-hunt.py ※ Lambdaの1 分間隔Metricsにおいて、15 日間は 1 分の分解能を持つ。以降も使用可能だが、5 分に 集約された分解能となる https://aws.amazon.com/jp/cloudwatch/faqs/
  29. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. • Duration Metricsに着目 • 関数Errorの急増に着目 • Recursive Callに注意 • Synchronous Callでないといけないか • Throttlingしてはいけない関数か • Service Quota request • Provisioned Concurrency Throttlingの対処
  30. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttlingの対処 • 関数Errorの急増に着目 • 関数の Error メトリクスの増加を確認 • 同期呼び出しでErrorが増えると • 呼び出し元の再試行が発生(SDK Exponential の仕様を要確認) • 呼び出しが全体的に増加 • 非同期呼び出しの場合 • Lambda は失敗した呼び出しをさらに 最大2 回再試行(configurable) • On failure Destination でエラーハンドリングも検討(configurable) $ aws lambda put-function-event-invoke-config --function-name func \ --maximum-event-age-in-seconds 3600 --maximum-retry-attempts 0 $ aws lambda update-function-event-invoke-config --function-name error \ --destination-config '{ "OnFailure": { "Destination": "arn:aws:sqs:us-east-2:123456789012:destination" } }'
  31. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. • Duration Metricsに着目 • 関数Errorの急増に着目 • Recursive Callに注意 • Synchronous Callでないといけないか • Throttlingしてはいけない関数か • Service Quota request • Provisioned Concurrency Throttlingの対処
  32. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. • Recursive Callに注意 Amazon Simple Storage Service AWS Lambda invokes Put Object AWS CloudTrail Amazon EventBridge AWS Lambda invokes GetFunctionConfiguration Triggered by Rule captures Throttlingの対処
  33. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. • Duration Metricsに着目 • 関数Errorの急増に着目 • Recursive Callに注意 • Synchronous Callでないといけないか • Throttlingしてはいけない関数か • Service Quota request • Provisioned Concurrency Throttlingの対処
  34. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttlingの対処 • Synchronous Callでないといけないか? • Asynchronous にできないか検討 • Latencyからの解放 • 呼び出し元の待機が不要 • 呼び出し元が別処理のために稼働できる • Asynchronous のResiliency • 多様なErrorハンドリングのサポート • Throttlingの緩和 • 分散システムを疎結合に
  35. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttlingの対処 • Synchronous Callでないといけないか? • Asynchronous にできないか検討 • Latencyからの解放 • 呼び出し元の待機が不要 • 呼び出し元が別処理のために稼働できる • Asynchronous のResiliency • 多様なErrorハンドリングのサポート • Throttlingの緩和 • 分散システムを疎結合に AWS LambdaでのAsync • Lambda Async Invoke • SQS Event Source • Kinesis Event Source
  36. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. SIDE LINE Pattern • メインの処理がThrottleするのを緩和する(健全性を担保したい) • メイン処理に同時実行数を割り大きく当て、メイン処理では異常デー タを検知しSide Line(副処理)に退避する。 • Side Lineの同時実行数を比較的小さく抑えておく
  37. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Lambda Async Invoke :: Side Line Pattern • LambdaのAsync Invokeは、Lambdaサービス内にQueueを持つ • LambdaにDestinationを設定し、エラーデータを退避 Side Line処理 Retry2回を超えたメッセージは メイン処理 同時実行数小 Async Invoke (Internal Queue) retry=2 設定値
  38. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Amazon SQS event sources :: Side Line Pattern • Lambdaの前段にSQSを配置 • QueueにDLQを設定し、エラーデータを退避 Side Line処理 maxReceiveCount(= 3 )を超えた メッセージは メイン処理 https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SetQueueAttributes.html Send Message maxReceiveCount(= 3 ) 同時実行数小 設定値
  39. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Amazon Kinesis Data Streams :: Side Line Pattern Side Line処理 メイン処理 • Lambdaの前段にKinesis Data Streamsを配置 • LambdaにDestinationを設定し、エラーデータを退避 同時実行数小 設定値
  40. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. • Duration Metricsに着目 • 関数Errorの急増に着目 • Recursive Callに注意 • Synchronous Callでないといけないか • Throttlingしてはいけない関数か • Service Quota request • Provisioned Concurrency Throttlingの対処
  41. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttlingの対処 • Throttlingしてはいけない関数か
  42. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttlingの対処 • Throttlingしてもよい関数の場合 • 予約された同時実行数を用いて、spikeを抑制 • 同時実行数を予約割り当てすることで、指定の数だけ占有 • 割り当てた同時実行数を超えるとThrottleする • 予約割り当てされた関数以外の関数 • アカウント、リージョン単位の残りの同時実行数を共有 • Spikyな関数による、他の関数への影響を緩和 $ aws lambda put-function-concurrency \ --function-name my-function \ --reserved-concurrent-executions 100
  43. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. • Duration Metricsに着目 • 関数Errorの急増に着目 • Recursive Callに注意 • Synchronous Callでないといけないか • Throttlingしてはいけない関数か • Service Quota request • Provisioned Concurrency Throttlingの対処
  44. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttlingの対処 • Service Quota request
  45. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttlingの対処 • Service Quota request
  46. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttlingの対処 • Service Quota request
  47. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttlingの対処 • Service Quota request
  48. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Throttling is not Evil :: A gentle reminder AWS では 160 種類を超えるクラウドサービス について従量制料金(Pay as You go)を適用しています。 Imagine there's no Throttling It's easy if you try You go You pay
  49. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. • Duration Metricsに着目 • 関数Errorの急増に着目 • Recursive Callに注意 • Synchronous Callでないといけないか • Throttlingしてはいけない関数か • Service Quota request • Provisioned Concurrency Throttlingの対処
  50. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Provisioned Concurrency To Be Continued … to Day2
  51. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Provisioned Concurrency 1st JULY Provisioned Concurrency Dive Deep & Practice
  52. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. まとめ • Provisioned Concurrencyを設定する前に、関数チューニングや、リアーキテ クチャなど対応できることが多くあることをご紹介しました。 • Throttlingは、サービスにとってなくてはならない機能です。 • うまく、Throttlingをコントロールして、 Resilientなシステムにしましょう。
  53. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Thank you ! @_kensh