apache/kafka · error · IllegalStateException

Tried to force a rebalance but consumer does not have a grou

Error message

Tried to force a rebalance but consumer does not have a group.

What it means

Thrown by KafkaConsumer.enforceRebalance(...) when the consumer has no group coordinator, i.e. group.id is not set (or coordinator was never created). enforceRebalance only makes sense for consumers participating in a group; without one there is nothing to rejoin, so the call is invalid. This is a client-side check before any network interaction.

Source

Thrown at clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java:1089

    }

    @Override
    public ConsumerGroupMetadata groupMetadata() {
        acquireAndEnsureOpen();
        try {
            throwIfGroupIdNotDefined();
            return coordinator.groupMetadata();
        } finally {
            release();
        }
    }

    @Override
    public void enforceRebalance(final String reason) {
        acquireAndEnsureOpen();
        try {
            if (coordinator == null) {
                throw new IllegalStateException("Tried to force a rebalance but consumer does not have a group.");
            }
            coordinator.requestRejoin(reason == null || reason.isEmpty() ? DEFAULT_REASON : reason);
        } finally {
            release();
        }
    }

    @Override
    public void enforceRebalance() {
        enforceRebalance(null);
    }

    @Override
    public void close() {
        close(CloseOptions.timeout(Duration.ofMillis(DEFAULT_CLOSE_TIMEOUT_MS)));
    }

    @Deprecated

View on GitHub (pinned to c31c9215e1)

Solutions

  1. Only call enforceRebalance() on consumers that use subscribe() with a group.id.
  2. Set group.id in consumer config if you intend to participate in a group and force rejoin.
  3. Remove the call from manual-assignment (assign()) code paths; manual assignment does not rebalance.

Example fix

// before (manual assignment)
consumer.assign(List.of(tp));
consumer.enforceRebalance(); // throws

// after - remove the call for assign()-based consumers
consumer.assign(List.of(tp));
// or, if you want rebalances, switch to subscribe:
// props.put(ConsumerConfig.GROUP_ID_CONFIG, "g1");
// consumer.subscribe(List.of("topic"));
Defensive patterns

Strategy: validation

Validate before calling

// enforceRebalance() requires a group; skip it for assign() (no-group) consumers:
if (groupId != null && !groupId.isEmpty()) {
    consumer.enforceRebalance("operator-triggered");
} else {
    log.debug("Skipping enforceRebalance: consumer has no group (manual assignment)");
}

Type guard

// Distinguish grouped vs standalone consumers at the type level:
sealed interface KafkaClient permits GroupedConsumer, StandaloneConsumer {}
final class GroupedConsumer {
    final String groupId;
    void enforceRebalance(String reason) { /* delegate */ }
}
final class StandaloneConsumer {
    // no enforceRebalance method exists at all — the call site won't compile
}

Try / catch

// Only swallow this specific misconfiguration; let other IllegalState exceptions propagate:
try {
    consumer.enforceRebalance();
} catch (IllegalStateException e) {
    if (!e.getMessage().contains("does not have a group")) throw e;
    log.info("enforceRebalance ignored: standalone consumer");
}

Prevention

When it happens

Trigger: Calling consumer.enforceRebalance() on a consumer built with assign() (manual assignment, no group.id); calling it when group.id is null or empty; using enforceRebalance in a standalone consumer to 'refresh'.

Common situations: Generic utility code that always calls enforceRebalance regardless of subscribe vs assign; misconfigured deployments missing group.id; copying code from a subscribing consumer into an assigning one.

Related errors


AI-assisted analysis of apache/kafka@c31c9215e1 (2026-08-03). Data as JSON: /data/errors/4c875c55281c1c7a.json. Report an issue: GitHub.