在Kafka中,如果为一个主题创建了一个新的分区,会发生什么?

huangapple go评论54阅读模式
英文:

What happens in Kafka if you create a new partition for a topic?

问题

我在一些在线指南上看到,如果你正在使用键排序,新分区最终会打破该排序,但我真的看不出来为什么。
这真的是会发生的情况吗?

英文:

I read on some guides online that if you are using key ordering the new partition will eventually break that ordering, I really can't see how.
Is this really what happens ?

答案1

得分: 1

是的,这通常是发生的情况。更精确地说,没有保证旧的顺序保持不变

消息的分区基本上是通过以下方式进行的:

hash(key) % number_of_partitions

假设你有一个包含两个分区的主题。你的数据(键:值)如下所示:

a:1
b:1
c:1
a:2
b:2
c:2

现在,这些消息将被放入两个分区:

分区0a:1, b:1, a:2, b:2
分区1c:1, c:2

如果你现在添加一个分区,并且将新消息 a:3, b:3, c:3 发布到主题中,可能会出现以下情况:

分区0a:1, b:1, a:2, b:2, a:3
分区1c:1, c:2, c:3
分区2b:3

现在,从这个主题中消费消息时,由于读取 分区0 的一个消费者可能比同一 ConsumerGroup 中读取 分区2 的另一个消费者花费更长的时间,因此你可能会在处理 b:2 之前处理 b:3

英文:

Yes, this is what is usually happening. To be more precise, there is no guarantee that the old ordering stays the same.

The partitioning of messages is basically happening through

hash(key) % number_of_partitions

Let us assume you have a topic with two partitions. Your data (key:value) looks like this

a:1
b:1
c:1
a:2
b:2
c:2

Now, those messages would go into two partitions:

partition0: a:1, b:1, a:2, b:2
partition1: c:1, c:2

If you now add one partition and you produce new messages a:3, b:3, c:3 into the topic you could end up like this:

partition0: a:1, b:1, a:2, b:2, a:3
partition1: c:1, c:2, c:3
partition2: b:3

Now, consuming the messages from this topic, you could end up processing b:3 before processing b:2 because the one consumer reading partition0 might take longer then another consumer of the same ConsumerGroup reading partition2.

huangapple
  • 本文由 发表于 2020年5月5日 16:55:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61609364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定