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

Serverless Data Processing with AWS Lambda and ...

kensh
January 25, 2025
120

Serverless Data Processing with AWS Lambda and Apache Kafka

このセッションでは、AWS Lambda と Apache Kafka を統合して、スケーラブルなサーバーレスデータ処理アプリケーションを構築するための実用的な洞察を得られます。リアルタイムのデータストリーミングと処理にイベント駆動型モデルを使用する方法を学びます。高スループット、フォールトトレランス、低レイテンシーを実現するためのさまざまなアーキテクチャパターンとベストプラクティスを学びます。リアルタイムのデータストリームから価値ある洞察を効率的かつコスト効率よく抽出するサーバーレスデータ処理パイプラインを設計するための知識を身に付けることができます。

kensh

January 25, 2025
Tweet

More Decks by kensh

Transcript

  1. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 1 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. with AWS Lambda and Apache Kafka Kensuke Shimokawa Amine Tei J A W S - U G D I V E R S I T Y , E Q U I T Y , A N D I N C L U S I O N Serverless data processing
  2. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 2 スタッフの紹介 Amine Tei Snr. Serverless Specialist Kensuke Shimokawa Manufacturing Solutions Architect
  3. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 10 Agenda ワークショップについて ワークショップに登場するサービス Go! Build! (もくもく) まとめ、振り返り、QA
  4. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 11 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. ワークショップについて
  5. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 12 ワークショップのゴール • Kafka を利用したストリーミング データに基づきサーバーレスの リアルタイムサービスによる位置追跡を構築する • Amazon Managed Streaming for Apache Kafka (MSK) • AWS Lambda • AWS AppSync Uberの配車サービスみたいなものをサクッと つくってみる (車両の位置情報収集部分のみ)
  6. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 13 作るアーキテクチャ Amazon Managed Streaming for Apache Kafka (Amazon MSK) AWS Lambda AWS AppSync Dashboard Unicorns AWS Cloud
  7. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 14 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. ワークショップに登場するサービス
  8. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 15 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. AWS AppSync
  9. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 16 • OSS QUERY ⾔語 型指定された Schema type Query { getTodos: [Todo] } type Todo { id: ID! name: String! description: String priority: Int duedate: String } クライアントがレスポンス形式 を指定 query { getTodos { id name priority } } type Query { getTodos: [Todo] } type Todo { id: ID! name: String! description: String priority: Int duedate: String } 3つの処理形態 Query Mutation Subscription https://graphql.org/ Over/Under-fetching の是正 A query language for your API GraphQL とは !?
  10. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 17 { "id": "1", "name": "Study", “priority": "1" }, { "id": “2", "name": “Go to gym", “priority": “5" },… type Query { getTodos: [Todo] } type Todo { id: ID! name: String description: String priority: Int duedate: String } query { getTodos { id name priority } } Schema とモデルデータ (Type) クライアントが必要なものだ けをリクエスト リクエストしたデータ のみが返される クライアントがレスポンス形式を指定
  11. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 18 Schema Query Mutation Subscription type Subscription { onComments (eventId: String!): Comment @aws_subscribe(mutations:["commentOnEvent") } type Mutation { commentOnEvent ( eventId: ID!, content: String!, createdAt: String! ): Comment } 3つの処理形態
  12. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 19 S I M P L E S T W A Y T O B U I L D S C A L A B L E A P I S T H A T C O N N E C T A P P L I C A T I O N S T O D A T A 100% Serverless Code 減少に伴い複雑さも減少 安全性と⾼パフォーマンス TLS 暗号化、安全な API、Custom domain、Cache 管理、認証/認可、 WAF、CloudTrail 等による⾼い Resiliency をBuilt-in Full-managed の GraphQL API と Pub/Sub API のセット アップ、管理、メンテナンス、 ⾼可⽤性を実現するためのイン フラプロビジョニング 他 AWS Services との統合により AppSync ⾃体は低 Layer での Utility code を処理し、マニュア ルエラーを削減 AWS AppSync とは
  13. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 20 Data Source 他 Services etc ... AppSync を BFF としたアーキテクチャの場合 Resolver、Data Source、 Back-end services の組合せ は開発者の⾃由 開発における選択肢が多⽤で拡張性が⾮常に ⾼い反⾯、Workload に沿ったアーキテク チャデザインが重要となる Resolver - Unit Resolver - Pipeline Resolver AWS AppSync アーキテクチャ AWS AppSync
  14. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 21 Pub/Sub APIs AWS AppSync Use cases Front-end developer は Serverless な WebSocket 接続を介してSubscribe している API Client に Real-time のデータ更新を発⾏
  15. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 22 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. Amazon Managed Streaming for Apache Kafka / MSK
  16. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 23 Apache Kafka って? Apache Kafka データストア サービスのBus RESTでアクセス可能 Streaming platform
  17. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 24 クラスター容量の初期適正見積りなく、 Apache Kafka クラスターを簡単に実行 容量の拡大縮小、パーティション再割り当て を気にすることなく、I/Oを即座に拡張 ストリーミング量と保持するデータ量に応じ た課金 Amazon MSK Serverless
  18. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 25 Kafka パーティション / Kinesis シャード old new 1 4 5 6 7 8 3 2 9 Partition 0/ shard 1 1 4 5 6 7 8 3 2 10 9 Partition 1/ shard 2 1 4 5 6 7 8 3 2 Partition 2/ shard 3 新規データ 書き込み Kafka トピック / Kinesis ストリーム
  19. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 26 パーティションキー Producer Producer Producer PK1 PK1 PK1 PK1 Pk4 PK4 PK4 PK4 PK2 PK2 PK3 PK3 MD5 hash function PK1 PK1 PK1 PK1 Partition 0 / shard 1 PK2 PK3 PK3 PK2 Partition 1 / shard 2 PK4 PK4 PK4 PK4 Partition 2 / shard 3 Kafka partition/ Kinesis shard
  20. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 27 Kafka オフセット / Kinesis シーケンス番号 コンシューマがストリーム内の処理済み位置を追跡するために保持 1 4 5 6 7 8 3 2 10 9 Partition New data Old Now Time put Producer Consumer-1 offset 4/ sequence number 4 Consumer-2 offset 7/ sequence number 7
  21. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 28 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. ストリームデータ処理
  22. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 29 EventBridge Pipes: AWSのサービスをパイプで繋ぐ Amazon SQS Amazon Kinesis Data Streams Amazon DynamoDB Amazon MSK Amazon MQ Source AWS service Amazon EventBridge Pipes Filter, enrich, and transform your events Filter Choose which events to keep Enrich and transform Restructure, modify, or pull additional data into your event Target AWS services Amazon EventBridge Amazon Kinesis Data Streams AWS Step Functions Amazon API Gateway Amazon SQS Amazon API Gateway AWS Step Functions AWS Lambda Amazon EventBridge API Destinations ※ Amazon EventBridge バス側にKafka Connect⽤のオープンソースコネクタが利⽤可能
  23. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 30 Kinesis ユースケース: データストリームの分割 コードを書かずに多重化されたデータストリームを複数のドメインストリームに分割 Amazon EventBridge Pipes Amazon Kinesis Data Streams Filter Amazon EventBridge Pipes Filter Amazon Kinesis Data Streams Amazon Kinesis Data Streams
  24. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 31 Kafka ユースケース: API destination コードを書かずに、セルフマネージド Kafka クラスターを AWS サービスや HTTP API に接続 VPC Self-managed Kafka Amazon EventBridge Pipes Filter Amazon EventBridge Pipes Filter Amazon SageMaker API Destination Enrichment
  25. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 32 Kafka Lambda consumer のオプション AWS Lambda service Event source mapping AWS Lambda function Kafka sink connector AWS Lambda function
  26. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 34 Lambda event source mapping / ESM イベントソースから読み取り、Lambda 関数を呼び出す Lambda リソース Event Source Amazon Kinesis DynamoDB Streams Amazon SQS Amazon MSK Amazon MQ Apache Kafka Filter Event pattern content filtering Batch AWS Lambda service pollers Target AWS Lambda function Synchronous invoke
  27. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 35 イベントドリブンアーキテク チャ選択における観点 Design Considerations
  28. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 36 At-least once 配信 イベントは、ターゲットに複数回配信さ れることも。処理されたイベントの状態 を追跡することによって重複イベントを 検出するロジックを含める (冪等性) 配信セマンティクス Exactly-once 配信* AWSサービスに重複排除に使用する 識別子を渡すことで重複排除 * Lambdaでリトライ設定している場合は、At-least once • Amazon EventBridge • Amazon SNS 標準トピック • Amazon SQS 標準 キュー • Amazon Kinesis • Amazon SNS FIFO トピック • Amazon SQS FIFO キュー • Kafka Topic
  29. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 37 順序保証なし イベント/メッセージが順序保証 なしで配信 順序セマンティクス* 順序保証 イベントは、パーティション、メッセージグループ などの中で順番に配信 (グローバルな順序保証はありません) • Amazon EventBridge • Amazon SNS 標準トピック • Amazon SQS 標準 キュー • Amazon Kinesis • Amazon SNS FIFO トピック • Amazon SQS FIFO キュー • Kafka Topic * 順不同のイベント処理ロジックが受⼊可能かは、ビジネス要件次第となります。順不動なイベントを処理する ようにアプリケーションを設計できない場合は、代わりにオーケストレーションも検討
  30. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 50 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. Kafka ESM のプロビジョニング モード
  31. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 51 Kafka ESM のプロビジョニング モードの発表 NEW 常時起動のポーラーの最⼩数 と最⼤数を構成可能 より⾼速なスケーリング、 レイテンシに敏感な ワークロードに最適
  32. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 52 Kafka ESM のプロビジョニング モードの発表
  33. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 53 パフォーマンスの違い • BatchSize = 50 • Batching Window = 1 sec • Mean duration = 200ms • Min pollers = 5 Producers • MSK Cluster • 2 brokers • 1 topic • 100 partitions • Record size 1.5KB • Random Partition Key • Initial traffic – 3,000 records / second • Traffic spike - 9,000 records / second
  34. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 54 パフォーマンスの違い • BatchSize = 50 • Batching Window = 1 sec • Mean duration = 200ms • Min pollers = 5 Producers • MSK Cluster • 2 brokers • 1 topic • 100 partitions • Record size 1.5KB • Random Partition Key • Initial traffic – 3,000 records / second • Traffic spike - 9,000 records / second
  35. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 55 オンデマンドとプロビジョニング・ ESM のパフォーマンス 3000 records/sec Kafka ESM – on-demand mode
  36. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 56 オンデマンドとプロビジョニング・ ESM のパフォーマンス 9000 records/sec Kafka ESM – on-demand mode
  37. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 57 オンデマンドとプロビジョニング・ ESM のパフォーマンス ~7 minutes 9000 records/sec Kafka ESM – on-demand mode
  38. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 58 オンデマンドとプロビジョニング・ ESM のパフォーマンス ~7 minutes 9000 records/sec Kafka ESM – on-demand mode
  39. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 59 オンデマンドとプロビジョニング・ ESM のパフォーマンス ~7 minutes ~15 minutes 9000 records/sec Kafka ESM – on-demand mode
  40. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 60 オンデマンドとプロビジョニング・ ESM のパフォーマンス Kafka ESM – on-demand mode Kafka ESM – Provisioned mode (min=5)
  41. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 61 オンデマンドとプロビジョニング・ ESM のパフォーマンス Kafka ESM – on-demand mode Kafka ESM – Provisioned mode (min=5) ~60,000 at peak ~3,000,000 at peak
  42. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 62 スパイクが多いワークロード
  43. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 63 スパイクが多いワークロード
  44. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 64 Thank you! © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark.