kafka protocol_error ai_generated true

org.apache.kafka.common.errors.InvalidFetchSizeException: Fetch size 0 is invalid

ID: kafka/invalid-fetch-size

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
1Evidence
2024-06-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Kafka 3.4.0 active
Kafka 3.5.0 active
Kafka 3.6.0 active

Root Cause

Consumer or follower fetch request specified a fetch size of 0 bytes, which is not allowed by the broker protocol.

generic

中文

消费者或跟随者获取请求指定了0字节的获取大小,代理协议不允许此操作。

Official Documentation

https://kafka.apache.org/documentation/#consumerconfigs_fetch.message.max.bytes

Workarounds

  1. 80% success Set fetch.max.bytes to a positive value (e.g., 52428800) in consumer properties. Example: properties.put(ConsumerConfig.FETCH_MAX_BYTES_CONFIG, 52428800); properties.put(ConsumerConfig.MAX_PARTITION_FETCH_BYTES_CONFIG, 1048576); # Also ensure fetch.min.bytes is > 0 properties.put(ConsumerConfig.FETCH_MIN_BYTES_CONFIG, 1);
    Set fetch.max.bytes to a positive value (e.g., 52428800) in consumer properties.
    Example:
    properties.put(ConsumerConfig.FETCH_MAX_BYTES_CONFIG, 52428800);
    properties.put(ConsumerConfig.MAX_PARTITION_FETCH_BYTES_CONFIG, 1048576);
    # Also ensure fetch.min.bytes is > 0
    properties.put(ConsumerConfig.FETCH_MIN_BYTES_CONFIG, 1);
  2. 75% success If using Kafka Streams, ensure 'fetch.max.bytes' is set in StreamsConfig. Command to check current config: kafka-consumer-groups --bootstrap-server localhost:9092 --group my-group --describe # If fetch.max.bytes is 0, override in properties: props.put(StreamsConfig.consumerPrefix(ConsumerConfig.FETCH_MAX_BYTES_CONFIG), 52428800);
    If using Kafka Streams, ensure 'fetch.max.bytes' is set in StreamsConfig.
    Command to check current config:
    kafka-consumer-groups --bootstrap-server localhost:9092 --group my-group --describe
    # If fetch.max.bytes is 0, override in properties:
    props.put(StreamsConfig.consumerPrefix(ConsumerConfig.FETCH_MAX_BYTES_CONFIG), 52428800);

中文步骤

  1. Set fetch.max.bytes to a positive value (e.g., 52428800) in consumer properties.
    Example:
    properties.put(ConsumerConfig.FETCH_MAX_BYTES_CONFIG, 52428800);
    properties.put(ConsumerConfig.MAX_PARTITION_FETCH_BYTES_CONFIG, 1048576);
    # Also ensure fetch.min.bytes is > 0
    properties.put(ConsumerConfig.FETCH_MIN_BYTES_CONFIG, 1);
  2. If using Kafka Streams, ensure 'fetch.max.bytes' is set in StreamsConfig.
    Command to check current config:
    kafka-consumer-groups --bootstrap-server localhost:9092 --group my-group --describe
    # If fetch.max.bytes is 0, override in properties:
    props.put(StreamsConfig.consumerPrefix(ConsumerConfig.FETCH_MAX_BYTES_CONFIG), 52428800);

Dead Ends

Common approaches that don't work:

  1. Set fetch.min.bytes to 1 instead of 0 50% fail

    fetch.min.bytes controls minimum bytes before returning data, not the fetch size itself; setting to 1 may cause unnecessary polling.

  2. Disable fetch.max.bytes in consumer config 70% fail

    Removing the config may default to 0 in some clients, causing the same error.

  3. Upgrade Kafka client to latest version 40% fail

    The issue is often a misconfigured client, not a bug; upgrading may not fix if config is wrong.