Slide 1

Slide 1 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon S3 Selectで実現する サーバーレス⾼負荷対応サイト 清家史郎 (@seike460) D - 3 ︓ 2 0 2 2 / 1 1 / 1 0 1 5 : 0 0 - 1 5 : 4 5 株式会社Fusic チームリーダー/プリンシパルエンジニア/エバンジェリスト

Slide 2

Slide 2 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Who am I? • 清家 史郎(@seike460) • 株式会社Fusic(From 福岡) • チームリーダー プリンシパルエンジニア エバンジェリスト • 技術コミュニティ好き • JAWS Days 2018-2020、2022 • AWS Dev Day 2021-2022

Slide 3

Slide 3 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 1. サイト要件 2. アーキテクチャ選定 3. バックエンド構築 4. フロントエンド構築 5. CI/CD構築 6. 課題と対応⽅法 7. 運⽤結果とまとめ Agenda

Slide 4

Slide 4 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. サイト要件

Slide 5

Slide 5 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. サイト要件 今回のサイト要件には以下の要件存在した • 既存のスマホアプリのサブサイトとして構築する • ⼀定のタイミングでスパイクアクセスが発⽣する • サブサイトはスマホアプリと同じ情報元を利⽤する • 情報の検索を⾏う必要があり、情報の更新は⾏わない

Slide 6

Slide 6 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. スマホアプリの状況

Slide 7

Slide 7 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 負荷が来るタイミングはわかっている • 管理アプリからのプッシュ通知からのスパイクアクセス • 指定時間から可能になる申込によるスパイクアクセス 指定時間への申込は通常の構成ではスパイクアクセスに耐えられない

Slide 8

Slide 8 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. スマホアプリ側の対策 Amazon Relational Database Service (Amazon RDS)への 負荷がボトルネックになる • Amazon Auroraのリードレプリカを利⽤して Read/Writeを負荷分散しつつ、強⼒なインスタンスサイズを選択 • ⾮同期に出来る処理を⾮同期化して、 アプリケーションへの同期的な負荷を低減 • 負荷試験を通してアプリケーションの最適化を⾏う • スパイクアクセス時に合わせて各所スケールアウトを⾏う

Slide 9

Slide 9 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. スマホアプリ運⽤ スマホアプリは運⽤スキルの向上もあり安定稼働状態であった ⼀⽅でかなりの運⽤負荷が発⽣している事実があった このような状況から今回のサイト構築要件にて 運⽤負荷をこれ以上増やしたくないという希望があった スマホアプリ 運用グループ サイト 運用グループ

Slide 10

Slide 10 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 2つの要件

Slide 11

Slide 11 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 2つの要件 スパイクアクセスへの対応 • 予測可能なスパイクアクセス • 通常構成では耐えられない事も 予測出来ている 運⽤負荷の低減 • スマホアプリと同⼀の Amazon RDSから情報取得 • 今まで培った運⽤スキルを コピーして運⽤を⾏うような 体制にしたくない サイト 運用グループ

Slide 12

Slide 12 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⾼負荷対策をしたいが運⽤負荷は増やしたくない この時浮かんで来たのがServerless化というアイデア スマホアプリのバックエンドとなっているのはPHPで、 対応メンバーもメイン⾔語はPHPだった そこで⾏ったのがPHPのサーバーレスアプリ化 AWS Lambda x PHPを利⽤したアーキテクチャ構築

Slide 13

Slide 13 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アーキテクチャ選定

Slide 14

Slide 14 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アーキテクチャ

Slide 15

Slide 15 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アーキテクチャ概要 ■サブサイト アカウント • Amazon S3 Select Lambda • CloudFront + S3 • AWS IAM PassRole ■スマホアプリ アカウント • Amazon EventBridge • AWS Fargate Batch • AWS IAM AssumeRole

Slide 16

Slide 16 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. データ⽣成アーキテクチャ • スマホアプリアカウント • Fargateでサブサイトに 必要なデータを定期的に更新する 前述の通りリードレプリカを利⽤ • 既存のTerraformに IAMAssumeRoleを追加 サブサイトアカウントのS3へ Jsonデータ挿⼊を⾏う

Slide 17

Slide 17 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. バックエンドアーキテクチャ • Amazon S3 Select Lambda • Amazon API Gateway + Lambda • RDBMS -> Amazon S3 Select 理論上のボトルネックを RDSからLambdaに移す

Slide 18

Slide 18 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. フロントエンドアーキテクチャ • CloudFront + S3 • CloudFront と S3の静的配信で フロントエンドを配信 • CloudFrontでのキャッシュを利⽤ フロントエンド速度を向上する事で バックエンドへの接続待ちを 滞留させない

Slide 19

Slide 19 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. バックエンド構築

Slide 20

