从分配的分区中获取记录时的 Kafka 消费者公平性

Kafka Consumer Fairness when fetching records from its assigned partitions

考虑具有 3 个分区 P1、P2、P3 的 kafka 主题部署,其中 events/records 在 P1、P2、P3 的分区中滞后分别等于 100、50、75。假设 num.poll.records(可以从代理获取的最大记录数)等于 100。

如果消费者发送请求以从 P1、P2、P3 获取记录,是否可以保证返回的记录将从可用分区中 fairly/uniformly 选择,例如,从 P1 中选择 34 条记录, P2 33 个,P3 33 个。

否则,如何处理返回记录的决定(例如,它是基于第一个回复提取请求的分区领导者,最滞后的分区等)。在这种情况下,如何保证跨不同分区的获取公平性,尤其是处理这种情况,例如,当记录从分配给消费者的一组分区中的单个分区结束时 fetched/read。

谢谢。

消费者使用贪婪 round-robin 算法进行投票。在您的示例中,这意味着所有 100 条消息都将在第一次轮询时从第一个分区中获取。 在第二次轮询(假设没有写入新消息)时,它将从 P2 轮询那里的所有消息,因为该分区上有 50 条消息少于 max.poll.records,它将从 P3 轮询另外 50 条消息。

您可以阅读更多相关信息here

谢谢。尽管如此,根据文档,看起来在下一个 poll/iteration 时,轮询将从 P2 中取出(无论新消息是否写入 P1)。
kafka consumer fairness

As before, we'd keep track of which partition we left off at so that the next iteration would begin there. This would not achieve the ideal balancing described above, but it still ensures that each partition gets consumed and requires only a single pass over the fetched records.

所以这是一种inter-fetch/poll公平。否则,主题中的几个分区可能会无限期地表现出饥饿。