Slide 1

Slide 1 text

Apache Kafka Meetup Japan #9 カフカはデータベースの夢をみるか - あるいはApache Kafka®の双対性という思想とksqlDB®について Shinichi Hashitani, Solutions Engineer, Sep-24, 2021. #kafkajp

Slide 2

Slide 2 text

Kafka and Data Stream-Table Duality Kafka Streams and ksqlDB Is Apache Kafka a database?

Slide 3

Slide 3 text

3 コラム:The Blind Men and The Elephant 『群盲象を評す』 - 元々はインド発祥の寓話。あるも のを一側面からのみ評価することは誤謬に繋がるとい う、真実の多面性を諭す教訓。 『KafkaはIoT基盤』 『Kafkaはイベントストリーム基盤』 『Kafkaはメッセージング基盤』 『Kafkaはマイクロサービスの通信基盤』 『Kafkaはデータフロー基盤』 … 本セッションではあえて『Apache Kafkaはデータ ベース』という観点から掘り下げる事によりApache Kafkaの本質を理解しようとする取り組みであり、 Apache Kafkaが実際データベースなのかという判断 を促すものではない。(i.e. どうでも良い) “Companies are Elephants!”, Wolfgang Göbl.

Slide 4

Slide 4 text

Kafka and Data Stream-Table Duality Kafka Streams and ksqlDB Is Apache Kafka a database?

Slide 5

Slide 5 text

Data Abstraction

Slide 6

Slide 6 text

Relational Database - Tables Data Lake (Hadoop) - Files Apache Kafka - Streams (Logs)

Slide 7

Slide 7 text

Stream Log ● Continuous ● Logical ● Infinite ● Continuous ● Physical ● Finite

Slide 8

Slide 8 text

Event Streams Event-Driven Architecture Event Sourcing Event Hub ...

Slide 9

Slide 9 text

Events… Streams… Logs...

Slide 10

Slide 10 text

Events and Streams

Slide 11

Slide 11 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. 11 final Properties settings = new Properties(); settings.put(ProducerConfig.CLIENT_ID_CONFIG, driverId); settings.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka:9092"); settings.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); settings.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class); settings.put(KafkaAvroSerializerConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://schema-registry:8081"); final KafkaProducer producer = new KafkaProducer<>(settings); ... final ProducerRecord record = new ProducerRecord<>(“store-order”, key, value); producer.send(record); どこにどう送るか? 何を送るか?

Slide 12

Slide 12 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. 12 final ProducerRecord record = new ProducerRecord<>(“store-order”, key, value); 何のイベントか? イベント自体 “store-order” is a Topic Kafkaにイベントを送る上で指定する唯一の「何のイベント か」に関わる情報。 KafkaはこのTopic毎にイベントをまとめて保存する。

Slide 13

Slide 13 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Stream = ある特定の目的 (Topic) で集められたイベントの集合 13 customer login: abc order confirmed: #001 order updated: #002 customer login: efg order canceled: #003 package received: #a01 at dist center: #b02 left dist center: #a02 delivered: #a01 customer C: 0001 order U: 0003 payment U: 0002 payment C: 0003 customer U: 0002 store-order order confirmed: #001 order updated: #002 order canceled: #003 store-customer customer login: abc customer login: efg logistic package received: #a01 left dist center: #a02 delivered: #a01 at dist center: #b02 orderdb-c customer C: 0001 customer U: 0002 orderdb-o order U: 0003 orderdb-p payment C: 0003 payment U: 0002

Slide 14

Slide 14 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Stream と Log 14 customer login: abc order confirmed: #001 order updated: #002 customer login: efg order canceled: #003 Append-Only Immutable 1 2 3 4 5 6 8 7 10 9 11 12 1 2 3 4 5 6 8 7 Old New

Slide 15

Slide 15 text

- Events with the same topic are aggregated and stored. - Events are immutable. - New events are appended at the end. ∴ [Topic(Event)] = Stream ≒ Log

Slide 16

Slide 16 text

16 コラム:I ♥ Logs 2014年O’Reilly Media刊行のApache Kafkaに関する 最初の本。著者は当時LikedinのPrinicipal Staff EngineerのJay Kreps。 データをStructured Logという観点で扱う基盤の構想 と、背景となるデータの考え方に関して詳細に説明し ている。言うなればApache Kafka(とそれを活用し たソフトウェア)のアーキテクチャに焦点を置いた書 籍。 ちなみにJay KrepsはApache Kafka開発メンバーであ り、Confluent創始者の一人であり、現在Confluentの CEO。

Slide 17

Slide 17 text

[Topic(Event)] = Stream ≒ Log [Topic(Event)] = Stream Partition([Topic(Event)]) = Log

Slide 18

Slide 18 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Stream と Partition 18 1 2 3 4 5 6 8 7 10 9 11 12 (“store-order”, key, value) Partition 1 Partition 0 Partition 2 hash(key) mod (Partition Count) = Partition # 3 7 9 10 11 16 17 1 2 12 18 22 4 5 6 13 14 15 20 19 21 23 8

Slide 19

