AWS Lambdaのスロットリングについて、同時実行数のクオータの考え方や、緩和の方法をご紹介します。
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.24th Jun 2020Kensuke ShimokawaAWS Lambdaのスロットリング緩和- How to manage throttling@_kensh
View Slide
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Who am I?NameKensuke ShimokawaCompanyAmazon Web Services Japan K.K.RoleServerless Specialist Solutions Architect@_kensh
© 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
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Scalabilityの基本Mostly, about AWS Lambda
© 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
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Throttling is not EvilWhat is Throttling?スロットリングは、リソースとダウンストリームアプリケーションを保護することを目的としています。Lambda は受信トラフィックに合わせて自動的にスケーリングしますが、さまざまな理由で関数がスロットリングされる可能性があります。https://aws.amazon.com/premiumsupport/knowledge-center/lambda-troubleshoot-throttlingLambdaを呼び出すLambdaから呼び出されるAWS Lambdaアップストリーム ダウンストリーム
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Throttling is not Evil
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Throttling is not Evilアカウント、リージョン毎
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Throttling is not EvilAWS では 160 種類を超えるクラウドサービスについて従量制料金(Pay as You go)を適用しています。Year
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Throttling is not EvilAWS では 160 種類を超えるクラウドサービスについて従量制料金(Pay as You go)を適用しています。Imagine there's no ThrottlingIt's easy if you tryYou go You payYear
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.関数のスケーリングと同時実行数
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.関数のスケーリングと同時実行数Concurrency = 同時実行数
© 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 RegionsConcurrency = 同時実行数
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.関数のスケーリングと同時実行数最初のバーストの後、関数の同時実行数は、1 分ごとにさらに 500 インスタンス増加します。Concurrency = 同時実行数
© 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 Regionshttps://docs.aws.amazon.com/lambda/latest/dg/invocation-scaling.html最初のバーストの後、関数の同時実行数は、1 分ごとにさらに 500 インスタンス増加します。
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Quotaについて
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Quota は共有される
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.同時実行数はアカウント、リージョン単位で共有• 各Lambda関数が、Quotaで設定された同時実行数を共有する• 各関数のConcurrencyの合計が 、同時実行数の制限に達した場合Throttleする。AWS CloudRegionQuota: 1000(default)Function A Function B Function C
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.同時実行数はアカウント、リージョン単位で共有AWS Cloud AWS Cloud AWS CloudRegionRegionRegionRegionRegionRegionQuota: 1000(default)
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.[email protected] 関数についてhttps://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cloudfront-limits.html#limits-lambda-at-edgeAWS CloudRegion XQuota: 1000Edge AReplicasRegional B Regional Cus-east-1Edge A (artifact)• [email protected]は、us-east-1のマスター関数を他のRegionに複製• CloudFrontへのアクセスにより、適切なRegionでLambda関数の複製を実行• 複製先でのConcurrencyQuotaはRegional Lambdaと共有behaviour 設定時に複製https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-edge-how-it-works.htmlAmazonCloudFrontDev Viewersclose toRegion XbehaviourviewerrequestMaster createinvokeAWS Lambda
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Throttling
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Throttlingの確認
© 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
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.どの関数がSpikeしているか確認(per Region)150 150 150130 140210180570300200300280 28030035066061010007306901000 1000 1000 1000 100002004006008001000120010:04 10:05 10:06 10:07 10:08Concurrencyfunc1 func2 func3 sum Quota
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.どの関数がSpikeしているか確認(per Region)150 150 150130 140210180570300200300280 28030035066061010007306901000 1000 1000 1000 100002004006008001000120010:04 10:05 10:06 10:07 10:08Concurrencyfunc1 func2 func3 sum QuotaHit 1000
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.どの関数がSpikeしているか確認(per Region)150 150 150130 140210180570300200300280 28030035066061010007306901000 1000 1000 1000 100002004006008001000120010:04 10:05 10:06 10:07 10:08Concurrencyfunc1 func2 func3 sum QuotaHit 1000Spike?
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Throttlingの対処
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.• Duration Metricsに着目• 関数Errorの急増に着目• Recursive Callに注意• Synchronous Callでないといけないか• Throttlingしてはいけない関数か• Service Quota request• Provisioned ConcurrencyThrottlingの対処
© 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.htmlThrottlingの対処
© 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/
© 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"}}'
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.• Recursive Callに注意Amazon Simple StorageServiceAWS LambdainvokesPut ObjectAWS CloudTrailAmazon EventBridge AWS LambdainvokesGetFunctionConfigurationTriggered by RulecapturesThrottlingの対処
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Throttlingの対処• Synchronous Callでないといけないか?• Asynchronous にできないか検討• Latencyからの解放• 呼び出し元の待機が不要• 呼び出し元が別処理のために稼働できる• Asynchronous のResiliency• 多様なErrorハンドリングのサポート• Throttlingの緩和• 分散システムを疎結合に
© 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
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.SIDE LINE Pattern• メインの処理がThrottleするのを緩和する(健全性を担保したい)• メイン処理に同時実行数を割り大きく当て、メイン処理では異常データを検知しSide Line(副処理)に退避する。• Side Lineの同時実行数を比較的小さく抑えておく
© 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設定値
© 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.htmlSend Message maxReceiveCount(= 3 )同時実行数小設定値
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Amazon Kinesis Data Streams :: Side Line PatternSide Line処理メイン処理• Lambdaの前段にKinesis Data Streamsを配置• LambdaにDestinationを設定し、エラーデータを退避同時実行数小設定値
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Throttlingの対処• Throttlingしてはいけない関数か
© 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
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Throttlingの対処• Service Quota request
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Throttling is not Evil :: A gentle reminderAWS では 160 種類を超えるクラウドサービスについて従量制料金(Pay as You go)を適用しています。Imagine there's no ThrottlingIt's easy if you tryYou go You pay
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Provisioned ConcurrencyTo Be Continued … to Day2
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Provisioned Concurrency1st JULYProvisioned ConcurrencyDive Deep & Practice
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.まとめ• Provisioned Concurrencyを設定する前に、関数チューニングや、リアーキテクチャなど対応できることが多くあることをご紹介しました。• Throttlingは、サービスにとってなくてはならない機能です。• うまく、Throttlingをコントロールして、 Resilientなシステムにしましょう。
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.Thank you !@_kensh