Slide 1

Slide 1 text

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 3 Serverless Virtual Waiting Room サーバーレスで仮想待合室を作ろう! Kensuke Shimokawa Snr. Serverless Specialist Amazon Web Services Japan

Slide 2

Slide 2 text

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 Shimokawa Amazon Web Services Japan Snr. Serverless Specialist Slides https://speakerdeck.com/_kensh Qiita https://qiita.com/_kensh

Slide 3

Slide 3 text

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 • Demo 3

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. どんなときに突発的に負荷が上がるか? 5 ワクチン接種開始 ※ 公共サービス 災害などの速報 アーティストの ライブ配信

Slide 6

Slide 6 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Traffic Pattern で考える。

Slide 7

Slide 7 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Traffic pattern 7 High, stable High, bumpy High, spiky

Slide 8

Slide 8 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. High, stable / bumpy / spiky 8 https://speakerdeck.com/_kensh/aws-lambda-performance-tuning-deep-dive

Slide 9

Slide 9 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Traffic pattern / 今回のターゲット 9 High, stable High, bumpy High, spiky

Slide 10

Slide 10 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Traffic pattern 10 High, spiky High, spiky High, spiky Provisioning Auto scale Request Basis ハンドルできていないトラフィック 余剰リソース 余剰リソース

Slide 11

Slide 11 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Request Basis で考えたいシステム特徴 11 High, spiky Request Basis • Burst 性を持つこと § 俊敏に Ramp-up すること • Scale-in もしっかりしてくれること Amazon API Gateway AWS Lambda Burst : 5000 Burst : 1000〜3000

Slide 12

Slide 12 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Burst が Spike に追いつかない 場合を考える。

Slide 13

Slide 13 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Traffic の方向性 • 今回、考えたいのは North-South Traffic 13 API Gateway client client client North-South Traffic East-West Traffic

Slide 14

Slide 14 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Shift North 15 API Gateway client client client North South • Traffic の終端をできるだけ North に寄せていく Traffic terminate

Slide 15

Slide 15 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Shift North 16 API Gateway client client client North South • Traffic の終端をできるだけ North に寄せていく • Queue や Stream で Traffic を終端 させて、HTTP status 202 Accepted を返却 Queue Stream Traffic terminate Shift North

Slide 16

Slide 16 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Shift North 17 client client client North South CDN • Traffic の終端をできるだけ North に寄せていく • Queue や Stream で Traffic を終端 させて、HTTP status 202 Accepted を返却 • CDN を前段に配置し、Edge からの Request Cache を返却 Traffic terminate Shift North

Slide 17

Slide 17 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Shift North に対するブロッカー について考える。

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 要件定義から Shift North できないか考える。

Slide 20

Slide 20 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. こんな要件にできないか? • Traffic の大部分を一次受付し、仕分けする • 解放された traffic • 抑制された traffic • 一次受付時の 要件を検証(API Keyなど)または ”認証なし” にする • 一次受付には CDN, Queue を利用 21

Slide 21

Slide 21 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 仮想的な待合室を用意する 22 API Gateway client1 North South Virtual Waiting room 一次 traffic Main Application serving counter waiting

Slide 22

Slide 22 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 仮想的な待合室を用意する 23 API Gateway client1 North South Virtual Waiting room Main Application serving counter = 8 position = 21

Slide 23

Slide 23 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 仮想的な待合室を用意する 24 API Gateway client1 North South Virtual Waiting room Main Application Polling... serving counter = 12 position = 21

Slide 24

Slide 24 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 仮想的な待合室を用意する 25 API Gateway client1 North South Virtual Waiting room Main Application Polling... position <= serving position serving counter = 26 serving counter = 26 position = 21

Slide 25

Slide 25 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 仮想的な待合室を用意する 26 API Gateway client1 North South Virtual Waiting room Main Application 認証トークン要求

Slide 26

Slide 26 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 仮想的な待合室を用意する 27 API Gateway client1 North South Virtual Waiting room Main Application Access Token

Slide 27

Slide 27 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 仮想的な待合室を用意する 28 API Gateway client1 North South Virtual Waiting room Main Application Token Validation Token

Slide 28

Slide 28 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 仮想的な待合室を用意することにより Shift North 29 API Gateway client1 North South Virtual Waiting room E2E 長距離な 抑制された traffic Shift North 短距離な 解放された traffic

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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 Room 31

Slide 31

Slide 31 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Virtual Waiting Room on AWS 32

Slide 32

Slide 32 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Virtual Waiting Room principal 1. 既存アプリを大幅変更することなく Side-Car Application として アタッチできること 2. Main App で利用される様々な認証機構に対応できること 3. コントロールパネルが備わっており、簡単に設定変更できること 4. 仮想待合室自体の Traffic 耐性があること 5. 仮想待合室がカスタマイズすることで、Main App 側での複雑な 仕様に対応可能なこと 33

Slide 33

Slide 33 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 1/ Side-Car Attachment 34 Cloud Native Application On-prem Application Virtual Waiting room Virtual Waiting room Main Application の形態によらずアタッチして使える

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 3/ Control panel が用意されている 36 Cloud Native Application Virtual Waiting room Control plane Data plane Main App controls

Slide 36

Slide 36 text

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 • 失効したトークンの数

Slide 37

Slide 37 text

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 • 待合室の現在値をリセット

Slide 38

Slide 38 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 4/ 仮想待合室自体の Traffic 耐性 39 Cloud Native Application Virtual Waiting room AWS Lambda Amazon CloudFront Amazon API Gateway Amazon SQS Amazon DynamoDB

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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 testing 41

Slide 41

Slide 41 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Locust とは 42 • Locust は、スクリプト化可能で スケーラブルなパフォーマンス テストツール • ユーザーの動作などのシナリオ は、Python コードで定義 Performance data 250,000 users / sec https://locust.io ※ Locust 1,280 プロセス で シナリオベースの負荷試験を 実施

Slide 42

Slide 42 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Performance testing on Lambda 43 Cloud Native Application Virtual Waiting room AWS Lambda → on Lambda で動作可能 AWS Step Functions Distributed MAP "Parameters": { "array.$": "States.ArrayRange(0, 9, 1)" },

Slide 43

Slide 43 text

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. DEMO 44

Slide 44

Slide 44 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 46 Position in Queue Serving Position position <= serving position Waiting Room Reserve

Slide 45

Slide 45 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 47 scroll down Main App に 進めるかどうか 待合室に何人 いるか Main App へ! Waiting Room

Slide 46

Slide 46 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Main App 48 発行 Token の 利用可能期間

Slide 47

Slide 47 text

SERVERLESS VIRTUAL WAITING ROOM © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Main App 49 Shopping site でのお買い物 purchase now もう一度 最初から試す scroll down 買い物結果

Slide 48

Slide 48 text

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 takeaways 50

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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/_kensh Qiita https://qiita.com/_kensh