<p><strong>Background</strong></p>
<p>We have a simple producer/consumer style application with Kafka as the message broker and Consumer Processes running as Kubernetes pods. We have defined two topics namely the in-topic and the out-topic. A set of consumer pods that belong to the same consumer group read messages from the in-topic, perform some work and finally write out the same message (key) to the out-topic once the work is complete.</p>
<p><strong>Issue Description</strong></p>
<p>We noticed that there are duplicate messages being written out to the out-topic by the consumers that are running in the Kubernetes pods. To rephrase, two different consumers are consuming the same messages from the in-topic twice and thus publishing the same message twice to the out-topic as well. We analyzed the issue and can safely conclude that this issue only occurs when pods are auto-downscaled/deleted by Kubernetes.</p>
<p>In fact, an interesting observation we have is that if any message is read by two different consumers  from the in-topic (and thus published twice in the out-topic), the given message is always the last message consumed by one of the pods that was downscaled. In other words, if a message is consumed twice, the root cause is always the downscaling of a pod.</p>
<p>We can conclude that a pod is getting downscaled after a consumer writes the message to the out-topic but before Kafka can commit the offset to the in-topic.</p>
<p><strong>Consumer configuration</strong></p>
<pre><code>props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, &quot;true&quot;);
props.put(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG, &quot;3600000&quot;);
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, &quot;latest&quot;);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,&quot;org.apache.kafka.common.serialization.StringDeserializer&quot;);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG&quot;org.apache.kafka.common.serialization.StringDeserializer&quot;)
</code></pre>
<p><strong>Zookeeper/broker logs</strong> :</p>
<pre><code>[2021-04-07 02:42:22,708] INFO [GroupCoordinator 0]: Preparing to rebalance group PortfolioEnrichmentGroup14 in state PreparingRebalance with old generation 1 (__consumer_offsets-17) (reason: removing member PortfolioEnrichmentConsumer13-9aa71765-2518-
493f-a312-6c1633225015 on heartbeat expiration) (kafka.coordinator.group.GroupCoordinator)
[2021-04-07 02:42:23,331] INFO [GroupCoordinator 0]: Stabilized group PortfolioEnrichmentGroup14 generation 2 (__consumer_offsets-17) (kafka.coordinator.group.GroupCoordinator)
[2021-04-07 02:42:23,335] INFO [GroupCoordinator 0]: Assignment received from leader for group PortfolioEnrichmentGroup14 for generation 2 (kafka.coordinator.group.GroupCoordinator)
</code></pre>
<p><strong>What we tried</strong></p>
<p>Looking at the logs, it was clear that rebalancing takes place because of the heartbeat expiration. We added the following configuration parameters to increase the heartbeat and also increase the session time out :</p>
<pre><code>props.put(ConsumerConfig.HEARTBEAT_INTERVAL_MS_CONFIG, &quot;10000&quot;)
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, &quot;latest&quot;);
props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, &quot;900000&quot;);
props.put(ConsumerConfig.MAX_PARTITION_FETCH_BYTES_CONFIG, &quot;512&quot;);
props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, &quot;1&quot;);
</code></pre>
<p>However, this did not solve the issue. Looking at the broker logs, we can confirm that the issue is due to the downscaling of pods.</p>
<p><strong>Question :</strong> What could be causing this behavior where a message is consumed twice when a pod gets downscaled?</p>
<p>Note : I already understand the root cause of the issue; however, considering that a consumer is a long lived process running in an infinite loop, how and why is Kubernetes downscaling/killing a pod before the consumer commits the offset? How do I tell Kubernetes not to remove a running pod from a consumer group until all Kafka commits are completed?</p>

Chetan Kinger
<blockquote>
<p>&quot;What could be causing this behavior where a message is consumed twice when a pod gets downscaled?&quot;</p>
</blockquote>
<p>You have provided the answer already yourself: &quot;[...] that a pod is getting downscaled after a consumer writes the message to the out-topic but before Kafka can commit the offset to the in-topic.&quot;</p>
<p>As the message was <em>processed but not committed</em>, another pod is re-processing the same message again after the downscaling happens. Remember that adding or removing a consumer from a consumer group always initiates a Rebalancing. You have now first-hand experience why this should generally be avoided as much as feasible. Depending on the Kafka version a rebalance will cause every single consumer of the consumer group to stop consuming until the rebalancing is done.</p>
<p>To solve your issue, I see two options:</p>
<ul>
<li>Only remove running pods out of the Consumer Group when they are idle</li>
<li>Reduce the consumer configuration <code>auto.commit.interval.ms</code> to <code>1</code> as this defaults to 5 seconds. This will only work if you set  <code>enable.auto.commit</code> to <code>true</code>.</li>
</ul>
