Apache Kafka on Heroku を
活用したイベント駆動アーキテクチャの設計	
Designing Event-Driven Architecture using Apache Kafka on Heroku
17 Nov, 2017
Ayumu Aizawa
Principal Solutions Architect, Heroku
Salesforce.com
Masashi Sato
Technical Specialist
FLECT Inc.
Forward-Looking Statements
​ Statement under the Private Securities Litigation Reform Act of 1995:
​ This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if any
of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-looking
statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of product or
service availability, subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of management for
future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments and customer
contracts or use of our services.
​ The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our
service, new products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth,
interruptions or delays in our Web hosting, breach of our security measures, the outcome of any litigation, risks associated with completed and any possible
mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain, and motivate our
employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling non-salesforce.com
products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of
salesforce.com, inc. is included in our annual report on Form 10-K for the most recent fiscal year and in our quarterly report on Form 10-Q for the most
recent fiscal quarter. These documents and others containing important disclosures are available on the SEC Filings section of the Investor Information
section of our Web site.
​ Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and may not
be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available.
Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.
Today's  Agenda
本⽇日のセッション内容
​  イベント駆動アーキテクチャ概要
​  Apache  Kafka  on  Heroku
​  株式会社フレクト様における事例例紹介
​  Q&A
Speakers
佐藤  正⼠士
株式会社フレクト
テクニカルスペシャリスト
相澤  歩
株式会社セールスフォース・ドットコム
プリンシパル・ソリューションアーキテクト
Ayumu Aizawa
Principal Solutions Architect, Heroku
Salesforce.com
​ @ayumin
イベント駆動アーキテクチャ概要
​ イベント駆動アーキテクチャ(イベント・ドリブン・アーキテクチャ)とは…
•  プログラムの流流れとは別に⽣生成された出来事(イベント)を検知し、それに反応して処理理を実⾏行行(イ
ベントの消費)
•  キーボードが押された
•  ある時刻になった
•  商談が成⽴立立した
•  ⾬雨が降降ってきた
•  …など
Event Driven Architecture Overview
イベントの生成	
イベントの検知	
イベントへの反応と消費	
新たなイベントの生成
イベント駆動アーキテクチャの設計
イベント駆動アーキテクチャを採⽤用することで…
•  コンポーネント間の結合強度度を疎にすることができる
•  コンポーネント同⼠士が⾮非同期に協調することができる
しかし、イベント駆動アーキテクチャのには以下の課題がある…
•  スケーリング :⽣生成されたイベントを受取る部分がスケールしないとイベントが失われる
•  過剰リソース :過剰なリソースが割当てられると、無駄なコストが発⽣生する
•  順序制御 :⽣生成されたイベントの順番を管理理する必要がある
•  処理理ボトルネック :シーケンシャルな処理理がシステム全体のボトルネックになる
•  データの保全性 :あるサービスで障害が発⽣生するとデータが失われる可能性がある
•  …などなど
Designing Event-Driven Architecture
Apache Kafka® の概要
Kafkaの適⽤用領領域
•  システムやアプリケーションを連携するための、可⽤用性
の⾼高いリアルタイム・ストリームデータ・パイプライン
の構築
•  データ変換や、リアクティブ(受動的)な処理理をするた
めのリアルタイム・ストリーム・アプリの構築
Kafkaのコンセプト
•  Kafkaはひとつまたは複数のサーバーによってクラスタ
を構成する
•  Kafkaクラスタはトピックと呼ばれるカテゴリに分類し
てデータを保存する
•  それぞれのデータにはキー、値、時刻(タイムスタン
プ)の情報が含まれる
Apache Kafka® Overview
Apache Kafka®による課題の解決
イベント駆動アーキテクチャの課題はKafkaによって解決される
•  スケーリング :Kafkaクラスタは柔軟に拡張するのでイベントを取りこぼさない
•  過剰リソース :Kafkaクラスタは適切切に管理理することで不不必要なリソース消費を抑制する
•  順序制御 :トピック内の同⼀一パーティションに投稿されたイベントの順序は保存される
•  処理理ボトルネック :Kafkaのクライアントは互いに独⽴立立し、他のクライアントの影響を受けない
•  データの保全性 :Kafkaクラスタは複数のサーバよって構成されデータは⾃自動的に分散管理理される
•  …などなど
Designing Event-Driven Architecture
Demo time!
​ Building streaming app with Apache Kafka on Heroku
Apache Kafka® on Heroku デモ
TwitterのデータをKafkaを経由してプロセス間で連携
web  Dyno
デプロイ
アドオン追加
Apache  Kafka
on  Heroku
Kafka  ダッシュボード アプリケーション・ログ
worker  Dyno
スケール
(workerを起動)
heroku	
  addons:a-ach	
   heroku	
  ps:scale	
  worker=1	
  
heroku	
  addons:open	
   heroku	
  logs	
  -­‐t	
  
heroku	
  create	
  
git	
  push	
  heroku	
  master	
  
heroku	
  config:set	
  
heroku	
  open	
  
