$30 off During Our Annual Pro Sale. View Details »

サーバーレスで仮想待合室を作ろう! / Serverless Virtual Waiting Room

kensh
September 23, 2023

サーバーレスで仮想待合室を作ろう! / Serverless Virtual Waiting Room

ゲームやスポーツイベントのチケットの販売開始、そして特売や他の大規模なセールなどでトラフィックの大規模なバースト中にウェブサイトがダウンしないようにリクエストをバッファリングするということはよくあると思います。このような場合にAWSのサーバーレスでどのようにシステムを組んでおけばよいでしょうか? このセッションではAWSのソリューションを例に一緒に考えてみたいと思います。

kensh

September 23, 2023
Tweet

More Decks by kensh

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  10. 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
    ハンドルできていないトラフィック
    余剰リソース
    余剰リソース

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  25. 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
    認証トークン要求

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  33. 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 の形態によらずアタッチして使える

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  41. 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 プロセス で
    シナリオベースの負荷試験を
    実施

    View Slide

  42. 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)"
    },

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide