Siam博客

Kafka rebalance 与滚动更新

2023-04-17

Kafka rebalance 与滚动更新

rebalance 的场景

Kafka的Rebalance机制是指在消费者组内有新的消费者加入或有某个消费者退出时,Kafka会重新分配分区给消费者以实现负载均衡。常见的会触发Rebalance的场景包括:

  • 新增Topic或Partition
  • 消费者组成员变化
  • 消费者心跳超时
  • 配置更改

需要注意的是,频繁的Rebalance可能会影响消费者的性能,因此应该尽量避免频繁的Rebalance。

优化

  • 旧版 hyperf/kafka 组件,在首次启动 kafka 的时候提前堵塞10秒,才开始消费数据。已提交修复PR #5636,可升级到新版本。
  • 在消费进程退出时,(比如更新代码,k8s 滚动更新)一定要先处理退出信号,让consumer先汇报leaveGroup指令给kafka服务端,避免服务端等待心跳超时再触发rebalance,从而极大影响业务数据消费。
  • 可以更新kafka服务端版本到 > 2.5.0,其中引入了新的再平衡协议,支持Consumer#poll在重新平衡过程中返回数据,同时也支持非致命的RebalanceInProgressException来通知用户正在进行的重新平衡。另外还引入了面向消费者用户的静态成员,减少了不必要的再平衡。

kafka更新特性摘抄

Notable changes in 2.5.0

When RebalanceProtocol#COOPERATIVE is used, Consumer#poll can still return data while it is in the middle of a rebalance for those partitions still owned by the consumer; in addition Consumer#commitSync now may throw a non-fatal RebalanceInProgressException to notify users of such an event, in order to distinguish from the fatal CommitFailedException and allow users to complete the ongoing rebalance and then reattempt committing offsets for those still-owned partitions.


当使用RebalanceProtocol#COOPERATIVE时,Consumer#poll仍然可以在重新平衡的过程中为那些仍然属于消费者的分区返回数据;此外,Consumer#commitSync现在可以抛出一个非致命的RebalanceInProgressException来通知用户这样的事件,以区别于致命的CommitFailedException,允许用户完成正在进行的重新平衡,然后重新尝试为那些仍然拥有的分区提交抵消。

分割线

Notable changes in 2.3.0

  • We are introducing a new rebalancing protocol for Kafka Connect based on incremental cooperative rebalancing. The new protocol does not require stopping all the tasks during a rebalancing phase between Connect workers. Instead, only the tasks that need to be exchanged between workers are stopped and they are started in a follow up rebalance. The new Connect protocol is enabled by default beginning with 2.3.0. For more details on how it works and how to enable the old behavior of eager rebalancing, checkout incremental cooperative rebalancing design.
  • We are introducing static membership towards consumer user. This feature reduces unnecessary rebalances during normal application upgrades or rolling bounces. For more details on how to use it, checkout static membership design.

  • 我们正在为Kafka Connect引入一个新的基于增量合作再平衡的再平衡协议。新的协议不需要在Connect工作者之间的再平衡阶段停止所有的任务。相反,只有那些需要在工作者之间交换的任务才会被停止,并在后续的再平衡中启动它们。从2.3.0开始,新的连接协议被默认启用。关于它如何工作以及如何启用旧的急切再平衡行为的更多细节,请查看增量合作再平衡设计。
  • 我们正在引入面向消费者用户的静态成员。这个功能在正常的应用升级或滚动弹跳中减少了不必要的再平衡。关于如何使用它的更多细节,请查看静态成员设计。
本文链接:
版权声明: 本文由 Siam原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
Tags: Kafka

扫描二维码,分享此文章