目录

目标

Consumer API版本

技术支持

分区重平衡及相关概念

什么时候可能发生分区重平衡

分区重平衡的意义

分区重平衡策略

range策略(按范围顺序分配)

round-robin策略(按轮询方式分配)

sticky策略(粘性策略)


目标

了解kafka分区重平衡的概念,了解分区重新分配的三种策略。


Consumer API版本

<dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>3.2.0</version>
</dependency>

技术支持

本文相关概念参考了kafka官网以及官网推荐的书籍Kafka The Defi nitive Guide(kafka定义指南)。


分区重平衡及相关概念

所有权

消费者和分区之间的映射通常称为消费者对分区的所有权。

分区重平衡

将分区所有权从一个消费者移动到另一个消费者被称为重新平衡。

group coordinator

每个消费者组都有一个组协调员(group coordinator)。

group leader
        第一个加入消费者组的消费者成为这个消费者组的组长(group leader)
        group leader从group coordinator获取组内消费者列表信息,并负责为每个消费者分配分区子集。它使用PartitionAssignor的实现来决定哪些分区应由哪个消费者处理。


什么时候可能发生分区重平衡

  • 向消费者组中添加新的消费者;
  • 当消费者宕机或者性能不足时,因为kafka服务器和消费者之间的心跳机制,消费者组会剔除出该消费者;
  • 主题扩容了分区;
  • 消费者组订阅了其他主题。

分区重平衡的意义

重新平衡很重要,因为它们为消费者组提供了高可用性和可扩展性(允许我们轻松安全地添加和删除消费者),但在正常情况下,它们是相当不受欢迎的。在重新平衡期间,消费者不能使用消息,这使消费者组处于一个不可用的短期状态。此外,当分区从一个消费者移动到另一个消费者时,消费者将丢失其当前状态;如果它正在缓存任何数据,则需要刷新其缓存,这会减慢应用程序的速度,直到消费者再次设置其状态。


分区重平衡策略

官网文档

partition.assignment.strategy

A list of class names or class types, ordered by preference, of supported partition assignment strategies that the client will use to distribute partition ownership amongst consumer instances when group management is used. Available options are:

  • org.apache.kafka.clients.consumer.RangeAssignor: Assigns partitions on a per-topic basis.
  • org.apache.kafka.clients.consumer.RoundRobinAssignor: Assigns partitions to consumers in a round-robin fashion.
  • org.apache.kafka.clients.consumer.StickyAssignor: Guarantees an assignment that is maximally balanced while preserving as many existing partition assignments as possible.
  • org.apache.kafka.clients.consumer.CooperativeStickyAssignor: Follows the same StickyAssignor logic, but allows for cooperative rebalancing.

The default assignor is [RangeAssignor, CooperativeStickyAssignor], which will use the RangeAssignor by default, but allows upgrading to the CooperativeStickyAssignor with just a single rolling bounce that removes the RangeAssignor from the list.

Implementing the org.apache.kafka.clients.consumer.ConsumerPartitionAssignor interface allows you to plug in a custom assignment strategy.

Type: list
Default: class org.apache.kafka.clients.consumer.RangeAssignor,class org.apache.kafka.clients.consumer.CooperativeStickyAssignor
Valid Values: non-null string
Importance: medium

根据官网描述,分区重平衡共有三种策略,分别是:

  • range
  • round-robin
  • sticky

range策略(按范围顺序分配)

官方文档对该策略的定义

Class RangeAssignorhttps://kafka.apache.org/32/javadoc/org/apache/kafka/clients/consumer/RangeAssignor.html

截取部分描述

The range assignor works on a per-topic basis. For each topic, we lay out the available partitions in numeric order and the consumers in lexicographic order. We then divide the number of partitions by the total number of consumers to determine the number of partitions to assign to each consumer. If it does not evenly divide, then the first few consumers will have one extra partition.

翻译过来的意思

该策略按照顺序分配分区给消费者,具体的做法是:分区数量➗消费者总数=每个消费者分配的分区数量,如果分配不均衡,则优先给前面的消费者多分配一个分区。概念图如下:


round-robin策略(按轮询方式分配)

官方文档对该策略的定义

Class RoundRobinAssignorhttps://kafka.apache.org/32/javadoc/org/apache/kafka/clients/consumer/RoundRobinAssignor.html

