# org.apache.kafka.common.errors.RebalanceInProgressException: 组正在重新平衡，因此重新平衡已在进行中。

- **ID:** `kafka/group-rebalance-timeout`
- **领域:** kafka
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

在消费者组重新平衡正在进行时，消费者请求（如偏移提交或加入组）被发出，导致请求被拒绝。

## 版本兼容性

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

## 解决方案

1. ```
   Increase max.poll.interval.ms to allow more time for processing between polls, reducing the chance of rebalance being triggered:

Properties props = new Properties();
props.put("max.poll.interval.ms", 600000);  // 10 minutes
props.put("max.poll.records", 500);  // Fewer records per poll

And ensure the consumer processes records quickly or uses async processing.
   ```
2. ```
   Handle RebalanceInProgressException in the consumer loop by catching the exception and retrying after a short delay:

try {
    consumer.commitSync();
} catch (RebalanceInProgressException e) {
    // Wait for rebalance to complete
    Thread.sleep(1000);
    consumer.poll(Duration.ofSeconds(1));  // Trigger rebalance join
    consumer.commitSync();
}
   ```

## 无效尝试

- **Increase the session.timeout.ms to a very high value** — Session timeout controls heartbeat detection, not rebalance duration; a high value may delay failure detection but does not prevent rebalance conflicts. (80% 失败率)
- **Set the consumer to use static group membership** — Static membership reduces rebalance frequency but does not eliminate it; rebalance can still be triggered by coordinator changes or partition reassignments. (70% 失败率)
