Kafka 获取最大字节不能按预期工作

Kafka fetch max bytes doesn't work as expected

我有一个主题值得 1 GB 的消息。 A. Kafka消费者决定消费这些消息。我该怎么做才能禁止消费者一次消费所有消息?我试图设置

fetch.max.bytes 经纪人

到 30 MB 以允许每个轮询中仅包含 30 MB 的消息。经纪人似乎不尊重这一点,并试图立即将所有消息提供给消费者,导致消费者内存不足错误。我该如何解决这个问题?

Kafka 配置可能会让人不知所措。通常在 Kafka 中,多个配置可以协同工作以实现一个结果。这带来了灵活性,但灵活性是有代价的。

来自 fetch.max.bytes 的文档:

Records are fetched in batches by the consumer, and if the first record batch in the first non-empty partition of the fetch is larger than this value, the record batch will still be returned to ensure that the consumer can make progress.

仅在消费者端,有更多配置需要考虑限制消费者内存使用,包括:

  • max.poll.records:限制在一次轮询调用中检索的记录数。默认值为 500。
  • max.partition.fetch.bytes:限制每个分区获取的字节数。这应该不是问题,因为默认值为 1MB。

根据KIP-81中的信息,实际的内存使用量应该类似于min(num brokers * max.fetch.bytes, max.partition.fetch.bytes * num_partitions)

此外,在同一个 KIP 中:

The consumer (Fetcher) delays decompression until the records are returned to the user, but because of max.poll.records, it may end up holding onto the decompressed data from a single partition for a few iterations.

我建议您也调整这些参数,希望这会让您进入所需的状态。