Slide 19 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Partition とイベント順序と並列処理 19 Eventの到達順序はPartition内でのみ保証される。 Partition数 = 最大並列 処理可能スレッド数 3 7 9 10 11 16 17 1 2 12 18 22 4 5 6 13 14 15 20 19 21 8

Slide 20

Slide 20 text

20 コラム:Partition を決めてい るのは Producer Kafka Brokerの主責務は「ログを冗長化、分散配置 し、格納すること」。一般的なメッセージブローカー と異なり、pullモデルを採用しておりConsumerの状 態は観測していない。(「どのConsumerがLogのど こまで取りに来たか」を示すOffsetのみ管理。) EventがどのPartitionにアサインされるかもKafka Brokerでは指定していない。Producerにおける PartitionerがKeyを元に保存先であるPartitionを決 めた上でEventを送っている。 Eventを送るバッチサイズの管理もProducerが行って いる。設定に応じて複数のRecordはPartition毎に Batchとして纏められ、複数のBatchは1つのRequest としてKafka Brokerに送られる。 Producer Producer Record Topic [Partition] [Timestamp] Value Serializer Partitioner Topic A Partition 0 Batch 0 Batch 1 Batch 2 Topic B Partition 1 Batch 0 Batch 1 Batch 2 Kafka Broker Send() Retry ? Fail ? Yes No Can’t retry, throw exception Success: return metadata Yes [Headers] [Key]

Slide 21

Slide 21 text

Next: Data and Streams

Slide 22

Slide 22 text

Kafka and Data Stream-Table Duality Kafka Streams and ksqlDB Is Apache Kafka a database?

Slide 23

Slide 23 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Chess Game Play and Chessboard 23 “Streams and Tables in Apache Kafka: A Primer”, Michael Noll, Confluent Blog. チェスの一手一手とチェス盤の状態は 同じデータの異なる表現方法。 • チェス盤はある特定時点での完全な 状態 (State) を表現できる。 • チェスの一手一手を漏れなく、順序 通り適用すればチェス盤の状態を再 現できる。

Slide 24

Slide 24 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Relational Database Internals 24 処理は全てメモリ上でなされる。 処理に必要なデータはメモリに読み込ま れる。処理後更新されたデータは定期的 にストレージと同期される。 処理はログとして記録される。 障害発生時、ストレージへの同期が未完 の処理を漏れなく順序通り実行すること でデータを復元する。 fsync buffer load

Slide 25

Slide 25 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Database Replication 25 DBのレプリケーション: • PrimaryからSecondaryにログ を漏れなく順序通り渡す • Secondaryにてログを漏れなく 順序通り処理する “Postgres Replication and Automatic Failover Tutorial”, Abbas Butt, EDB.

Slide 26

Slide 26 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Change Data Capture 26 データソースの更新情報をストリーム化し: • 部分的なデータのみ • 異なるスキーマへ • 異なるストレージへ • 恒久的なストレージへ 漏れなく順序通り処理し渡すことによりデー タの整合性を保ちつつ同期する。 The DB

Slide 27

Slide 27 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Stream-Table Duality 27

Slide 28

Slide 28 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Stream-Table Duality 28 Stream (facts) Table (dims) alice Berlin bob Lima alice Berlin alice Rome bob Lima alice Paris bob Sydney alice Berlin alice Rome bob Lima alice Paris bob Sydney 更新情報StreamからTableの状態を再現 更新情報StreamからTableの状態を常に 最新状態に維持 StreamとTableは同じデータを表現する双対関係 Stream-Table Duality (双対性)

Slide 29

Slide 29 text

Next: Processing Streams

Slide 30

Slide 30 text

Kafka and Data Stream-Table Duality Kafka Streams and ksqlDB Is Apache Kafka a database?

Slide 31

Slide 31 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. 31 Apache Kafka ksqlDB kafka.apache.org ksqldb.io Apache Software Foundation Confluent Inc. Apache License 2.0 Confluent Community License github.com/apache/kafka github.com/confluentinc/ksql Free Free Storage Process

Slide 32

Slide 32 text

32 コラム:Confluent Community License Confluent Platform (Schema Registry、ksqlDB、 等)に付随するライセンス。2018年にApache License 2.0より移行。 商業利用を含む利用は無料でありApache License 2.0 に近いが、「SaaS形態での提供不可」という制約を設 けている。 Apache Kafka関連と合わせ、Confluent Clinet、 Serializers等は引き続きApache License 2.0。 License Change for Confluent Platform Confluent Community License FAQ A Developer’s Guide to Confluent Community License Confluent Platform Developer License

Slide 33

Slide 33 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. 33 Apache Kafkaをベースとしたストリーム処理 DB CONNECTOR CONNECTOR APP APP DB STREAM PROCESSING CONNECTOR APP DB

Slide 34

Slide 34 text

“in-transit” Processing It’s awesome and everything, but...

Slide 35

Slide 35 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. 35 Apache Kafkaとの接続 - データのin/out DB CONNECTOR CONNECTOR APP APP DB STREAM PROCESSING CONNECTOR APP DB

Slide 36

