# org.apache.kafka.common.errors.InvalidFetchSizeException: 获取大小0无效

- **ID:** `kafka/invalid-fetch-size`
- **领域:** kafka
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Kafka 3.4.0 | active | — | — |
| Kafka 3.5.0 | active | — | — |
| Kafka 3.6.0 | active | — | — |

## 解决方案

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);
   ```

## 无效尝试

- **Set fetch.min.bytes to 1 instead of 0** — fetch.min.bytes controls minimum bytes before returning data, not the fetch size itself; setting to 1 may cause unnecessary polling. (50% 失败率)
- **Disable fetch.max.bytes in consumer config** — Removing the config may default to 0 in some clients, causing the same error. (70% 失败率)
- **Upgrade Kafka client to latest version** — The issue is often a misconfigured client, not a bug; upgrading may not fix if config is wrong. (40% 失败率)
