Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous over Synchronous / 同期という思い込み 世界は非同期で構成されている

kensh
January 20, 2024

Asynchronous over Synchronous / 同期という思い込み 世界は非同期で構成されている

サーバーレスサービスである API Gateway や Lambda を使って 同期的な REST API を構築されている方は多いのではないでしょうか。サーバーレスで REST APIをつくると、リクエストの分だけの課金であったり、デフォルトの可用性が与えられたりメリットは大きいです。ただし、サーバーレスの真価は非同期アーキテクチャにあります。このセッションではさまざまなサーバーレスの非同期パターンを紹介するとともに、実際の顧客事例においてどのようなユースケースで非同期系サーバーレスが利用されているかをご紹介いたします。

kensh

January 20, 2024
Tweet

More Decks by kensh

Other Decks in Technology

Transcript

  1. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. J A W S - U G 大 分 同期という思い込み 世界は非同期で構成されている Kensuke Shimokawa Snr. Serverless Specialist Amazon Web Services Japan Serverless Event Driven Architecture
  2. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 2 Kensuke Shimokawa Amazon Web Services Japan Snr. Serverless Specialist https://speakerdeck.com/_kensh https://qiita.com/_kensh About me…
  3. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. デリバリーの速度 3
  4. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. イノベーションループ 4 Listen Idea Experiment イノベーション ループ 傾聴 アイデア 実験
  5. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. "デリバリー速度"の獲得 • マイクロサービスの最大のメリットはビジネス変化に対応する"デ リバリー速度"の獲得 • でもAPIで作ったサービスは頻繁な変更に対応するのは難しい • なぜなら API にはインターフェース契約があるから • そこで、”疎結合” にマイクロサービス を組み立てる、イベントドリブン アーキテクチャ 5
  6. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API で結合する 6
  7. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. アーキテクチャの結合度 7 小さなピースに分けて ゆるく結合させたい
  8. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 小さなピースを API で結合してみる 8 API API
  9. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 契約による結合 9 API API インターフェース契約 APIにはインターフェースを公開し 利用者に宣言する必要がある 利用者は公開者の宣言にしたがって 送信する義務を負う
  10. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 互いに依存する 10 API API (API利用者) 契約を守らないと呼び出せない (API提供者) 契約を破棄できない 不自然な維持、冗長な拡張
  11. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 仕様と実装の乖離が許されないAPI = 契約 11 • 契約を記述する手段の進化 • 契約の配布やスキーマレジストリ 管理のマネージド化 • ただし、契約は公開されるが、強制できる ものではない。 • そして、APIエコノミーへ • クローズドなAPIからオープンなAPIへ
  12. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 多様化、複雑化、膨大化する依存関係 12 API 互いに依存する 互いに依存する
  13. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. “デリバリー速度”の獲得? 達成できたか?
  14. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 同期的な API の課題 • 他サービスの障害に影響を受ける • 障害影響範囲 • 依存関係の同時デプロイ • クライアントへの統一した体験が提供しにくい • HTTPリソースやメソッドごとに要件が複雑化 14
  15. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. API における様々な改善 • サービスメッシュ • API ゲートウェイ • サーキットブレーカー • 集約ログ • ポリグロット • GraphQL • サービスディスカバリ (注意) • 今回のセッションでは非同期の理 解のためにあえて同期と比較をし ていますが、同期と非同期は排他 の関係ではなく、通常は協調して 利用します • API の管理方法も日々進化していま すので、API の課題がそのまま当て はまるわけではありません 15 ※本日のセッションではAPIの管理方法の進化については割愛
  16. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 非同期について 16
  17. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 応答性とサービス間依存度の違い 17 API Event
  18. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 結合度を実例で確認 (同期API) 18 プロデューサ コンシューマ 😀 受注 サービス 発送 サービス 会計 サービス • プロデューサとコンシューマ が密結合 • 複数サービスが障害ポイント • サービスクオリティの低いも のが全体のサービス品質を決 定 • 外部への依存度が大きく、拡 張性が乏しい
  19. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 結合度を実例で確認 (同期API) 19 プロデューサ コンシューマ 受注 サービス 発送 サービス 会計 サービス • プロデューサとコンシューマ が密結合 • 複数サービスが障害ポイント • サービスクオリティの低いも のが全体のサービス品質を決 定 • 外部への依存度が大きく、拡 張性が乏しい 😩 ダウンストリームの 障害に影響を受ける
  20. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 結合度を実例で確認 (同期API) 20 プロデューサ コンシューマ 受注 サービス 発送 サービス 会計 サービス 在庫 サービス • プロデューサとコンシューマ が密結合 • 複数サービスが障害ポイント • サービスクオリティの低いも のが全体のサービス品質を決 定 • 外部への依存度が大きく、拡 張性が乏しい NEW NEW 😀 新サービス追加 コードの修正が必要
  21. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 結合度を実例で確認 (非同期Event) 21 プロデューサ コンシューマ 😀 受注 サービス 発送 サービス 会計 サービス • プロデューサとコンシューマ が疎結合 • 各サービスは独立 • 品質、可用性管理を独自に実 施可能 • 外部への依存度が低いため、 拡張性が高い Event
  22. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 結合度を実例で確認 (非同期Event) 22 プロデューサ コンシューマ 😀 受注 サービス 発送 サービス 会計 サービス • プロデューサとコンシューマ が疎結合 • 各サービスは独立 • 品質、可用性管理を独自に実 施可能 • 外部への依存度が低いため、 拡張性が高い Event ダウンストリームの 障害からの分離
  23. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 結合度を実例で確認 (非同期Event) 23 プロデューサ コンシューマ 😀 受注 サービス 発送 サービス 会計 サービス • プロデューサとコンシューマ が疎結合 • 各サービスは独立 • 品質、可用性管理を独自に実 施可能 • 外部への依存度が低いため、 拡張性が高い Event 在庫 サービス コードの修正不要 新サービス追加時に Subscribeするだけ NEW
  24. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 24 非同期ユースケース 顧客事例を添えて
  25. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. アップローダー 25
  26. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. API Gateway でのアップローダーについての課題 • API Gateway には ペイロードサイズ 10 MB 制限が 26
  27. © 2024, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. コーセー様 複数の個別カウンセリングアプリを短期間で開発 顧客体験向上のための診断機能付き 個別アプリを半年で 7 本リリース ⾼⽣産性 スケーラビリティ 共通機能を SAM テンプレート化し 個別アプリをそれぞれ 1、2ヶ⽉で構築 お客様の利⽤が増加しても対応可能 (容量⾒積もりの時間短縮に貢献) ファイアウォール SAM テンプレート化 → 個別アプリを迅速展開 マネージド 業務注⼒ 顧客体験の向上・改善に集中 アプリの運⽤・維持をクラウドに移譲 問診・商品リコメンド 画像解析 カウンセリング 画像 アップロード キューにある 処理要求を取得 解析結果 データ ステート更新 キューに配置 解析結果取得 VPC コーセー HADA mite SEKKISEI 透明感測定 : ログ処理
  28. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. アップローダーとしての非同期パターン • API Gateway には ペイロードサイズ 10 MB 制限が • API Gateway は read / command に専念 • 署名付き URL を発行することで、S3 に対するアップロード • write / upload / event は S3 を対象にすることでスケール • 署名付きなのでセキュア • 署名付き URL を取得できるクライアントは API Gateway 経由で認証済み 28
  29. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. Amazon S3 署名付き URL 29 User API Gateway Lambda Mobile client S3 認証 発行 同期 登録 SQS Lambda 処理 バッファ HTTPS リクエスト 非同期 署名付き URL
  30. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API x Websocket 30
  31. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 非同期アーキテクチャによる応答の課題 • Client が受け取るのは、HTTP 202 Accepted であり、実際の処理結果 ではない • どのように Service B の処理結果を 受け取るか? 31 HTTP 202 Accepted 直接の接続性はない
  32. © 2024, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. 国内⼩売企業 EC/モバイル決済の店舗への伝播処理 WebSocket 中継 EC / モバイル モバイルアプリでの キャッシュレス決済 Web での 事前注⽂ WebSocket マネージド 業務注⼒ ⾼⽣産性 スケーラビリティ (機会損失防⽌) 店舗POS 即時処理 逐次処理 コネクション管理
  33. © 2024, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. コネクション管理 状態管理 KDDI様 auPAY サービスにおけるイベント駆動の決済完了通知サービス キャッシュレス決済処理の完了の 通知機能をサーバーレス実装 顧客体験向上 マネージド ⾃動リソース管理 スケーラビリティ 配信処理エラー率を⼤幅に改善 インシデント応答時間を 1/3 に短縮 サーバー台数の配慮不要の並列配信で - 同時配信実績: 50% 向上 - 単位処理時間: 1/2 未満に短縮 需要増における対応: これまでの 必要作業が⾃動化され、⼯数 ゼロ へ auPAY アプリ WebSocketによる 即時通知 決済完了したら APIをコール 決済完了通知API 決済完了 状態を格納 完了更新イベントを受けて通知 決済処理システム 接続管理・通知 Streams
  34. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. API x Websocket による応答性の克服 • Websocket によるサーバー サイド Push 34 HTTP 202 Accepted connected Push
  35. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. API x Websocket による応答性 35 User API Gateway Websocket 接続 Mobile client サーバーサイドプッシュ SQS 処理 $connect 非同期
  36. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API x キュー 36
  37. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. トラフィック急増の課題 37 リクエストに応じて スケール プロビジョン オートスケール ハンドルできていないトラフィック 余剰リソース 余剰リソース
  38. © 2024, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. USEN様 「U MUSIC」AI によるチャンネル選曲とプレイリスト配信サービス 店舗DX :お店の雰囲気、時間帯、 天候に合わせた最適な BGM を提供 コスト最適化 ⾼⽣産性 数⼗万店舗のプレイリストの作成を 並列化し、1時間ごとに 5分 で完了 開発期間を 数ヶ⽉単位 で短縮 新サービスの迅速な展開を加速化 初期投資を 約 6000万円 削減 運⽤⼯数低減 → 中⻑期の維持費も削減 AI 処理 (学習・スコアリング) 来店者 属性情報 楽曲情報更新 スケーラビリティ データ センター トークン管理 店舗 店舗のお客様 チャンネル情報更新 楽曲管理 配信処理 配信リスト取得 毎時、⾃動学習し 店舗ごとの リストを⽣成 AI 編成担当 チャンネル情報 天気情報
  39. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. トラフィックの方向性 • 今回、考えたいのは North-South Traffic 39 API Gateway client client client North-South Traffic East-West Traffic
  40. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. Shift North 40 API Gateway client client client North South • Traffic の終端をできるだけ North に寄せていく Traffic terminate
  41. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

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

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

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API キャッシュ 43
  44. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. キャッシュ有効期限を超えた場合の レスポンス低下 • 最新キャッシュから応答できているうちは高速 • キャッシュ有効期限を超えた場合、オブジェクト生成して応答 するので遅延が顕著になる 44
  45. © 2024, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. AWS Cloud 産経デジタル様 複数メディアで利⽤する共通機能のAPI化 リアルタイムランキングAPI リアルタイムアンケートAPI アクセス 記録DB ランキング 集計 キャッシュ 判定 ランキング 集計指⽰ アンケート 投票 アンケート 登録・管理 アンケート 管理者 ⾼⽣産性 保守容易性 マネージド リソース⾃動管理 API ニュース サイト ⼀般ユーザー
  46. © 2024, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Sansan様 Eight における⼈物のリアルタイムリコメンデーション ͓஌Γ߹͍Ͱ͔͢ ໊ࢗަ׵ϦΫΤετΛૹ৴ͯ͠ɺ௥Ճ͠·͠ΐ͏ɻ ユーザーアクション 表⽰、クリック、 組織変更… Webサーバー ログ格納 ストリーム処理 (分類、フィルタ) 基礎 データ 洗い替え リコメンド計算/キャッシュ化 レーティング⽤データ作成(洗い替え時) レーティング⽤データ更新(随時更新: 10,000件/分) 計算のための キャッシュ処理 つながり度の計算 データ処理 (追加、更新) リアルタイム スコアリング 10万件/分 レーティング テーブル リコメンド結果 リアルタイム 変更容易性 マネージド 業務注⼒ ⼀連のデータ処理フロー管理(Step Functions)
  47. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. キャッシュ更新指示を受けるキュー • 同期リクエスト応答には 有効期限切れオブジェクトを返却しつつ SQS に更新命令を送信 • SQS からトリガーされた Lambda は非同期に キャッシュを最新化 • 次回以降のキャッシュヒットでは有効期限内のオブジェクト応答 • ランキングやリコメンドなど、”情報の最新さ” の厳密性を求めら れない場合に有効 47
  48. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. Incremental Cache Generation (段階的なキャッシュ生成) 48 User API Gateway Mobile client SQS データストア 非同期 Timestamp が許容範囲外なら キャッシュ応答後にSQSに更新指示 キャッシュ 同期 取得 更新
  49. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. バケツリレー 49
  50. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 小さくて大量のタスクを捌きたい • Apache Spark など、専門性の高いエンジニアの確保が難しい • 再試行の管理を手軽にしたい 50
  51. © 2024, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. AWS Cloud 受取 ダイソー様 サーバーレスによるPOSデータ処理 マネージド ⾃動リソース管理 • 5,000 を超える店舗、 70,000 以上の商品点数 • 今後のデータ増加に ⾃動でリソース拡張 変更容易性 マネージド 業務注⼒ それぞれの実際の負荷に応じて独⽴して⾃動で拡張/縮退 処理優先 or コスト優先で処理量の調整を設定可能 チェック ためる 参照更新 参照 コマンド クエリ SNS SNS SNS
  52. © 2024, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. 横浜ゴム様 需給管理システムのサーバーレス実装 コア業務を内製で再構築 • コア業務のロジックを⾃社で 理解し、差別化機能にする • 運⽤⼯数の省⼒化は必達事項 スケーラビリティ コスト最適化 変更容易性 - 今後の対象拠点の拡⼤展開を ⾒据えたデータ増加に対処可能 - 事業部の要望やデータ構造の 変更に短期での対応が可能 - サーバー運⽤⼯数省⼒化 - ランニングコストの⼤幅な削減 定期実⾏ 各拠点の 販売計画・⽣産・在庫データ 最終出⼒結果 中間作業データ データ準備 データ整形 データ加⼯ 集計 最⼤ 50 並列化実⾏ 加⼯/集計フロー 可視化ツール (tableau) On- Premise 業務担当者
  53. © 2024, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. ワークスアプリケーションズ様 アプリ画⾯⽣成処理の並列化 マネージド ⾃動リソース管理 コスト最適化 スケーラビリティ • 約9000画⾯分の処理 (CPUトータル100時間) • Apache Sparkでも2時間程度の処理、 度重なる分散調整(メンテ困難) → Lambdaで並列化して - 流動的な分散を⾃動で構成 - 10分で処理を完了 CSSコンパイル JSコンパイル ファイル種別 での処理分割 ... ... ... ... ... ... 処理対象画⾯の 構成情報⽣成 処理 ステート 処理状態チェック 処理対象 アプリ情報 成果物 HTML/JS/CSS
  54. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 最近の Map Reduce は Step Functions で 54 "Game Number", "Game Length" 1, 30 2, 29 3, 31 4, 16 ‥ S3 に置かれた CSV ファイル内のデータにビジネスロジックを適用して処理を行う
  55. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 最近の Map Reduce は Step Functions で 55 "Game Number", "Game Length" 1, 30 2, 29 3, 31 4, 16 ‥ S3 に置かれた CSV ファイル内のデータにビジネスロジックを適用して処理を行う
  56. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. 各イテレーションの実行ステータスの確認 / DescribeMapRun API 56
  57. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. まとめ 57 • API Gateway + Lambda で、REST API を生成という王道以外の、 サーバーレス非同期パターンも重要 • 同期、非同期は排他的ではなく一緒に合わせて使える • 多くのエンタープライズ企業で、非同期アーキテクチャが導入さ れている
  58. ASYNCHRONOUS OVER SYNCHRONOUS © 2024, Amazon Web Services, Inc. or

    its affiliates. All rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Thank you!