Slide 20 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Bref PHPのCustom Runtime環境を 簡単に構築出来るツール • Serverless Frameworkを利⽤ • AWS Serverless Application Model(SAM)でも利⽤可能 • 公開 Docker Imageも DockerHubで存在するので 開発⽤のDockerComposeも 容易に準備可能

Slide 21

Slide 21 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Laravel • PHPのデファクトスタンダード Webフレームワークと⾔っても 過⾔ではないフレームワーク • AWSとの相性も抜群で S3への接続やSESへの接続、 SQSへの接続等カジュアルに AWSを利⽤することが出来る • Bref公式サイトにてLaravelの 導⼊⽅法も⽰されている為、 スムーズに導⼊可能

Slide 22

Slide 22 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Laravel bref を組み合わせる # Laravelを通常どうりインストール $ composer create-project laravel/laravel testProject # brefパッケージをインストール $ composer require bref/bref bref/laravel-bridge -- update-with-dependencies # serverless.ymlの⽣成 $ php artisan vendor:publish --tag=serverless-config PHP8.1を使いたい場合はphp-80 -> php-81へ変更 ※既に8.2を実験的に提供されています

Slide 23

Slide 23 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS S3 Select • AWS S3にあるJSONやCSVに 対して、フィルタリングした 結果を返してくれる • SQLの主要な構⽂は ほとんど使える • ⼀⽅でOffsetが存在しない このような制限があることは 理解しておく必要がある

Slide 24

Slide 24 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. S3 Select⽤の基底クラスの作成 • Webフレームワークでは ⼀般的にModel層が データアクセスを受け持ちます • RDBMSへのアクセスを受け持つ Laravelの基底Modelの継承を、 独⾃作成したS3Select⽤Modelに 置き換える

Slide 25

Slide 25 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 取得データの考え⽅ • RDBMSからデータを取得する要領で S3 Selectからデータを取得する • Webフレームワークの概念 • データベース→バケット • テーブル→Jsonファイル • レコード→Jsonオブジェクト • リレーション • レコード取得後にPHP側で リレーション構築可能と考察 • 実際はJson作成時にRDBMSへのSQLにて Joinした値をJsonファイルに設定した

Slide 26

Slide 26 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 検索 • フリーワード検索 • S3 Select LIKE • AND OR 検索 • S3 Select AND OR • リレーション先への検索 • Jsonにリレーション情報保存し、 リレーション先の値で検索

Slide 27

Slide 27 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. フロントエンド構築

Slide 28

Slide 28 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. svelte • CloudFront配信のフロントエンド • UIを作成するための コンポーネントベースの JavaScriptフレームワーク • ⽐較的Vanilla JSと同じ感覚で 利⽤出来て、記述量も少ないので 短期間での構築に向いている

Slide 29

Slide 29 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. カジュアルにState管理が出来る Svelteを整備してくれた チームメイトがいた事が⼤きいが とても良い選択をしてくれた • 少ない記述量でのState管理 • 値の共有を⾏うStore機能 フロントエンドが得意領域ではない ⾃分にはすごくシンプルに⼊り込めた

Slide 30

Slide 30 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Vite 前述のフロントエンドを 整えてくれたチームメイトが 最初から導⼊済み • 開発サーバーの起動が爆速 • 開発サーバーの更新も爆速 • envファイルによる環境変数設定も可能

Slide 31

Slide 31 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Tailwind CSS • 前述のフロントエンドを 整えてくれたチームメイトが 最初から導⼊済み • CSS初期設定後の 組込側の微調整も容易で ドキュメント含めて充実していた • ⼀⽅でCSSへの理解⼒がある チームメイトがいた事が⼤きいと感じた

Slide 32

Slide 32 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. CI/CD構築

Slide 33

Slide 33 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Serverless Framework • Serverless Framework • 簡単にLambdaをデプロイする事が 可能な構成管理デプロイツール • Yamlを記述することで簡単に API Gateway付きのLambdaが 構築出来る • Yaml内にAWS CloudFormationの 記法も出来る為、CloudFrontも ここで構成管理が可能

Slide 34

Slide 34 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. GitHub Actions GitHubへのPushやMergeを契機に 動作するCI/CDパイプライン GitHubにてソースコード管理を している時にカジュアルに利⽤可

Slide 35

Slide 35 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. GitHubのbrunch マージを契機にデプロイ • backend • Serverless framework deploy • frontend • aws s3 sync • aws cloudfront create-invalidation • frontendとbackend分けてDeploy • CIも同様に切り分け

Slide 36

Slide 36 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 課題と対応⽅法

Slide 37

Slide 37 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. リアルタイム性 データ更新後にEventBridgeを経て データJsonが作成される 情報の即時性について要件の すり合わせを⾏い30分に ⼀度の更新を⾏う⽅針で解決 また即時更新でなくても良いことで CloudFrontキャッシュ利⽤も可能に

Slide 38