Slide 36 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. 36 ストリーム処理 - ランタイムとそれを支える基盤 DB CONNECTOR CONNECTOR APP APP DB STREAM PROCESSING CONNECTOR APP DB

Slide 37

Slide 37 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. 37 ksqlDB Abstraction DB APP APP DB PULL PUSH CONNECTORS STREAM PROCESSING MATERIALIZED VIEWS ksqlDB APP

Slide 38

Slide 38 text

But, before that...

Slide 39

Slide 39 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Kafka Streams / ksqlDB - 異なる抽象化レイヤー 39 ksqlDB Kafka Streams Kafka Connect Producer Consumer Producer/Consumer、Kafka Streams、ksqlDBは独立した技 術ではなく同じスタックの異な る抽象化レイヤーを構成。 Kafka Streamsは Producer/Consumerを利用し、 ksqlDBは内部でKafka Streams プロセスを実行している。

Slide 40

Slide 40 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. コードで見る Kafka Streams と ksqlDB 40 ConsumerRecords records = consumer.poll(100); Map counts = new DefaultMap(); for (ConsumerRecord record : records) { String key = record.key(); int c = counts.get(key) c += record.value() counts.put(key, c) } for (Map.Entry entry : counts.entrySet()) { int stateCount; int attempts; while (attempts++ < MAX_RETRIES) { try { stateCount = stateStore.getValue(entry.getKey()) stateStore.setValue(entry.getKey(), entry.getValue() + stateCount) break; } catch (StateStoreException e) { RetryUtils.backoff(attempts); } } } builder.stream("input-stream", Consumed.with(Serdes.String(), Serdes.String())) .groupBy((key, value) -> value) .count() .toStream() .to("counts", Produced.with(Serdes.String(), Serdes.Long())); SELECT x, count(*) FROM stream GROUP BY x EMIT CHANGES; Consumer Kafka Streams ksqlDB

Slide 41

Slide 41 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Kafka Streams / ksqlDB - 異なる抽象化レイヤー 41 ksqlDB Kafka Streams Producer Consumer ● Stateless / Stateful ● Java or pseudo-SQL ● Stateless ● Many Languages Kafka StreamがTopologyと ステート管理の機能を提 供。この為集約(SUM、 AVG)等の処理が可能であ り、提供言語がJavaのみと なる。 ksqlDBは擬似SQLでの利用 となるが、内部的にはKafka Stream処理に変換される為 中身はJava。

Slide 42

Slide 42 text

Topology? Statefulness? Source Sink Proce ssor Proce ssor Source Proce ssor Proce ssor Proce ssor State Store

Slide 43

Slide 43 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Kafka Streams - Topology and State Store 43 Kafka Streams内で処理フローを管理。 状態は内部のDB (!) で管理。 (RocksDB)

Slide 44

Slide 44 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Kafka Streams - Middleware in Library 44 Karka StreamsはJavaライブラリ。一般的には専 用のランタイム構成が必要なストリーム処理をア プリ内で完結。 ● StreamもしくはTableとしてデータを抽象化。 ● Partition毎に処理。処理を分けるのではなく 対象データセットを分ける事で並列処理。 ● Topology内を流れるのは常に1イベント。 Karka Stream Appは「分散処理オーケストレー ション」が可能なJava App。

Slide 45

Slide 45 text

ksqlDB uses Kafka Streams. Now let us look into kslqDB.

Slide 46

Slide 46 text

ksqlDB: The Animated Illustration

Slide 47

Slide 47 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. by Courtesy of... 47 Michael Drogalis Principal Product Manager of Confluent @MichaelDrogalis developer.confluent.io Thank you, Michael!

Slide 48

Slide 48 text

Next: Apache Kafka and Data

Slide 49

Slide 49 text

Kafka and Data Stream-Table Duality Kafka Streams and ksqlDB Is Apache Kafka a database?

Slide 50

Slide 50 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Database Inside Out 50 fsync buffer load 1 2 3 4 5 6 8 7 10 9 11 12 1 2 3 4 5 6 8 7 Storage Tables Materialized Views Query Engine

Slide 51

Slide 51 text

with a Bonus Content ♥

Slide 52

Slide 52 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Hello! My Name is: 52 創業: 2014 CEO: Jay Kreps 日本オフィス2021年開設 Engineers@Japan Office Ayumu Aizawa Solutions Engineer Shinichi Hashitani Solutions Engineer Keigo Suda Solutions Architect TBD Support Engineer 今後のKafkaコミュニティに 対する活動に向けた参考とな ります。 お手数ですが是非アンケート のご回答をお願い致します!

Slide 53

Slide 53 text

Copyright 2021, Confluent, Inc. All rights reserved. This document may not be reproduced in any manner without the express written permission of Confluent, Inc. Free Stuff! 53 I ♥ Logs by Jay Kreps eBook (pdf) 今後のKafkaコミュニティに 対する活動に向けた参考とな ります。 お手数ですが是非アンケート のご回答をお願い致します! Mastering Kafka Streams and ksqlDB by Mitch Seymour eBook (pdf) 200 USD Credit for 3 months! http://cnfl.io/mu-try-cloud