Partition rebalancing: the pause nobody puts in their SLO

A consumer group of twelve instances processes order events. A routine rolling deploy replaces them one at a time, waiting for each to become healthy before moving on. It takes about four minutes.

During those four minutes the group processes almost nothing. Lag climbs to 90,000 and takes twenty minutes to clear afterwards, so a four minute deploy produces a twenty four minute delay in order processing.

Nobody configured that. The deploy did exactly what it was told, and each of the twelve restarts triggered a rebalance in which every consumer, including the eleven that were not being replaced, stopped.

Eager rebalancing stops everyone

The default protocol is blunt. When group membership changes, every consumer revokes every partition it holds, the coordinator computes a new assignment, and consumers resume with their new partitions.

sequenceDiagram
    participant C1 as Consumer 1
    participant C2 as Consumer 2
    participant CO as Coordinator
    participant C3 as Consumer 3 (joining)

    Note over C1,C2: processing normally
    C3->>CO: join group
    CO->>C1: revoke ALL partitions
    CO->>C2: revoke ALL partitions
    Note over C1,C2: entire group stopped
    CO->>CO: compute assignment
    CO->>C1: assign
    CO->>C2: assign
    CO->>C3: assign
    Note over C1,C2: resume

Consumers 1 and 2 were healthy and processing. They stopped because a third instance joined. That is the design of the eager protocol, and the total cost scales with how often membership changes rather than with how much actually needed to move.

A rolling deploy changes membership twice per instance, once on shutdown and once on startup. Twelve instances is twenty four rebalances.

The rebalance you did not ask for

Deliberate scaling is the easy case. The one that produces incidents is a consumer being ejected because its handler was slow.

Two different timeouts govern this and conflating them wastes a lot of debugging time.

session.timeout.ms covers heartbeats, which are sent by a background thread. If the process dies or the network drops, heartbeats stop and the coordinator ejects the member. Default is 45 seconds.

max.poll.interval.ms covers how long the application may spend between calls to poll(). It has nothing to do with heartbeats: the background thread keeps sending them happily while your handler grinds through a batch. Default is 5 minutes.

So a healthy process with a slow handler keeps heartbeating, keeps looking alive, and gets ejected anyway for not polling. The log line says the consumer is not calling poll frequently enough, which is at least honest, and by then the rebalance has already happened.

The feedback loop is what makes it nasty. A slow handler triggers a rebalance. The rebalance pauses the group. Lag grows. The consumer resumes facing a larger backlog, fetches a full batch, takes longer to process it, and exceeds the interval again.

// The usual fix is not a bigger interval, it is a smaller batch.
props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 50);       // default is 500
props.put(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG, 300_000);

Reducing max.poll.records bounds the worst case time per poll directly. Raising max.poll.interval.ms instead means a genuinely stuck consumer takes longer to be detected, which trades one problem for a slower version of another.

Cooperative rebalancing is the setting to change

Incremental cooperative rebalancing revokes only the partitions that need a new owner. Everything else keeps processing throughout.

props.put(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG,
          CooperativeStickyAssignor.class.getName());

Adding one instance to a group of eleven means moving a couple of partitions. Under the eager protocol all twelve stop. Under cooperative, ten keep working and two partitions move.

It takes two rounds to converge, so the wall clock duration of the rebalance is not shorter. The time during which nothing is being processed is dramatically shorter, and that is the number that shows up as lag.

The upgrade path matters if you have a running group: moving from eager to cooperative requires a two step rolling deploy, first adding the cooperative assignor alongside the existing one, then removing the old one. Switching directly causes the group to fail to form, which is an unpleasant way to find out.

Static membership is the other lever, and it targets deploys specifically.

props.put(ConsumerConfig.GROUP_INSTANCE_ID_CONFIG, "order-consumer-" + podOrdinal);

With a stable instance id, a consumer that restarts within session.timeout.ms reclaims its previous partitions without triggering a rebalance at all. For a rolling deploy where each pod is back within seconds, this removes the rebalances entirely rather than making them cheaper.

What breaks during a rebalance, beyond the pause

In-flight work is the part that produces correctness bugs rather than just latency.

A consumer that has processed messages but not committed their offsets loses its partitions, and the new owner starts from the last committed offset, reprocessing everything since. That is at-least-once behaviour working correctly, and it means a handler that is not idempotent will double apply during every rebalance.

The onPartitionsRevoked callback is where to commit before letting go, and it runs under time pressure, since dragging it out delays the whole group.

consumer.subscribe(topics, new ConsumerRebalanceListener() {
    @Override
    public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
        // Last chance to commit progress. Keep it fast: the group is waiting.
        consumer.commitSync(currentOffsets);
        flushLocalState(partitions);
    }

    @Override
    public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
        restoreLocalState(partitions);
    }
});

Any per-partition local state, a windowed aggregate, a deduplication set, an in-memory cache keyed by partition, has to be rebuilt by the new owner. For a stateful stream processor that rebuild can take longer than the rebalance itself, and it is invisible in rebalance duration metrics.

What I would set and watch

Use the cooperative assignor. On most groups this is the single highest value change and it is one line plus a careful rollout.

Set a static group instance id if deploys are the main source of rebalances, which they usually are.

Bound max.poll.records from measured per-message processing time rather than leaving it at 500, because 500 times your p99 handler duration is the number that has to fit inside the poll interval.

Alert on rebalance rate rather than only on lag. A group rebalancing every few minutes is spending a meaningful fraction of its life not consuming, and lag alone will attribute that to slowness rather than to churn.

Frequently Asked Questions

What causes a Kafka consumer group rebalance?
Any change in group membership or partition assignment: an instance starting, stopping, crashing, or being ejected for failing to poll within max.poll.interval.ms, as well as a topic gaining partitions or a subscription changing. The most common cause in practice is not deliberate scaling but a consumer whose message handler took too long, which the broker interprets as the consumer having died even though the process is running normally.
What is the difference between eager and cooperative rebalancing?
Eager rebalancing, the historical default, has every consumer revoke all of its partitions at the start of a rebalance, so the entire group stops processing until a new assignment is computed and distributed. Cooperative rebalancing, available through the CooperativeStickyAssignor, revokes only the partitions that actually need to change owner, so consumers keep processing everything else throughout. It usually takes two rounds to converge, and the total stop-the-world time is far smaller.
Why does my consumer keep getting kicked out of the group?
Because the time between successive calls to poll exceeded max.poll.interval.ms, which defaults to five minutes. This is not about heartbeats, which run on a background thread, but about how long your handler takes to process one batch. Fetching five hundred records and spending a second on each exceeds the limit, and the fix is usually to reduce max.poll.records rather than to raise the interval, since a lower batch size bounds the worst case processing time per poll.

Sources

[ RELATED_LOGS ]

TTFB: -- ms LOAD: -- s PAYLOAD: -- kb