Slide 38 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambdaの制限 1 Lambdaの最⼤ペイロードサイズは6MB 変更不可能な制限 こちらを回避するために、 ペイロード制限を⾏う必要があった UIから必要とされるFieldを精査 S3Slectにて取得するカラムの制限を ⾏うことでペイロードを減らした これでも駄⽬ならペイロード圧縮も 試すつもりだったが 今回はそこまで必要なかった

Slide 39

Slide 39 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambdaの制限 2 全てのカラムが必要な箇所は、 UI側でのページング実装 S3 Select はOffsetのSQL⾮対応 Limit OffsetをPHP側で対応することで 容易に対応出来た

Slide 40

Slide 40 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS マネージドサービスにおけるキャッシュ スループット向上の為に、 API Gatewayのキャッシュを検討 結論、検討の結果API Gatewayの キャッシュは⾏わなかった 後述のパフォーマンスとコスト等の 総合的な判断を⾏った

Slide 41

Slide 41 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 負荷試験 • 実際の負荷に耐えれるのかを 検証するために負荷試験を実施 • CloudFront等のフロントエンドが ボトルネックになることは考え⾟い • バックエンドのLambda + S3 Select に対してApache Benchによる 負荷試験を実施

Slide 42

Slide 42 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. API Gateway キャッシュ利⽤ Brefのデフォルト設定ではAPI Gatewayを HTTP APIとして利⽤している 記述変更によりRest APIへの変更 その後キャッシュを有効化を⾏う

Slide 43

Slide 43 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 負荷試験結果 (500クライアント、2000リクエスト) 0 100 200 300 400 500 600 700 800 900 RPS 500Client – 2000Request GET500 GETキャッシュ500 POST500 ※POSTは検索により 1/10のデータ量

Slide 44

Slide 44 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 負荷試験結果 (100クライアント、200000リクエスト) 0 200 400 600 800 1000 1200 RPS 100Client – 200000Request GET100 GET100キャッシュ POST100 Clod Startが慣らされる事で 更にスループットの上昇 API Gateway のキャッシュは スループットが上がらない 結果となってしまった

Slide 45

Slide 45 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 負荷試験結果 (1000クライアント、200000リクエスト) 0 200 400 600 800 1000 1200 RPS 1000Client – 200000Request GET1000 POST1000 S3 Selectの限界は来るので 必ず検証が必要

Slide 46

Slide 46 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. API Gateway キャッシュについての判断 AWS S3 Select Lambdaの パフォーマンスがかなり良好だった キャッシュにおける 「時間あたりの料⾦」は 本構成において費⽤インパクトあった デフォルトではGETのみ対応であり 設定変更などの管理項⽬が増える キャッシュの適切な利⽤が出来ていない 総合的な判断で⾏わない事にした

Slide 47

Slide 47 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 運⽤結果とまとめ

Slide 48

Slide 48 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 現在の運⽤状況 Custom Runtime運⽤に対する不安や、 リクエストに耐えれるか不安はあったが、特に問題が起きていない 運⽤の負荷もほぼないに等しく、CloudWatchも安定している 今回の2つの要件を同時に解決出来たと考えます 理論を元に検証を⾏い、対策が打てた事が⼤きいと感じている

Slide 49

Slide 49 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 運⽤コスト API GatewayやLambdaの使⽤量を⾒る限り、 リクエストが少ないわけではない Fargate + RDSでの対応なら、⼤きめのリソースをあてたくなります 前述の運⽤する上での⼈⼒コストの低減に合わせて AWS 利⽤料も合わせて考えるとかなりコストインパクトが⼤きい

Slide 50

Slide 50 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. S3 Select Lambdaの選択に対して S3 Selectをアーキテクチャに選択したことは成功だったと考えます 要件をアーキテクチャに落とし込む際に、 制限の中でしっかり検証することでシステムに付加価値を出せました

Slide 51

Slide 51 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. API Gatewayのキャッシュについて 正しく利⽤出来ていない可能性も否定出来ない ⼀⽅で検証結果を元に⽅針の変更も⾏ったのは、 知識を正しく運⽤するという⾯において正しい判断の可能性もある

Slide 52

Slide 52 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 今後の導⼊やスキル継承について 別プロジェクトでも導⼊を進めている Laravel + AWSの相性の良さもあり、実装が簡略化出来る 現在別のプロジェクトにてSESへの対応など含めて 私の代わりに若⼿のPHPerがコーディング中であり スキル継承も滞りなく⾏えると感じています

Slide 53

Slide 53 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 知識を最⼤限に⽣かして価値提供 AWSは数百万のビジネスを⽀えており、 リクエストを⽀える基盤は整っている 出来る、出来ないを正しく理解して検証、導⼊することで、 エンジニアリングにおけるさらなる価値提供が可能

Slide 54

Slide 54 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Thank you! © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 清家 史郎 株式会社 Fusic @seike460