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

カフカはデータベースの夢をみるか - あるいはApache Kafkaの双対性という思想とksqlDBについて

hashi
September 24, 2021

カフカはデータベースの夢をみるか - あるいはApache Kafkaの双対性という思想とksqlDBについて

Apache Kafka Meetup Japan #9 登壇資料

hashi

September 24, 2021
Tweet

More Decks by hashi

Other Decks in Technology

Transcript

  1. 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.
  2. 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<String, PositionValue> producer = new KafkaProducer<>(settings); ... final ProducerRecord<String, PositionValue> record = new ProducerRecord<>(“store-order”, key, value); producer.send(record); どこにどう送るか? 何を送るか?
  3. 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<String, PositionValue> record = new ProducerRecord<>(“store-order”, key, value); 何のイベントか? イベント自体 “store-order” is a Topic Kafkaにイベントを送る上で指定する唯一の「何のイベント か」に関わる情報。 KafkaはこのTopic毎にイベントをまとめて保存する。
  4. 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
  5. 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
  6. - Events with the same topic are aggregated and stored.

    - Events are immutable. - New events are appended at the end. ∴ [Topic(Event)] = Stream ≒ Log
  7. 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。
  8. 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
  9. 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
  10. 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]
  11. 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) を表現できる。 • チェスの一手一手を漏れなく、順序 通り適用すればチェス盤の状態を再 現できる。
  12. 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
  13. 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.
  14. 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
  15. 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
  16. 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 (双対性)
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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 プロセスを実行している。
  24. 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<String, String> records = consumer.poll(100); Map<String, Integer> counts = new DefaultMap<String, Integer>(); for (ConsumerRecord<String, Integer> record : records) { String key = record.key(); int c = counts.get(key) c += record.value() counts.put(key, c) } for (Map.Entry<String, Integer> 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
  25. 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。
  26. 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)
  27. 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。
  28. 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!
  29. 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
  30. 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コミュニティに 対する活動に向けた参考とな ります。 お手数ですが是非アンケート のご回答をお願い致します!
  31. 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