# CommitFailedError：由于消费者组已重新平衡并将分区分配给其他成员，无法完成提交

- **ID:** `communication/kafka-consumer-commit-failed-rebalance`
- **领域:** communication
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

Kafka 消费者在消费者组重新平衡后尝试提交偏移量，通常是因为处理时间超过了 `max.poll.interval.ms`，导致消费者被从组中移除。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Apache Kafka 3.4 | active | — | — |
| Kafka 3.6 | active | — | — |
| confluent-kafka-python 2.3 | active | — | — |
| spring-kafka 3.0 | active | — | — |

## 解决方案

1. ```
   将 `max.poll.interval.ms` 增加到高于预期最大处理时间的值，例如在消费者配置中设置 `max.poll.interval.ms=600000`（10 分钟）。
   ```
2. ```
   通过异步处理减少每次轮询的处理时间：获取记录，在单独线程池中处理，并在所有处理完成后提交偏移量，例如使用 `enable.auto.commit=false` 和手动异步提交。
   ```
3. ```
   通过设置 `partition.assignment.strategy=CooperativeStickyAssignor` 实现协作式重新平衡（增量重新平衡协议），允许消费者在重新平衡期间保留部分分区。
   ```

## 无效尝试

- **Increase `max.poll.records` to process more records per poll and reduce polling frequency** — Processing more records per poll increases processing time, exacerbating the rebalance issue. (75% 失败率)
- **Disable auto-commit and commit offsets manually after every single record** — Frequent commits increase load and may still fail if a rebalance occurs between commits. (65% 失败率)
- **Set `session.timeout.ms` to a very low value to detect failures faster** — This can cause unnecessary rebalances if consumers are healthy but take slightly longer to poll. (80% 失败率)