截取部分描述

The round robin assignor lays out all the available partitions and all the available consumers. It then proceeds to do a round robin assignment from partition to consumer. If the subscriptions of all consumer instances are identical, then the partitions will be uniformly distributed. (i.e., the partition ownership counts will be within a delta of exactly one across all consumers.)

For example, suppose there are two consumers C0 and C1, two topics t0 and t1, and each topic has 3 partitions, resulting in partitions t0p0t0p1t0p2t1p0t1p1, and t1p2.

The assignment will be:

  • C0: [t0p0, t0p2, t1p1]
  • C1: [t0p1, t1p0, t1p2]

When subscriptions differ across consumer instances, the assignment process still considers each consumer instance in round robin fashion but skips over an instance if it is not subscribed to the topic. Unlike the case when subscriptions are identical, this can result in imbalanced assignments. For example, we have three consumers C0C1C2, and three topics t0t1t2, with 1, 2, and 3 partitions, respectively. Therefore, the partitions are t0p0t1p0t1p1t2p0t2p1t2p2C0 is subscribed to t0C1 is subscribed to t0t1; and C2 is subscribed to t0t1t2.

That assignment will be:

  • C0: [t0p0]
  • C1: [t1p0]
  • C2: [t1p1, t2p0, t2p1, t2p2]

Since the introduction of static membership, we could leverage group.instance.id to make the assignment behavior more sticky. For example, we have three consumers with assigned member.id C0C1C2, two topics t0 and t1, and each topic has 3 partitions, resulting in partitions t0p0t0p1t0p2t1p0t1p1, and t1p2. We choose to honor the sorted order based on ephemeral member.id.

The assignment will be:

  • C0: [t0p0, t1p0]
  • C1: [t0p1, t1p1]
  • C2: [t0p2, t1p2]

After one rolling bounce, group coordinator will attempt to assign new member.id towards consumers, for example C0 -> C5 C1 -> C3C2 -> C4.

The assignment could be completely shuffled to:

  • C3 (was C1): [t0p0, t1p0] (before was [t0p1, t1p1])
  • C4 (was C2): [t0p1, t1p1] (before was [t0p2, t1p2])
  • C5 (was C0): [t0p2, t1p2] (before was [t0p0, t1p0])

This issue could be mitigated by the introduction of static membership. Consumers will have individual instance ids I1I2I3. As long as 1. Number of members remain the same across generation 2. Static members' identities persist across generation 3. Subscription pattern doesn't change for any member

The assignment will always be:

  • I0: [t0p0, t1p0]
  • I1: [t0p1, t1p1]
  • I2: [t0p2, t1p2]

翻译过来的意思

分区按照循环的方式分配给消费者,如果所有消费者订阅的主题相同,则分区将均匀分布。概念图如下:

如果消费者订阅的主题不同则可能导致分配不平衡。假如:

  • 有三个消费者={c0,c1,c2};
  • 有三个主题={t0,t1,t2};
  • 每个主题的分区数量t0={p0},t1={p0,p1},t2={p0,p1,p2};
  • 消费者订阅的主题:c0={t0},c1={t0,t1},c2={t0,t1,t2}。

每个消费者则分区分配如下:


sticky策略(粘性策略)

官网文档对该策略的定义

Class StickyAssignorhttps://kafka.apache.org/32/javadoc/org/apache/kafka/clients/consumer/StickyAssignor.html详解

        该策略初始分配分区给消费者和round-robin相似,但是一旦重平衡,则round-robin策略会重新初始化分配,而sticky则根据两项原则分配,原则一优于原则二:

  • 原则一:使分区尽可能平均分配给消费者;
  • 原则二:主题分区尽可能地保留在其先前分配的消费者中。

从原则二来看,这或许就是该策略命名为sticky(粘性的)的原因。

