ゲームやスポーツイベントのチケットの販売開始、そして特売や他の大規模なセールなどでトラフィックの大規模なバースト中にウェブサイトがダウンしないようにリクエストをバッファリングするということはよくあると思います。このような場合にAWSのサーバーレスでどのようにシステムを組んでおけばよいでしょうか? このセッションではAWSのソリューションを例に一緒に考えてみたいと思います。
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.S E R V E R L E S S D A Y S T O K Y O 2 0 2 3Serverless Virtual Waiting Roomサーバーレスで仮想待合室を作ろう!Kensuke ShimokawaSnr. Serverless SpecialistAmazon Web Services Japan
View Slide
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Kensuke ShimokawaAmazon Web Services JapanSnr. Serverless SpecialistSlides https://speakerdeck.com/_kenshQiita https://qiita.com/_kensh
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Agenda• 負荷が高い時にどうするか?• Traffic Pattern で考える。• Burst が Spike に追いつかない場合を考える。• Shift North に対するブロッカーについて考える。• 要件定義から Shift North できないか考える。• Virtual Waiting Room• Performance testing• Demo3
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.負荷が高い時にどうする?4
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.どんなときに突発的に負荷が上がるか?5ワクチン接種開始※ 公共サービス 災害などの速報アーティストのライブ配信
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Traffic Pattern で考える。
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Traffic pattern7High, stable High, bumpy High, spiky
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.High, stable / bumpy / spiky8https://speakerdeck.com/_kensh/aws-lambda-performance-tuning-deep-dive
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Traffic pattern / 今回のターゲット9High, stable High, bumpy High, spiky
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Traffic pattern10High, spikyHigh, spiky High, spikyProvisioning Auto scale Request Basisハンドルできていないトラフィック余剰リソース余剰リソース
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Request Basis で考えたいシステム特徴11High, spikyRequest Basis• Burst 性を持つこと§ 俊敏に Ramp-up すること• Scale-in もしっかりしてくれることAmazon API Gateway AWS LambdaBurst : 5000 Burst : 1000〜3000
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Burst が Spike に追いつかない場合を考える。
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Traffic の方向性• 今回、考えたいのはNorth-South Traffic13API Gatewayclient clientclientNorth-South TrafficEast-West Traffic
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Shift North15API Gatewayclient clientclientNorthSouth• Traffic の終端をできるだけ Northに寄せていくTrafficterminate
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Shift North16API Gatewayclient clientclientNorthSouth• Traffic の終端をできるだけ Northに寄せていく• Queue や Stream で Traffic を終端させて、HTTP status 202Accepted を返却QueueStreamTrafficterminateShiftNorth
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Shift North17client clientclientNorthSouthCDN• Traffic の終端をできるだけ Northに寄せていく• Queue や Stream で Traffic を終端させて、HTTP status 202Accepted を返却• CDN を前段に配置し、Edge からのRequest Cache を返却TrafficterminateShiftNorth
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Shift North に対するブロッカーについて考える。
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.なぜ、Shift North できないか?• Traffic の大部分が Write traffic となっており、CDNのキャッシュ効率が悪い• Edge では認証できない独自の認証プロトコル、または Edge での認証による Latency を許容できない• Write traffic の終端を Queue にしてしまうと、非同期に処理結果の通知アーキテクチャを導入することになり、クライアント側の実装変更が発生する19
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.要件定義から Shift Northできないか考える。
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.こんな要件にできないか?• Traffic の大部分を一次受付し、仕分けする• 解放された traffic• 抑制された traffic• 一次受付時の 要件を検証(API Keyなど)または ”認証なし” にする• 一次受付には CDN, Queue を利用21
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.仮想的な待合室を用意する22API Gatewayclient1NorthSouthVirtual Waiting room一次 trafficMainApplicationserving counterwaiting
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.仮想的な待合室を用意する23API Gatewayclient1NorthSouthVirtual Waiting roomMainApplicationserving counter = 8position = 21
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.仮想的な待合室を用意する24API Gatewayclient1NorthSouthVirtual Waiting roomMainApplicationPolling...serving counter = 12position = 21
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.仮想的な待合室を用意する25API Gatewayclient1NorthSouthVirtual Waiting roomMainApplicationPolling...position <= serving positionserving counter= 26serving counter = 26position = 21
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.仮想的な待合室を用意する26API Gatewayclient1NorthSouthVirtual Waiting roomMainApplication認証トークン要求
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.仮想的な待合室を用意する27API Gatewayclient1NorthSouthVirtual Waiting roomMainApplicationAccess Token
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.仮想的な待合室を用意する28API Gatewayclient1NorthSouthVirtual Waiting roomMainApplicationTokenValidationToken
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.仮想的な待合室を用意することにより Shift North29API Gatewayclient1NorthSouthVirtual Waiting roomE2E長距離な抑制されたtrafficShiftNorth短距離な解放された traffic
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Shift North できるようになったのか?• Traffic の大部分が Write traffic となっており、CDNのキャッシュ効率が悪い→ Main App 側の E2Eの仕組みに変更はなく、仮想待合室側にCDN配備• Edge では認証できない独自の認証プロトコル、または Edge での認証による Latency を許容できない→ 認証は待合室を抜ける際に、そのトラフィックだけバックエンドに通す• Write traffic の終端を Queue にしてしまうと、非同期に処理結果の通知アーキテクチャを導入することになる→ Queue を用いるのは待合室側であり、Main App の E2E は同期に設計可能30
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Virtual Waiting Room31
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Virtual Waiting Room on AWS32
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Virtual Waiting Room principal1. 既存アプリを大幅変更することなく Side-Car Application としてアタッチできること2. Main App で利用される様々な認証機構に対応できること3. コントロールパネルが備わっており、簡単に設定変更できること4. 仮想待合室自体の Traffic 耐性があること5. 仮想待合室がカスタマイズすることで、Main App 側での複雑な仕様に対応可能なこと33
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.1/ Side-Car Attachment34Cloud NativeApplicationOn-premApplicationVirtual Waiting room Virtual Waiting roomMain Application の形態によらずアタッチして使える
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.2/ 多様な Authentication に対応• API Gateway authorizers• Lambda Authorizer• IAM Authorizer• JWT Authorizer• Cognito Authorizer• ID プロバイダーを介した OpenID の統合35
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.3/ Control panel が用意されている36Cloud NativeApplicationVirtual Waiting roomControl planeData planeMain Appcontrols
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.3/ Control panel で何ができるか37§ 現在の待合状況をモニターできる• Serving Counter• 先頭から何人までを Main に向けるか• いつでも設定変更可能• ここを Strategy パターンで実装• Waiting Room Size• 現在何人待合室に滞留しているか• Active Tokens• 何人 Main App に通したか• Expired Tokens• 失効したトークンの数
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.3/ Control panel で何ができるか38§ 待合室の設定を変更できる• Increment Serving Counter• 先頭から何人までを Main App に向けるかを設定変更• Reset Waiting Room• 待合室の現在値をリセット
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.4/ 仮想待合室自体の Traffic 耐性39Cloud NativeApplicationVirtual Waiting roomAWS LambdaAmazon CloudFrontAmazon API GatewayAmazon SQSAmazon DynamoDB
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.5/ カスタマイズ性について• Apache License Version 2.0 なので• 改変して複製、公開したり、再配布して商用利用することも可能• Strategy パターンによる Increment Serving Counter 変更戦略• 先頭から何人までを Main App に向けるかを随時設定• MaxSize 戦略• Token発行数、Token失効数、Main App 側でのトランザクション完了数により待合室の最大サイズを調整• Periodic 戦略• 定期実行処理で一律に、 Serving Counter を増加させる40
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Performance testing41
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Locust とは42• Locust は、スクリプト化可能でスケーラブルなパフォーマンステストツール• ユーザーの動作などのシナリオは、Python コードで定義Performance data250,000 users / sechttps://locust.io※ Locust 1,280 プロセス でシナリオベースの負荷試験を実施
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Performance testing on Lambda43Cloud NativeApplicationVirtual Waiting roomAWS Lambda→ on Lambda で動作可能AWS Step FunctionsDistributed MAP"Parameters": {"array.$": "States.ArrayRange(0, 9, 1)"},
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.DEMO44
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 46Position inQueueServingPositionposition <= serving positionWaiting RoomReserve
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 47scroll downMain App に進めるかどうか待合室に何人いるかMain App へ!Waiting Room
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Main App48発行 Token の利用可能期間
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Main App49Shopping siteでのお買い物purchase nowもう一度最初から試すscroll down買い物結果
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Key takeaways50
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Key takeaways• 負荷が高いアプリケーションでは負荷の特性に応じて対処• Request Basis でスケールし Burst 性をもつサービス選定も• 負荷対策の基本は Shift North、前段でハンドリングできる割合を増やせる仕組みを設計段階で組み込む• Shift North できないアプリケーションでは、仮想待合室の検討を試みる• 待合が許容できるように仕様を再検討する• 待合室は Side-Car としてアタッチできるような設計にする51
SERVERLESS VIRTUAL WAITING ROOM© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Thank you!© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.Slides https://speakerdeck.com/_kenshQiita https://qiita.com/_kensh