<p>I am running Kafka on Kubernetes using the Kafka Strimzi operator. I am using incremental sticky rebalance strategy by configuring  my consumers with the following:</p>
<pre><code>ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG,
      org.apache.kafka.clients.consumer.CooperativeStickyAssignor.class.getName()
</code></pre>
<p>Each time I scale consumers in my consumer group all existing consumer in the group generate the following exception</p>
<p><em>Exception in thread &quot;main&quot; org.apache.kafka.common.errors.RebalanceInProgressException: Offset commit cannot be completed since the consumer is undergoing a rebalance for auto partition assignment. You can try completing the rebalance by calling poll() and then retry the operatio</em>n</p>
<p>Any idea on what caused this exception and/or how to resolve it?</p>
<p>Thank you.</p>

<p>The consumer rebalance happens whenever there is a change in the metadata information of a consumer group.</p>
<p>Adding more consumers (scaling in your words) in a group is one such change and triggers a rebalance. During this change, each consumer will be re-assigned partitions and therefore will not know which offsets to commit until the re-assignment is complete. Now, the <code>StickyAssignor</code> does try and ensure that the previous assignment gets preserved as much as possible but the rebalance will still be triggered and even distribution of partitions will take precedence over retaining previous assignment. (Reference - <a href="https://kafka.apache.org/23/javadoc/org/apache/kafka/clients/consumer/StickyAssignor.html" rel="noreferrer">Kafka Documentation</a>)</p>
<p>Rest, the exception's message is self-explanatory that while the rebalance is happening some of the operations are prohibited.</p>
<p><strong>How to avoid such situations?</strong></p>
<p>This is a tricky one because Kafka needs rebalancing to be able to work effectively. There are a few practices you could use to avoid unnecessary impact:</p>
<ol>
<li>Increase the polling time - <code>max.poll.interval.ms</code> - so the possibility of experiencing these exceptions is reduced.</li>
<li>Decrease the number of poll records - <code>max.poll.records</code> or <code>max.partition.fetch.bytes</code></li>
<li>Try and utilise the latest version(s) of Kafka (or upgrade if you're using an old one) as many of the latest upgrades so far have made improvements to the rebalance protocol</li>
<li>Use <a href="https://cwiki.apache.org/confluence/display/KAFKA/KIP-345%3A+Introduce+static+membership+protocol+to+reduce+consumer+rebalances" rel="noreferrer">Static membership protocol</a> to reduce rebalances</li>
<li>Might consider configuring <code>group.initial.rebalance.delay.ms</code> for empty consumer groups (either for the first time deployment or destroyin everything and redeploying again)</li>
</ol>
<p>These techniques can only help you reduce the unnecessary behaviour or exception but will <em>NOT</em> prevent rebalance completely.</p>
