# How to minimize switchover time between Kafka Streams instances

**URL:** <https://forum.confluent.io/t/how-to-minimize-switchover-time-between-kafka-streams-instances/8804>\
**Category:** Kafka Streams\
**Created:** [3 October 2023 06:17 UTC](https://forum.confluent.io/t/how-to-minimize-switchover-time-between-kafka-streams-instances/8804 "2023-10-03T06:17:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hermanjakobsen](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/hermanjakobsen/32/3369_2.png) [@hermanjakobsen](https://forum.confluent.io/u/hermanjakobsen)\
**Post date:** [3 October 2023 06:17 UTC](https://forum.confluent.io/t/how-to-minimize-switchover-time-between-kafka-streams-instances/8804/1 "2023-10-03T06:17:42Z")

</div>

I have an application with strict requirements to downtime. Currently I have an input topic with three partitions, and I am running three instances of the application to handle the data load. In a ideal distribution of the partitions, each instance will process data from its respective partition.

My problem occurs when one of the instances is shut down gracefully. When this happens, the other instances are too slow to take over the idle tasks resulting from the shut down instance. I have configured `num.standby.replicas = 1`, so one of the other instances should be warmed-up and ready to immediately take over the task. However, it seems to take around the default value for `session.timeout.ms` before the task is migrated to a running instance.

Is it possible to enforce the instance shutting down to leave the group during shutdown? And in that way, ensure that the running instances immediately take over the idle task?

---

<div class="post-metadata">

**Author:** ![mjsax](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/mjsax/32/3113_2.png) [@mjsax](https://forum.confluent.io/u/mjsax)\
**Post date:** [4 October 2023 02:50 UTC](https://forum.confluent.io/t/how-to-minimize-switchover-time-between-kafka-streams-instances/8804/2 "2023-10-04T02:50:35Z")

</div>

What you observe is by design. Many release back, Kafka Streams changed how it uses the consumer, and it disabled the consumer feature to send a “leave group request” when the consumer is closed. Thus, in your case, only after `session.timeout.ms` triggers, a rebalance happens.

The goal was to avoid two rebalance when an instance is bounces. Back in the day, there was no incremental rebalancing yet, and the task assignment was not done as well as we do it today. I am personally not 100% sure if this behavior does still make sense of if we should change it back to normal…

To achieve this, the consumer has an internal config `internal.leave.group.on.close` that Kafka Streams sets to `false` – it should be possible to overwrite this config to `true`.

---

<div class="post-metadata">

**Author:** ![hermanjakobsen](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/hermanjakobsen/32/3369_2.png) [@hermanjakobsen](https://forum.confluent.io/u/hermanjakobsen)\
**Post date:** [4 October 2023 05:25 UTC](https://forum.confluent.io/t/how-to-minimize-switchover-time-between-kafka-streams-instances/8804/3 "2023-10-04T05:25:22Z")

</div>

In our case, the “extra” rebalance when an instance bounces is completely fine as rebalancing usually only takes a few milliseconds. Hence, the internal config sounds like a viable option.

Just to clarify, the Stream Properties would look something like this?

```plaintext
import org.apache.kafka.streams.StreamsConfig

Properties().apply {
        put(StreamsConfig.consumerPrefix("internal.leave.group.on.close"), true)
       // More config below...
    }

```

---

<div class="post-metadata">

**Author:** ![mjsax](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/mjsax/32/3113_2.png) [@mjsax](https://forum.confluent.io/u/mjsax)\
**Post date:** [5 October 2023 03:29 UTC](https://forum.confluent.io/t/how-to-minimize-switchover-time-between-kafka-streams-instances/8804/4 "2023-10-05T03:29:02Z")

</div>

Yes, looks correct to me.

---

<div class="post-metadata">

**Author:** ![coltmcnealy-lh](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/coltmcnealy-lh/32/3736_2.png) [@coltmcnealy-lh](https://forum.confluent.io/u/coltmcnealy-lh)\
**Post date:** [26 April 2024 17:46 UTC](https://forum.confluent.io/t/how-to-minimize-switchover-time-between-kafka-streams-instances/8804/5 "2024-04-26T17:46:43Z")

</div>

@hermanjakobsen a more “stable” way of doing it might be the following:

```auto
streams.close(new CloseOptions().leaveGroup(true));

```

_Edit:_ This is not true, there is actually a bug. Matthias is correct, and you have to use the `internal` flag. Please see: [[KAFKA-16514] Kafka Streams: stream.close(CloseOptions) does not respect options.leaveGroup flag. - ASF JIRA](https://issues.apache.org/jira/browse/KAFKA-16514)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/confluentcommunity/original/1X/c49438c90c9df282e9996fdf6971be890c71b65a.svg) [@system](https://forum.confluent.io/u/system)\
**Post date:** [10 June 2024 12:51 UTC](https://forum.confluent.io/t/how-to-minimize-switchover-time-between-kafka-streams-instances/8804/6 "2024-06-10T12:51:34Z")

</div>

Closed on 10 June 2024 12:51 UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
