kafka 流中 statestore 和 changelog 主题的目的?

Cur*_*ind 2 apache-kafka apache-kafka-streams

我有一个 kafka 流应用程序,它在其中使用 stateStore(由 RocksDB 支持)。

流线程所做的就是从 kafka 主题中获取数据并将数据放入状态存储。(还有其他线程从 statestore 读取数据并进行业务逻辑处理)。

我观察到它因为 stateStore 创建了一个新的 kafka 主题“changelog”。

但我没有明白“变更日志”kafka 主题的目的是什么?

  • 为什么需要它(变更日志)?
  • statestore 和“changelog”kafka 主题之间的关系是什么?
  • 谁把数据放到这个话题里?(“变更日志”)

小智 8

Short answer to this question is to achieve fault tolerance.

Details:

changelog enables the State Store in your Kafka Streams application to be fault tolerant. As your application ingests more data into the state store, it gets pushed to the changelog topic, so that if the node that is running the application goes down, then the changelog topic is used to load the state store with the latest state.

Each application thread or instance gets it's own changelog topic partition so that every instance can recreate it's state after the application is restarted post failure.

当对状态存储进行更新时,Kafka Streams 会自动将数据推送到主题。

我建议阅读 Kafka Definitive Guide 的第 11 章 - 它包含对 Kafka Streams 架构和流处理模式的很好的解释。

希望这可以帮助。


tha*_*_DG 2

当您为状态存储启用更改日志记录时,Kafka Streams 会捕获状态更改并将其写入 Kafka 中的更改日志主题。该变更日志主题充当状态的持久且容错的存储,允许在应用程序重新启动或失败时恢复状态。

让我们以字数统计为例。

初始状态:

  • 单词:“你好”,计数:1
  • 单词:“世界”,计数:1

更改日志条目:

当一个单词被处理多次时,状态存储会更新该单词的计数,并将这些更新写入更改日志主题。

  • “你好”的更新:
    • 单词:“你好”,计数:2
  • “世界”更新:
    • 词语:“世界”,计数:2
  • 再次更新“你好”:
    • 单词:“你好”,计数:3

变更日志主题:

的变更日志主题word-count-store可能包含如下记录

  • 键:“hello”,值:1(初始状态)
  • 键:“world”,值:1(初始状态)
  • 键:“hello”,值:2(更新)
  • 键:“世界”,值:2(更新)
  • 键:“hello”,值:3(更新)

恢复状态:

word-count-store如果 Kafka Streams 应用程序重新启动或故障转移到另一个实例,它可以通过从头开始重播变更日志主题来恢复状态。这确保了应用程序实例之间的状态是一致且最新的。

紧凑的主题:

为了优化存储并减少更改日志数据量,可以配置为使用日志压缩。这可确保更改日志主题中仅保留每个密钥的最新更新,从而允许完全恢复状态,同时最大限度地减少存储需求。