http://bit.ly/2i352uE
より複雑な活⽤用例例
トピックを⽤用いてキーワードを分類し、グラフを描画
https://heroku.github.io/kafka-demo/
https://twitter-display.herokuapp.com/
設計上の注意事項
パーティションの切切り分け
•  全てのデータを1つのパーティションで処理理する構成はパフォーマンスの劣劣化を招く。
•  コンシューマプロセスの並列列化はパーティションの数が上限
•  順序性の担保が必要なデータとそうでないデータをデータ設計時点で検討すること
⾮非同期連携に関する考慮
•  Kafkaによるデータ連携は⾮非同期連携。トランザクション整合(Atomicity)が必須の
サービス間では他の⽅方法(API連携など)を利利⽤用する。
•  マイクロサービスの責任分解点を考慮すること
CariotのKafkaの採⽤用事例例
​ Building streaming app with Apache Kafka on Heroku
Masashi Sato
Technical Specialist
FLECT Inc.
リアルタイム⾞車車両管理理  Cariot®(キャリオット)
⾞車車両にSIMカードを搭載したOBD2デバイスを取付る
だけで、⾞車車両の⾛走⾏行行情報をインターネットに接続する
だけで…
•  Salesforce  Platformに構築されたコネクテッド・カーア
プリで営業⾞車車両、トラック、⼯工事⾞車車両の管理理ができる
Real-‐‑‒time  vehicle  management  service  “Cariot®”
エリアマップ 現在位置の配信 ⾛走⾏行行履履歴
ヒートマップ(渋滞情
報)
稼働率率率レポート 危険運転検知
レポート・ダッシュボード 予約管理理 権限設定
なぜCariotが必要とされているか?
複雑な現実世界をクラウドにつなげる
顧客サポートのための情報を⽇日次で収集
デバイスからの情報量量は膨⼤大なため⽇日次でデータ収集してレポートを作成
Google
Cloud  Dataflow
Cariot  
Platform
Google  BigQuery
⽇日次ログ
バッチ処理理
Google
Cloud  Datalab
データを⽇日次バッチで取り込ん
でいるため、翌⽇日にならないと
データ分析ができない!
課題:顧客からの緊急問合せに対して”今”何が起きているかを把握することが困難
顧客サポートのための情報をリアルタイムで管理理画⾯面に反映
Kafkaを活⽤用して膨⼤大なデータをKafkaのトピック種類別に分類
Google
Cloud  Dataflow
Cariot  
Platform
Cariot  管理理画⾯面
Google  BigQuery
Apache  Kafka  on  
Heroku
Apache Kafka on
Heroku
worker  Dyno
Apache  Kafka  on  Heroku  に
リアルタイムでデータを送信
BigQueryのデータを取得し、
通知種別ごとにトピックに分解
worker  Dyno
種別ごとに適切切なチャネルに
データを連携
Demo time!
​ Real world use-case of Cariot
危険運転検知のデモ
Google
Cloud  Dataflow
Cariot  
Platform
Cariot  管理理画⾯面
Google  BigQuery
Apache  Kafka  on  
Heroku
Apache  Kafka  on  
Heroku
worker  Dyno
時速40㌔で⾛走⾏行行していた
⾞車車両の速度度が急に0に!
(⾞車車両事故の可能性?)
危険運転を検知したことを
すぐに管理理者に通知!
worker  Dyno
urgent-‐‑‒events
Apache  Kafka  on  Herokuの採⽤用理理由
メッセージの取りこぼし、ロストを防ぐ
•  Kafkaクラスタによる⾼高いスケーラビリティとデータ保全
最⼤大40個のトピックを設定可能
•  ひとつのKafkaインスタンスで多くの仕分けができる
サービスコンポーネントの疎結合化
•  コンポーネント構成の責任分界点を明確に、アーキテクチャをシンプルにすることが可能
様々なプログラム⾔言語⽤用クライアントライブラリ
•  Cariotではno-‐‑‒kafka(NodeJS)、Apache  Beam  kafkaIO(Java)を採⽤用
Why  Apache  Kafka  on  Heroku
Apache  Kafka  on  Herokuの提供価格(マルチテナント型)
プラン 容量 データ保持期間(最大) 受信サイズ(最大) 送信サイズ(最大) 価格(月額)
basic-0 3.73GB 1週間 64kb/sec 64kb/sec 100 USD
basic-1 29.8GB 512kb/sec 64kb/sec 125 USD
basic-2 59.6GB 4096kb/sec 64kb/sec 175 USD
※  マルチテナント型を利利⽤用する場合、プロビジョニング時に⾃自動的に設定される環境変数
(KAFKA_̲PREFIX)をトピック名に付加すること。
参照資料料⼀一覧
デモアプリ(本セミナーで使⽤用したもの)
•  https://github.com/ayumin/heroku-‐‑‒kafka-‐‑‒demo
Apache  Kafka  Project
•  プロジェクト公式  https://kafka.apache.org/  
•  ⽇日本語訳(⾮非公式)http://mogile.web.fc2.com/kafka/kafka0110/index.html
Apache  Kafka  on  Heroku
•  https://heroku.github.io/kafka-‐‑‒demo/
•  https://devcenter.heroku.com/articles/kafka-‐‑‒on-‐‑‒heroku
•  https://devcenter.heroku.com/articles/multi-‐‑‒tenant-‐‑‒kafka-‐‑‒on-‐‑‒heroku
Kafkaを活⽤用したアプリの設計に関する資料料
•  https://devcenter.heroku.com/articles/kafka-‐‑‒event-‐‑‒stream-‐‑‒modeling
•  https://devcenter.heroku.com/articles/robust-‐‑‒kafka
Apache  Kafka  on  Herokuのマイグレーションに関する資料料
•  https://devcenter.heroku.com/articles/kafka-‐‑‒addon-‐‑‒migration
リアルタイム⾞車車両管理理  Cariot(キャリオット)
•  https://devcenter.heroku.com/articles/kafka-‐‑‒addon-‐‑‒migration
Apache Kafka on Herokuを活用したイベント駆動アーキテクチャの設計と実装

Apache Kafka on Herokuを活用したイベント駆動アーキテクチャの設計と実装