图文并茂地带你了解kafka分区Rebalance机制相关推荐

  1. 【Kafka】Kafka的Rebalance机制可能造成的影响及解决方案

    一.kafka的rebalance机制 在Kafka中,当有新消费者加入或者订阅的Topic数发生变化时,会触发Rebalance(再均衡:在同一个消费者组当中,分区的所有权从一个消费者转移到另外一个 ...

  2. kafka消费者Rebalance机制

    目录 1.Rebalance机制 2.消费者Rebalance分区分配策略 3.Rebalance过程 1.Rebalance机制 rebalance就是说如果消费组里的消费者数量有变化或消费的分区数 ...

  3. kafka分区副本机制

    Kafka为分区引入了多副本(Replica)机制,通过增加副本数量可以提升容灾能力.同一分区的不同副本中保存的是相同消息(在同一时刻,副本之前并非完全一样),副本之间是"一主多从" ...

  4. Kafka的rebalance机制

    这是针对Kafka的消费者的机制,以下场景将发生rebalance: 消费者组新增消费实例或者有消费实例退出group: group消费超时: group订阅的topic个数发生变化: group订阅 ...

  5. Kafka组消费之Rebalance机制

    点击上方蓝色字体,选择"设为星标" 回复"资源"获取更多资源 大数据技术与架构 点击右侧关注,大数据开发领域最强公众号! 暴走大数据 点击右侧关注,暴走大数据! ...

  6. kafka 消息分发机制、分区和副本机制

    一.消息分发机制 1.1 kafka 消息分发策略 消息是 kafka 中最基本的数据单元,在 kafka 中,一条消息由key.value两部分构成,在发送一条消息 时,我们可以指定这个key,那么 ...

  7. Kafka Consumer Group和Consumer Rebalance机制

    参考文章: Kafka Consumer Group和Consumer Rebalance机制 Kafka Consumer Group和Consumer Rebalance机制 - 简书 在新建一个 ...

  8. 聊聊 Kafka: Consumer 源码解析之 Rebalance 机制

    一.前言 我们上一篇分析了 Consumer 如何加入 Consumer Group,其实上一篇是一个很宏观的东西,主要讲 ConsumerCoordinator 怎么与 GroupCoordinat ...

  9. kafka分区机制详解

    本文来说下SpringBoot整合kafka之kafka分区实战 文章目录 kafka分区机制 分区个数选择 分区写入策略 轮询策略 随机策略 按键保存策略 本文小结 kafka分区机制 分区机制是k ...

  10. KafKa - 分区副本ISR选举机制

    一.KafKa分区副本ISR选举机制 kafka 中每一个主题又进一步划分成若干个分区.副本的概念实际上是在分区层级下定义的,每个分区配置有多若干个副本. 所谓的副本,本质上就是一个只能追加写消息的提 ...

最新文章

  1. 【iOS】NSDate分类,获得中国农历
  2. 青少年软件编程等级考试 python-中国电子学会、北大等推出青少年软件编程等级标准升级版...
  3. 通用前端监控采集脚本
  4. 使用C#調用外部程式或是執行DOS命令
  5. 如何备份数据_如何通过归档、备份和灾难恢复实现多云数据保护
  6. 1034 有理数四则运算 (20 分)java
  7. Java 数组转 List 的三种方式及对比
  8. jsp九大内置对象和四种属性范围介绍
  9. 以太网的分层架构_读《企业应用架构模式》记录-分层
  10. Chrome浏览器showModalDialog兼容性及解决方案
  11. DotNetBar 6.6.0.1 Crack
  12. 圣诞收到最搞笑的短信两则
  13. Qt-textEdit 滚顶条设置只读模式
  14. 点云处理学习笔记(八)-- 点云配准
  15. SAS时间序列分析案例--有季节效应的非平稳序列分析
  16. lottie动画android,Lottie动画简介
  17. SRAM、PSRAM、SPI FLASH初步认识
  18. 计算机假期计划内容,2019寒假计划,超详细学习计划表
  19. 清晰明了有趣味的数字加密讲解
  20. Linux基础篇之权限的设定

热门文章

  1. uniapp对接ucharts图表
  2. CorelDRAW2022新版本序列号 cdrx8安装向导教程
  3. 微软商店下载显示错误,win11无法下载更新的解决方法
  4. react 创建组件的两种方式
  5. 我的职业规划500字计算机范文,职业生涯规划自我分析(职业生涯规划500字)
  6. Excel如何实现随机不重复抽取
  7. vep文件如何转换mp4_VEP视频文件怎么转换成普通视频文件?
  8. annovar与VEP对SNP进行位置注释
  9. 百分点感知智能实验室:语音识别技术发展阶段探究
  10. Java编程——九九乘法表