Reactive Kafka
with Akka Streams
Krzysztof Ciesielski
about.me
• Scala developer @

• Editor of ScalaTimes

• Open Source (akka-stream-kafka, scala-clippy and other)
Premier Consulting Partner
akka-stream-kafka
• Formely reactive-kafka



• A library for accessing Kafka as stages of Akka Streams
akka-stream-kafka は Akka Streams のストレージとして Kafka にアクセスするラ
イブラリ
Apache Kafka
a message log
8 7 6 5 4 3 2 1
Producer
Producer
Consumer
Consumer
topic “topic1”
Apache Kafka
a distributed message log
4 3 2 1
Producer
4 3 2 1
partition 1
partition 2
“topic1”
“topic1”
分散メッセージログ
Apache Kafka
a distributed message log
Consumer
balancing
4 3 2 1
Producer
4 3 2 1
partition 1
partition 2
“topic1”
“topic1”
分散メッセージログ
Apache Kafka
a distributed message log
Consumer1
Consumer2
4 3 2 1
Producer
4 3 2 1
partition 1
partition 2
“topic1”
“topic1”
分散メッセージログ
Apache Kafka
a distributed message log
4 3 2 1
4 3 2 1
partition 1
partition 3
“topic1”
“topic1”
Consumer1
Consumer2
4 3 2 1
partition 2“topic1”
manual assignment
分散メッセージログ
Apache Kafka
Uses Zookeeper
Leader election
Cluster coordination
Shared configuration
リーダー選出、クラスタ協調、共有された設定に Zookeeper を使う
Apache Kafka
Decoupling of producers and consumers
プロデューサとコンシューマの分離
Apache Kafka
Commit
8 7 6 5 4 3 2 1 Consumer
commit(p1, o3)
msg1, msg2
8 7 6 5 4 3 2 1 Consumer
Apache Kafka
Commit
Apache Kafka
Commit
8 7 6 5 4 3 2 1 New consumer
msg3
Akka Streams
• DSL for describing data transformation pipelines.

• Uses Actor Model.

• Implements the Reactive Streams specification.
アクターモデルを使用した、データ変換パイプラインを記述するためのDSL。
Reactive Streams仕様を実装。
Akka Streams
• Focused on backpressure and async processing.

• Comes with a powerful test kit.

• Extensible.
バックプレッシャーと非同期処理にフォーカス。強力な非同期コード用のテストツー
ル。拡張可能。
Alpakka
Initiative to build a rich set of connectors
HTTP
Streaming TCP
Streaming File IO
Reactive Streams (MongoDB Reactive Streams Java Driver)
豊富なコネクタを構築する取組み
Akka Streams + Kafka
Akka Streams + Kafka
Akka Streams + Kafka
Akka Streams + Kafka
akka-stream-kafka
• Started as reactive-kafka @ SoftwareMill
• First users
• Alexey Romanchuk has rebuilt the library
• Akka Team joined (Patrik Nordwall, Endre Varga)
reactive-kafka として SoftwareMill で開発を始める。Alexey Romanchuk が再構
築し、その後 Akka Team が参加。
Example: plain consumer
plain consumer (素のコンシューマ) の例
Example: plain consumer
plain consumer (素のコンシューマ) の例
Example: plain consumer
+async processing
+error handling
+backpressure
plain consumer (素のコンシューマ) の例
Consumer Actor
SourceStage
ConsumerActor
consumer
(Kafka API)
RequestMessages
demand messages
Messages
Scheduled Poll
poll()
dispatcher
Plain consumer performance
1,202,081
944,746
plain consumer は直接 Java API を叩くぐらいの性能が出ている
Apache Kafka
Commit
8 7 6 5 4 3 2 1 Consumer
commit(p1, o3)
msg1, msg2
次に commit の性能を比較する
Consumer Actor and
commit
SourceStage
ConsumerActor
consumer
(Kafka API)
? Commit
commit()
Scheduled Poll
poll()
dispatcher
msg
msg
commitAsync()
callback
Request
Messages
Committable Source
commit 機能を持った Source
Committable Source
commit 機能を持った Source
Batched Committable
Source
commit を呼ぶと重いのでバッチ化する
Batched Committable
Source
groupedWithin でメッセージを 100 個ごと、もしくは 5 秒経過するごとにグループ
としてまとめる
At-least-once delivery
456,695
1,194,331
External commit source
オフセットを独自に保存することで exactly-once セマンティックを実現する
Producer
Sink に流す形のプロデューサ
Producer
Vanilla API
Producer as a Flow
Flow としてのプロデューサ
Producer as a Flow
Flow としてのプロデューサ
From Kafka to Kafka
Kafka からのログを Kafka へ流す
From Kafka to Kafka
SourceStage
Producer
Stage
msg send()
callback()
msg
msg mapAsync
Consumer
Actor
Kafka からのログを Kafka へ流す
From Kafka to Kafka
Kafka からのログを Kafka へ流す
It’s streaming streams
backpressure per partition
パーティションごとにバックプレッシャーをかける
Source[(TopicPartition, Source[CommittableMessage[Array[Byte], String], NotUsed]), Control]
It’s streaming streams
パーティションごとにバックプレッシャーをかける
Error handling
run() から Future を取得してエラー処理を行う
Java API
Kafka Streams
Apache Kafka 製の Kafka Stream という別物のライブラリもある
Kafka Streams
akka-stream-kafka
Kafka Streams
Scala + Java, backpressure, async processing, interop with Akka Streams
Java, stateful processors, windowing, joining, aggregation operations
Apache Kafka 製の Kafka Stream という別物のライブラリもある
Thank you!
Join us!
https://gitter.im/akka/reactive-kafka
@kpciesielski

Reactive Kafka with Akka Streams