How does Kafka's consumer rebalance work and how can it affect consumer performance?
Instruction: Describe the consumer rebalance process in Kafka and discuss the potential impacts on performance and message processing.
Updated
Official answer
A rebalance changes which members of a standard subscribing consumer group own partitions, for example after membership, subscriptions or partition counts change. Assignment aims to distribute ownership; it does not guarantee equal business processing cost.
The protocol matters. Under the classic protocol, an eager assignor revokes all partitions during reassignment, while cooperative assignment limits revocation to partitions that need to move. Kafka 4.x also supports the consumer protocol, whose coordinator performs incremental assignment without the classic global synchronization barrier. Do not claim every rebalance always stops every consumer.
For the consumer protocol, heartbeat/session settings and assignment strategies are controlled on the broker side, with supported client selection options. Classic client settings are not interchangeable with these settings. Verify the deployed protocol before tuning timeouts or assignors.
A rebalance can still cause pauses for transferred partitions, cache/state restoration and replay. Handle revocation and lost ownership carefully: finish or cancel bounded work, commit only completed progress while permitted, and prevent stale workers from committing or producing uncontrolled effects after ownership is lost. A lost-partition callback is not a safe opportunity to assume ownership can be recovered by committing.
Reduce unnecessary churn with appropriate polling/processing bounds and, where suitable, a stable unique static member identity. Static membership can delay reassignment on failure and does not eliminate rebalances. Measure rebalance frequency, duration and per-partition completion age, and test crashes and slow workers.